-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkr_ex4-11.c
357 lines (334 loc) · 9.34 KB
/
kr_ex4-11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAXITER 10
/* based on kr_ex4-6.c, modified getop so that it uses an internal
static variable instead of ungetch() */
/* adding support for variables (lowercase alphabet letters) and
special variable '#' for holding the last printed element */
/* adding commands to print ('?') top element of the stack without
popping it, duplicate it ('>'), swap two last elements ('<') and
clear the stack ('!') */
#define MAXOP 100 // max size of operands
#define NUMBER '0' // signal number has encountered
#define MATHFUN '\007'
#define USERVARIABLE '\005'
int getop(char []);
void push(double f, int vt);
double pop(void);
void initStorage(void); // initialize stack and variables
double applymathfun(char funname[], double x);
int storeVariable(void); // store number in variable
void printLastItem();
void duplicateLastItem();
// reverse Polish calculator
int main()
{
int type;
double op2, op3;
char s[MAXOP];
int count = 0;
initStorage(); // clear stack and variables
while ((type = getop(s)) != EOF) {
switch (type) {
case NUMBER:
push(atof(s), NUMBER);
break;
case '+':
push(pop() + pop(), NUMBER);
break;
case '*':
push(pop() * pop(), NUMBER);
break;
case '-':
op2 = pop();
push(pop() - op2, NUMBER);
break;
case '/':
op2 = pop();
if (op2 != 0)
push(pop() / op2, NUMBER);
else
printf("error: zero divisor\n");
break;
case '%':
op2 = pop();
if (op2 != 0)
push((int) pop() % (int) op2, NUMBER);
else
printf("error: zero divisor\n");
break;
case '?':
printLastItem();
break;
case '>':
duplicateLastItem();
/* op2 = pop(); // !!! need to be made compatible with variables */
/* push(op2, NUMBER); */
/* push (op2, NUMBER); */
break;
case '<': // !!! need to be made compatible with variables
op2 = pop();
op3 = pop();
push(op2, NUMBER);
push(op3, NUMBER);
break;
case '!':
while (!isnan(op2 =pop()) && ++count <= MAXITER)
;
if (count == MAXITER) {
printf("\n error: cannot clear stack, max iteration reached\n");
return -1;
}
break;
case '^': // storing variable value
storeVariable();
break;
case '\n':
printLastItem();
pop();
//printf("\t%.8g\n", pop());
break;
case MATHFUN:
op2 = pop();
op3 = applymathfun(s, op2);
if (isfinite(op3))
push(op3, NUMBER);
else
printf("\tunsupported %s(%g)\n", s, op2);
break;
case USERVARIABLE:
push(NAN, s[0]); // first argument ignored and replaced with current variable value
break;
default:
printf("error: unkwown command %s\n", s);
break;
}
}
return 0;
}
double applymathfun(char funname[], double x)
{
if (strcmp(funname, "sin") == 0)
return 1;//sin(x);
else if (strcmp(funname, "cos") == 0)
return 1;//cos(x);
else if (strcmp(funname, "tan") == 0)
return 1;//tan(x);
else if (strcmp(funname, "exp") == 0)
return 1;//exp(x);
else if (strcmp(funname, "sqrt") == 0)
return 1;//sqrt(x);
else if (strcmp(funname, "abs") == 0)
return 1;//abs(x);
else if (strcmp(funname, "log") == 0)
return 1;//log(x);
else if (strcmp(funname, "log10") == 0)
return 1;//log10(x);
else // still unsupported mathematical function
return NAN;
}
#define MAXVAL 100 // maximum depth of stack
#define VARNUMBER ('z'-'a'+2) //letters plus special variable '#'
int sp = 0; // next free stack position
double val [MAXVAL]; // value stack
int valtype[MAXVAL]; // NUMBER for numbers ASCII code for variables
double variables[VARNUMBER];
double getVariable(int c);
void setVariable(int c, double x);
int isVariable(int c);
//push: push f onto value stack
void push(double f, int vt)
{
if (sp < MAXVAL){
valtype[sp] = vt;
if (vt == NUMBER)
val[sp] = f;
else
val[sp] = getVariable(vt); //storing current variable value
sp++;
}
else
printf("error: stack full, can\'t push %g\n", f);
}
// pop: pop and return top value from stack
double pop(void)
{
double x;
if (sp > 0) {
--sp;
if (valtype[sp] == NUMBER)
return val[sp];
else {
x = getVariable(valtype[sp]);
/* printf("\t retrieved variable \'%c\', current value: %g, previous value: %g\n", */
/* valtype[sp], x, val[sp]); */
return x; // get current variable value rather than stored one
}
}
else {
printf("stack empty\n");
return NAN;
}
}
void initStorage(void) {
int i;
for (i = 0; i < VARNUMBER; i++)
variables[i] = 0;
for (i = 0; i < MAXVAL; i++) {
val[i] = 0;
valtype[i] = NUMBER;
}
}
int isVariable(int c)
{
if ((c >= 'a' && c <= 'z') || c == '#')
return c;
else
return 0;
}
double getVariable(int c)
{
if (c == '#')
return variables[VARNUMBER - 1];
else if (c >= 'a' && c <= 'z')
return variables[c - 'a'];
else {
printf("unsupported variable: \'%c\'\n", c);
return NAN;
}
}
void setVariable(int c, double x)
{
if (c == '#')
variables[VARNUMBER - 1] = x;
else if (c >= 'a' && c <= 'z')
variables[c - 'a'] = x;
else {
printf("unsupported variable: \'%c\'\n", c);
}
}
int storeVariable(void)
{
double prevValue;
double newValue;
int vt;
if ((vt = isVariable(valtype[sp-1])) == 0) {
printf("\t error:last item in stack (type:\'%c\') is not a variable\n", sp-1);
return 0;
}
prevValue = pop();
newValue = pop();
setVariable(vt, newValue);
push(newValue, vt);
printf("\t changed \'%c\' from %g to %g\n", vt, prevValue, newValue);
return 1;
}
void printLastItem(void)
{
double op2;
op2 = pop();
printf("\t%.8g\n", op2);
setVariable('#', op2);
push(op2, NUMBER);
}
void duplicateLastItem(void)
{
double x;
int vt;
vt = valtype[sp-1];
if (vt == NUMBER) {
x = pop();
push(x, NUMBER);
push (x, NUMBER);
}
else if (isVariable(vt)) {
push(NAN, vt);
}
else
printf("error: tryed to duplicate invalid item type %c\n", vt);
}
#include <ctype.h>
int getch(void);
void myungetch(int c);
// getop: get next operator or numeric operand
int getop(char s[])
{
int i, c;
static int oldc;
if (oldc != '\0') {
c = oldc;
oldc = '\0';
}
else
while ((s[0] = c = getch()) == ' ' || c == '\t')
; // skipping white spaces
s[1] = '\0';
i = 0;
if (c == EOF || c == '\n')
return c; // reached end of line or file, stop here
else // otherwise read next character
c = getch();
if ( isdigit(s[0]) || (s[0] == '.' && isdigit(c)) || //we have a number
((s[0] == '+' || s[0] == '-') && ( isdigit(c) || c == '.' )) )
{ // reading a number
while (isdigit(c)) { // reading integer part
s[++i] = c;
c = getch();
}
if (c == '.') { //reading fraction part
s[++i] = c;
while (isdigit(c = getch()))
s[++i] = c;
}
s[++i] = '\0';
if (c != EOF)
oldc = c;
if (i == 2 && s[1] == '.' && (s[0] == '+' || s[0] == '-'))
{ // one sign and a decimal separator are not a number
oldc = s[1]; // we put back the decimal separator
return s[0]; // and return the sign to be reprocessed as operator
}
else
return NUMBER;
}
else if (s[0] == '#') {
s[1] = '\0';
if (c != EOF)
oldc = c;
return USERVARIABLE;
}
else if (s[0] >= 'a' && s[0] <= 'z') { // reading a word
while (c >= 'a' && c <= 'z') { // made of alphabetic characters
s[++i] = c;
c = getch();
}
s[++i] = '\0';
if (c != EOF)
oldc = c;
if (i == 1)
return USERVARIABLE;
else
return MATHFUN;
}
else { // single character operator
if (c != EOF)
oldc = c;
return s[0];
}
}
#define BUFSIZE 100
char buf[BUFSIZE]; // buffer for myungetch
int bufp = 0; // nextt free position in buf
int getch(void) // get a (possibly pushed back) char
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void myungetch(int c) // push char back on input
{
if (bufp >= BUFSIZE)
printf("myungetch: too many characters\n");
else
buf[bufp++] = c;
}