2
2
* Exercise 4-10. An alternative organization uses getline to read an entire
3
3
* input line; this makes getch and ungetch unnecessary. Revise the calculator
4
4
* to use this approach.
5
+ *
5
6
* Faisal Saadatmand
6
7
*/
7
8
18
19
#define MAXVAL 100 /* maximum depth of val stack */
19
20
#define BUFSIZE 100
20
21
#define MAXVAR 26
21
- #define TOP val[sp - 1] /* top element in stack */
22
22
23
23
/* functions */
24
24
int getop (char []);
@@ -40,7 +40,6 @@ double val[MAXVAL]; /* value stack */
40
40
double mem [MAXVAR ]; /* variables values */
41
41
char buf [BUFSIZE ]; /* buffer from ungetch */
42
42
int bufp ; /* next free position in buf */
43
- int peak ; /* flag: peak at top of the stack */
44
43
int variable ; /* current input variable */
45
44
double printed ; /* last printed value */
46
45
@@ -127,18 +126,18 @@ int getLine(char s[], int lim)
127
126
/* printTOP: print top of the stack without pop */
128
127
void printTOP (void )
129
128
{
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 );
133
134
}
134
135
135
136
/* duplicateTop: duplicate the top element in the stack */
136
137
void duplicateTop (void )
137
138
{
138
139
double top ;
139
140
140
- if (sp < 1 )
141
- return ;
142
141
push (top = pop ());
143
142
push (top );
144
143
}
@@ -148,11 +147,6 @@ void duplicateTop(void)
148
147
{
149
148
double top1 , top2 ;
150
149
151
- if (sp < 2 ) {
152
- if (sp == 1 )
153
- printf ("error: 1 element in stack\n" );
154
- return ;
155
- }
156
150
top1 = pop ();
157
151
top2 = pop ();
158
152
push (top1 );
@@ -162,8 +156,7 @@ void duplicateTop(void)
162
156
/* clear: clears the entire stack */
163
157
void clearStack (void )
164
158
{
165
- while (sp > 1 )
166
- pop ();
159
+ sp = 0 ;
167
160
}
168
161
169
162
/* mathfunction: call the appropriate math function according to value of s,
@@ -192,7 +185,6 @@ int mathfunction(char s[])
192
185
* location in mem and push back to top of stack */
193
186
void storeVariable (void )
194
187
{
195
- // if (isalpha(variable) && islower(variable)) {
196
188
if (variable >= 'a' && variable <= 'z' ) {
197
189
pop ();
198
190
push (mem [variable - 'a' ] = pop ());
@@ -233,7 +225,6 @@ int main(void)
233
225
push (printed );
234
226
} else if (!strcmp (s , "mc" )) {
235
227
clearMemory ();
236
- peak = 1 ;
237
228
} else if (!mathfunction (s ))
238
229
printf ("error: unknown command %s\n" , s );
239
230
break ;
@@ -261,27 +252,23 @@ int main(void)
261
252
else
262
253
printf ("error: zero divisor\n" );
263
254
break ;
264
- case '! ' :
265
- peak = 1 ;
255
+ case '? ' :
256
+ printTOP () ;
266
257
break ;
267
- case '# ' :
258
+ case 'd ' :
268
259
duplicateTop ();
269
260
break ;
270
- case '& ' :
261
+ case 's ' :
271
262
swapTopTwo ();
272
263
break ;
273
- case '~ ' :
264
+ case 'c ' :
274
265
clearStack ();
275
266
break ;
276
267
case '=' :
277
268
storeVariable ();
278
269
break ;
279
270
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 ());
285
272
break ;
286
273
default :
287
274
if (islower (type ))
0 commit comments