1
1
/*
2
2
* Exercise 4-11. Modify getop so that it doesn't need to use ungetch. Hint:
3
3
* use an internal static variable.
4
+ *
4
5
* Faisal Saadatmand
5
6
*/
6
7
16
17
#define MAXVAL 100 /* maximum depth of val stack */
17
18
#define BUFSIZE 100
18
19
#define MAXVAR 26
19
- #define TOP val[sp - 1] /* top element in stack */
20
20
21
21
/* functions */
22
22
int getop (char []);
@@ -36,9 +36,8 @@ void clearMemory(void);
36
36
int sp ; /* next free stack position */
37
37
double val [MAXVAL ]; /* value stack */
38
38
double mem [MAXVAR ]; /* variables values */
39
- char buf [BUFSIZE ]; /* buffer from ungetch */
39
+ int buf [BUFSIZE ]; /* buffer from ungetch */
40
40
int bufp ; /* next free position in buf */
41
- int peak ; /* flag: peak at top of the stack */
42
41
int variable ; /* current input variable */
43
42
double printed ; /* last printed value */
44
43
@@ -116,18 +115,18 @@ int getch(void)
116
115
/* printTOP: print top of the stack without pop */
117
116
void printTOP (void )
118
117
{
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 );
122
123
}
123
124
124
125
/* duplicateTop: duplicate the top element in the stack */
125
126
void duplicateTop (void )
126
127
{
127
128
double top ;
128
129
129
- if (sp < 1 )
130
- return ;
131
130
push (top = pop ());
132
131
push (top );
133
132
}
@@ -137,11 +136,6 @@ void duplicateTop(void)
137
136
{
138
137
double top1 , top2 ;
139
138
140
- if (sp < 2 ) {
141
- if (sp == 1 )
142
- printf ("error: 1 element in stack\n" );
143
- return ;
144
- }
145
139
top1 = pop ();
146
140
top2 = pop ();
147
141
push (top1 );
@@ -151,8 +145,7 @@ void duplicateTop(void)
151
145
/* clear: clears the entire stack */
152
146
void clearStack (void )
153
147
{
154
- while (sp > 1 )
155
- pop ();
148
+ sp = 0 ;
156
149
}
157
150
158
151
/* mathfunction: call the appropriate math function according to value of s,
@@ -181,7 +174,6 @@ int mathfunction(char s[])
181
174
* location in mem and push back to top of stack */
182
175
void storeVariable (void )
183
176
{
184
- // if (isalpha(variable) && islower(variable)) {
185
177
if (variable >= 'a' && variable <= 'z' ) {
186
178
pop ();
187
179
push (mem [variable - 'a' ] = pop ());
@@ -222,7 +214,6 @@ int main(void)
222
214
push (printed );
223
215
} else if (!strcmp (s , "mc" )) {
224
216
clearMemory ();
225
- peak = 1 ;
226
217
} else if (!mathfunction (s ))
227
218
printf ("error: unknown command %s\n" , s );
228
219
break ;
@@ -250,13 +241,13 @@ int main(void)
250
241
else
251
242
printf ("error: zero divisor\n" );
252
243
break ;
253
- case '! ' :
254
- peak = 1 ;
244
+ case '? ' :
245
+ printTOP () ;
255
246
break ;
256
- case '# ' :
247
+ case 'd ' :
257
248
duplicateTop ();
258
249
break ;
259
- case '& ' :
250
+ case 's ' :
260
251
swapTopTwo ();
261
252
break ;
262
253
case '~' :
@@ -266,11 +257,7 @@ int main(void)
266
257
storeVariable ();
267
258
break ;
268
259
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 ());
274
261
break ;
275
262
default :
276
263
if (islower (type ))
0 commit comments