Skip to content

Commit dc5f5e6

Browse files
committed
simplify code
1 parent 6306fd8 commit dc5f5e6

File tree

2 files changed

+24
-50
lines changed

2 files changed

+24
-50
lines changed

chapter04/4-5.c

+14-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Exercise 4-5. Add access to library functions like sin, exp and pow. see
33
* <math.h> in Appendix B, Section 4.
4+
*
45
Faisal Saadatmand
56
*/
67

@@ -15,7 +16,6 @@
1516
#define NAME '1' /* signal that a string command was found */
1617
#define MAXVAL 100 /* maximum depth of val stack */
1718
#define BUFSIZE 100
18-
#define TOP val[sp - 1] /* top element in stack */
1919

2020
/* functions */
2121
int getop(char []);
@@ -26,15 +26,13 @@ void ungetch(int);
2626
void printTOP(void);
2727
void duplicateTop(void);
2828
void swapTopTwo(void);
29-
void clearStack(void);
3029
int mathfunction(char []);
3130

3231
/* globals */
3332
int sp; /* next free stack position */
3433
double val[MAXVAL]; /* value stack */
3534
char buf[BUFSIZE]; /* buffer from ungetch */
3635
int bufp; /* next free position in buf */
37-
int peak; /* flag: peak at top of the stack */
3836

3937
/* push: push f onto value stack */
4038
void push(double f)
@@ -114,19 +112,18 @@ void ungetch(int c)
114112
/* printTOP: print top of the stack without pop */
115113
void printTOP(void)
116114
{
117-
if (sp < 1)
118-
printf("stack empty\n");
119-
printf("\t%.8g\n", TOP);
115+
double top;
116+
117+
top = pop();
118+
printf("\t%.8g\n", top);
119+
push(top);
120120
}
121121

122122
/* duplicateTop: duplicate the top element in the stack */
123123
void duplicateTop(void)
124124
{
125125
double top;
126126

127-
if (sp < 1)
128-
return;
129-
130127
push(top = pop());
131128
push(top);
132129
}
@@ -136,22 +133,16 @@ void duplicateTop(void)
136133
{
137134
double top1, top2;
138135

139-
if (sp < 2) {
140-
if (sp == 1)
141-
printf("error: 1 element in stack\n");
142-
return;
143-
}
144136
top1 = pop();
145137
top2 = pop();
146138
push(top1);
147139
push(top2);
148140
}
149141

150-
/* clear: clears the entire stack */
142+
/* clearStack: clear the stack */
151143
void clearStack(void)
152144
{
153-
while (sp > 1)
154-
pop();
145+
sp = 0;
155146
}
156147

157148
/* mathfunction: call the appropriate math function according to value of s,
@@ -216,24 +207,20 @@ int main(void)
216207
else
217208
printf("error: zero divisor\n");
218209
break;
219-
case '!':
220-
peak = 1; /* don't pop */
210+
case '?':
211+
printTOP();
221212
break;
222-
case '#':
213+
case 'd':
223214
duplicateTop();
224215
break;
225-
case '&':
216+
case 's':
226217
swapTopTwo();
227218
break;
228-
case '~':
219+
case 'c':
229220
clearStack();
230221
break;
231222
case '\n':
232-
if (peak) {
233-
printTOP();
234-
peak = 0;
235-
} else
236-
printf("\t%.8g\n", pop());
223+
printf("\t%.8g\n", pop());
237224
break;
238225
default:
239226
printf("error: unknown command %s\n", s);

chapter04/4-6.c

+10-23
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#define MAXVAL 100 /* maximum depth of val stack */
2222
#define BUFSIZE 100
2323
#define MAXVAR 26
24-
#define TOP val[sp - 1] /* top element in stack */
2524

2625
/* functions */
2726
int getop(char []);
@@ -44,7 +43,6 @@ double val[MAXVAL]; /* value stack */
4443
double mem[MAXVAR]; /* variables values */
4544
char buf[BUFSIZE]; /* buffer from ungetch */
4645
int bufp; /* next free position in buf */
47-
int peak; /* flag: peak at top of the stack */
4846
int variable; /* current input variable */
4947
double printed; /* last printed value */
5048

@@ -126,18 +124,18 @@ void ungetch(int c)
126124
/* printTOP: print top of the stack without pop */
127125
void printTOP(void)
128126
{
129-
if (sp < 1)
130-
printf("stack empty\n");
131-
printf("\t%.8g\n", TOP);
127+
double top;
128+
129+
top = pop();
130+
printf("\t%.8g\n", top);
131+
push(top);
132132
}
133133

134134
/* duplicateTop: duplicate the top element in the stack */
135135
void duplicateTop(void)
136136
{
137137
double top;
138138

139-
if (sp < 1)
140-
return;
141139
push(top = pop());
142140
push(top);
143141
}
@@ -147,22 +145,16 @@ void duplicateTop(void)
147145
{
148146
double top1, top2;
149147

150-
if (sp < 2) {
151-
if (sp == 1)
152-
printf("error: 1 element in stack\n");
153-
return;
154-
}
155148
top1 = pop();
156149
top2 = pop();
157150
push(top1);
158151
push(top2);
159152
}
160153

161-
/* clear: clears the entire stack */
154+
/* clear: clear the entire stack */
162155
void clearStack(void)
163156
{
164-
while (sp > 1)
165-
pop();
157+
sp = 0;
166158
}
167159

168160
/* mathfunction: call the appropriate math function according to value of s,
@@ -232,7 +224,6 @@ int main(void)
232224
push(printed);
233225
} else if (!strcmp(s, "mc")) {
234226
clearMemory();
235-
peak = 1;
236227
} else if (!mathfunction(s))
237228
printf("error: unknown command %s\n", s);
238229
break;
@@ -260,8 +251,8 @@ int main(void)
260251
else
261252
printf("error: zero divisor\n");
262253
break;
263-
case '!':
264-
peak = 1;
254+
case '?':
255+
printTOP();
265256
break;
266257
case '#':
267258
duplicateTop();
@@ -276,11 +267,7 @@ int main(void)
276267
storeVariable();
277268
break;
278269
case '\n':
279-
if (peak) {
280-
printTOP();
281-
peak = 0;
282-
} else
283-
printf("\t%.8g\n", printed = pop());
270+
printf("\t%.8g\n", printed = pop());
284271
break;
285272
default:
286273
if (islower(type))

0 commit comments

Comments
 (0)