Skip to content

Commit 059f83e

Browse files
committed
simplify code
1 parent 3397a08 commit 059f83e

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

chapter04/4-10.c

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Exercise 4-10. An alternative organization uses getline to read an entire
33
* input line; this makes getch and ungetch unnecessary. Revise the calculator
44
* to use this approach.
5+
*
56
* Faisal Saadatmand
67
*/
78

@@ -18,7 +19,6 @@
1819
#define MAXVAL 100 /* maximum depth of val stack */
1920
#define BUFSIZE 100
2021
#define MAXVAR 26
21-
#define TOP val[sp - 1] /* top element in stack */
2222

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

@@ -127,18 +126,18 @@ int getLine(char s[], int lim)
127126
/* printTOP: print top of the stack without pop */
128127
void printTOP(void)
129128
{
130-
if (sp < 1)
131-
printf("stack empty\n");
132-
printf("\t%.8g\n", TOP);
129+
double top;
130+
131+
top = pop();
132+
printf("\t%.8g\n", top);
133+
push(top);
133134
}
134135

135136
/* duplicateTop: duplicate the top element in the stack */
136137
void duplicateTop(void)
137138
{
138139
double top;
139140

140-
if (sp < 1)
141-
return;
142141
push(top = pop());
143142
push(top);
144143
}
@@ -148,11 +147,6 @@ void duplicateTop(void)
148147
{
149148
double top1, top2;
150149

151-
if (sp < 2) {
152-
if (sp == 1)
153-
printf("error: 1 element in stack\n");
154-
return;
155-
}
156150
top1 = pop();
157151
top2 = pop();
158152
push(top1);
@@ -162,8 +156,7 @@ void duplicateTop(void)
162156
/* clear: clears the entire stack */
163157
void clearStack(void)
164158
{
165-
while (sp > 1)
166-
pop();
159+
sp = 0;
167160
}
168161

169162
/* mathfunction: call the appropriate math function according to value of s,
@@ -192,7 +185,6 @@ int mathfunction(char s[])
192185
* location in mem and push back to top of stack */
193186
void storeVariable(void)
194187
{
195-
// if (isalpha(variable) && islower(variable)) {
196188
if (variable >= 'a' && variable <= 'z') {
197189
pop();
198190
push(mem[variable - 'a'] = pop());
@@ -233,7 +225,6 @@ int main(void)
233225
push(printed);
234226
} else if (!strcmp(s, "mc")) {
235227
clearMemory();
236-
peak = 1;
237228
} else if (!mathfunction(s))
238229
printf("error: unknown command %s\n", s);
239230
break;
@@ -261,27 +252,23 @@ int main(void)
261252
else
262253
printf("error: zero divisor\n");
263254
break;
264-
case '!':
265-
peak = 1;
255+
case '?':
256+
printTOP();
266257
break;
267-
case '#':
258+
case 'd':
268259
duplicateTop();
269260
break;
270-
case '&':
261+
case 's':
271262
swapTopTwo();
272263
break;
273-
case '~':
264+
case 'c':
274265
clearStack();
275266
break;
276267
case '=':
277268
storeVariable();
278269
break;
279270
case '\n':
280-
if (peak) {
281-
printTOP();
282-
peak = 0;
283-
} else
284-
printf("\t%.8g\n", printed = pop());
271+
printf("\t%.8g\n", printed = pop());
285272
break;
286273
default:
287274
if (islower(type))

0 commit comments

Comments
 (0)