Skip to content

Commit 6306fd8

Browse files
committed
simplify code
1 parent 25a1a2a commit 6306fd8

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

Diff for: chapter04/4-11.c

+13-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Exercise 4-11. Modify getop so that it doesn't need to use ungetch. Hint:
33
* use an internal static variable.
4+
*
45
* Faisal Saadatmand
56
*/
67

@@ -16,7 +17,6 @@
1617
#define MAXVAL 100 /* maximum depth of val stack */
1718
#define BUFSIZE 100
1819
#define MAXVAR 26
19-
#define TOP val[sp - 1] /* top element in stack */
2020

2121
/* functions */
2222
int getop(char []);
@@ -36,9 +36,8 @@ void clearMemory(void);
3636
int sp; /* next free stack position */
3737
double val[MAXVAL]; /* value stack */
3838
double mem[MAXVAR]; /* variables values */
39-
char buf[BUFSIZE]; /* buffer from ungetch */
39+
int buf[BUFSIZE]; /* buffer from ungetch */
4040
int bufp; /* next free position in buf */
41-
int peak; /* flag: peak at top of the stack */
4241
int variable; /* current input variable */
4342
double printed; /* last printed value */
4443

@@ -116,18 +115,18 @@ int getch(void)
116115
/* printTOP: print top of the stack without pop */
117116
void printTOP(void)
118117
{
119-
if (sp < 1)
120-
printf("stack empty\n");
121-
printf("\t%.8g\n", TOP);
118+
double top;
119+
120+
top = pop();
121+
printf("\t%.8g\n", top);
122+
push(top);
122123
}
123124

124125
/* duplicateTop: duplicate the top element in the stack */
125126
void duplicateTop(void)
126127
{
127128
double top;
128129

129-
if (sp < 1)
130-
return;
131130
push(top = pop());
132131
push(top);
133132
}
@@ -137,11 +136,6 @@ void duplicateTop(void)
137136
{
138137
double top1, top2;
139138

140-
if (sp < 2) {
141-
if (sp == 1)
142-
printf("error: 1 element in stack\n");
143-
return;
144-
}
145139
top1 = pop();
146140
top2 = pop();
147141
push(top1);
@@ -151,8 +145,7 @@ void duplicateTop(void)
151145
/* clear: clears the entire stack */
152146
void clearStack(void)
153147
{
154-
while (sp > 1)
155-
pop();
148+
sp = 0;
156149
}
157150

158151
/* mathfunction: call the appropriate math function according to value of s,
@@ -181,7 +174,6 @@ int mathfunction(char s[])
181174
* location in mem and push back to top of stack */
182175
void storeVariable(void)
183176
{
184-
// if (isalpha(variable) && islower(variable)) {
185177
if (variable >= 'a' && variable <= 'z') {
186178
pop();
187179
push(mem[variable - 'a'] = pop());
@@ -222,7 +214,6 @@ int main(void)
222214
push(printed);
223215
} else if (!strcmp(s, "mc")) {
224216
clearMemory();
225-
peak = 1;
226217
} else if (!mathfunction(s))
227218
printf("error: unknown command %s\n", s);
228219
break;
@@ -250,13 +241,13 @@ int main(void)
250241
else
251242
printf("error: zero divisor\n");
252243
break;
253-
case '!':
254-
peak = 1;
244+
case '?':
245+
printTOP();
255246
break;
256-
case '#':
247+
case 'd':
257248
duplicateTop();
258249
break;
259-
case '&':
250+
case 's':
260251
swapTopTwo();
261252
break;
262253
case '~':
@@ -266,11 +257,7 @@ int main(void)
266257
storeVariable();
267258
break;
268259
case '\n':
269-
if (peak) {
270-
printTOP();
271-
peak = 0;
272-
} else
273-
printf("\t%.8g\n", printed = pop());
260+
printf("\t%.8g\n", printed = pop());
274261
break;
275262
default:
276263
if (islower(type))

0 commit comments

Comments
 (0)