-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse-polish.c
348 lines (309 loc) · 7.5 KB
/
reverse-polish.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
#include <stdio.h>
#include <stdlib.h> /* for atof() */
#include <math.h>
#include <string.h>
#include <stdbool.h>
#define MAXLINE 1000 /* max size of line */
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
#define GET_VARIABLE '$' /* signal that a get var was found */
#define COMMAND '@' /* signal that a command was found */
int my_getline(char s[], int lim);
int getop(char line[], int *start_index, char s[]);
void push(double);
double pop(void);
double peek(void);
void dupe(void);
void clear(void);
void swap(void);
void dump(void);
void handle_library_function(char s[]);
bool is_variable_name(char c);
void set_last_variable(float val);
void set_variable(char variable_name, float val);
float get_variable(char variable_name);
/* reverse Polish calculator */
int main()
{
int type;
double op2;
int line_length;
int line_index;
char line[MAXLINE];
char s[MAXOP];
bool do_calculation;
while((line_length = my_getline(line, MAXLINE)) > 0) {
line_index = 0;
while ((type = getop(line, &line_index, s)) != EOF
&& line_index <= line_length) {
/* printf("type: %c\n", type); */
/* printf("string: %s\n", s); */
if (type != '\n') {
do_calculation = true;
}
switch (type) {
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if (op2 != 0.0) {
push(pop() / op2);
} else {
printf("error: zero divisor\n");
do_calculation = false;
}
break;
case '%':
op2 = pop();
if (op2 != 0.0) {
push((int)pop() % (int)op2);
} else {
printf("error: zero divisor\n");
do_calculation = false;
}
break;
case '\n':
if (do_calculation) {
printf("------------------\n");
printf("\t%f\n", peek());
printf("\n");
set_last_variable(peek());
}
break;
case 'p':
printf("%f\n", peek());
break;
case 'd':
dupe();
printf("%f\n", peek());
break;
case 'c':
clear();
break;
case 's':
swap();
printf("%f\n", peek());
break;
case 'z':
dump();
do_calculation = false;
break;
case COMMAND:
//TODO: use a const return value from getop instead of hardcoding at sign?
handle_library_function(s);
break;
case GET_VARIABLE:
push(get_variable(s[1]));
break;
default:
do_calculation = false;
if (is_variable_name(type)) {
set_variable(type, pop());
} else {
printf("error: unknown command %s\n", s);
}
break;
}
}
}
/* printf("final type int: %d\n", type); */
/* printf("final type char: %c\n", type); */
return 0;
}
#define MAXVAL 100 /* maximum depth of val stack */
int sp = 0; /* next free stack position */
double val[MAXVAL]; /* value stack */
/* push: push f onto value stack */
void push(double f)
{
if (sp < MAXVAL) {
val[sp++] = f;
} else {
printf("error: stack full, can't push %g\n", f);
}
}
/* pop: pop and return top value from stack */
double pop(void)
{
if (sp > 0) {
return val[--sp];
} else {
printf("error: stack empty\n");
return 0.0;
}
}
/* peek: peek at top value from stack, without popping */
double peek(void)
{
if (sp > 0) {
return val[sp - 1];
} else {
/* don't show an error, we sometimes peek at an empty stack */
/* printf("error: stack empty\n"); */
return 0.0;
}
}
/* dupe: dupe the top value from stack */
void dupe(void)
{
if (sp > 0) {
sp++;
val[sp - 1] = val[sp - 2];
} else {
printf("error: stack empty\n");
}
}
/* clear: clear the stack */
void clear(void)
{
sp = 0;
}
/* swap: swap the top two items on the stack */
void swap(void)
{
float temp = val[sp - 1];
val[sp - 1] = val[sp - 2];
val[sp - 2] = temp;
}
/* dump: dump the top two items on the stack */
void dump(void)
{
printf("<<<<dump>>>\n");
for(int i = 0; i < sp; i++) {
printf("(%3d) %f\n", i, val[i]);
}
printf("<<<<\\dump>>>\n");
}
// needed for isdigit
#include <ctype.h>
/* getop: get next operator or numeric operand */
int getop(char line[], int *start_index, char s[])
{
int c;
int result;
int i = 0;
/* printf("about to look for whitespace %d\n", sp); */
while((s[0] = c = line[(*start_index)++]) == ' ' || c == '\t') {
/* printf("ping\n"); */
;
}
s[1] = '\0';
if (c == '\n' || c == EOF) {
return c;
} else if (isdigit(c) || c == '.' || c == '-') {
result = NUMBER;
} else {
result = c;
}
while((s[++i] = c = line[(*start_index)++]) != ' '
&& c != '\t'
&& c != '\n'
&& c != '\0'
&& c != EOF) {
;
}
s[i] = '\0';
/* printf("c now int: %d\n", c); */
/* printf("c now char: %c\n", c); */
if (s[0] != c) {
(*start_index)--;
}
/* if the first digit is a dash and there are no other chars,
* it's an operator and not a number. */
if (result == NUMBER && s[0] == '-' && i == 1) {
// what we have here is a subtraction operator
result = s[0];
}
/* printf("string: <<--%s-->>\n", s); */
/* printf("result int: %d\n", result); */
/* printf("result char: %c\n", result); */
return result;
}
void handle_library_function(char s[])
{
float op2;
if (strcmp(s, "@sin") == 0) {
push(sin(pop()));
} else if (strcmp(s, "@exp") == 0) {
push(exp(pop()));
} else if (strcmp(s, "@pow") == 0) {
op2 = pop();
push(pow(pop(), op2));
} else {
printf("error: unknown library command %s\n", s);
}
}
// create a global to hold our variable values, initialized to zeros
#define NUMBER_OF_VARIABLES 26
#define VARIABLE_NOT_FOUND -1
#define LAST_VARIABLE_CHAR '!'
float variables[NUMBER_OF_VARIABLES] = {0};
float last_variable = 0;
bool is_variable_name(char c)
{
return (c == LAST_VARIABLE_CHAR
|| (c >= 'A' && c <= 'Z'));
}
int get_letter_variable_index(char variable_name)
{
if (variable_name >= 0 || variable_name < NUMBER_OF_VARIABLES) {
return variable_name - 'A';
} else {
printf("Unknown variable %c\n", variable_name);
return VARIABLE_NOT_FOUND;
}
}
void set_variable(char variable_name, float val)
{
int variable_index;
if (variable_name == LAST_VARIABLE_CHAR) {
printf("$! variable cannot be set directly.\n");
return;
}
if ((variable_index = get_letter_variable_index(variable_name))
!= VARIABLE_NOT_FOUND) {
variables[variable_index] = val;
} else {
printf("Unknown variable %c\n", variable_name);
}
}
void set_last_variable(float val)
{
last_variable = val;
}
float get_variable(char variable_name)
{
int variable_index;
if (variable_name == LAST_VARIABLE_CHAR) {
return last_variable;
}
if ((variable_index = get_letter_variable_index(variable_name))
!= VARIABLE_NOT_FOUND) {
return variables[variable_index];
}
return 0.0;
}
/* my_getline:get line into s, return length */
int my_getline(char s[], int lim)
{
int c, i;
i=0;
while(--lim > 0 && (c=getchar()) != EOF && c != '\n') {
s[i++] = c;
}
if(c =='\n' )
s[i++] = c;
s[i]='\0';
return i;
}