This repository has been archived by the owner on Jun 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
expr.c
305 lines (267 loc) · 7.68 KB
/
expr.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
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h> /* for atof() */
#define MAXOP 100 /* max size of operand or operator */
#define MAXVARS 26 /* number of variables */
#define MAXLINE 1000 /* max line length */
#define NUMBER 'N' /* signal that a number was found */
#define FUNCTION 'F' /* signal that a number was found */
#define VARIABLE 'V' /* signal that a variable was found */
void callFunc(char funcName[]);
int getop(char[]);
void duplicate(void);
void swap(void);
void printTopOfStack(void);
void push(double d);
void pushv(char f[]);
double pop(void);
char* popv(void);
int _getline(char line[], int maxline);
/* must compile with 'gcc calculator.c -lm -o a.out' */
char line[MAXLINE];
int lp;
double vars[MAXVARS];
/* reverse Polish calculator */
int main(int argc, char *argv[]) {
int i, type;
double op2;
char s[MAXOP];
if (argc <= 1) {
printf("error: need more than 1 argument\n");
return 1;
}
lp = 0;
while(--argc > 0) {
char temp[MAXLINE];
sprintf(temp, "%s%s", *++argv, (argc > 1) ? " " : "");
strcat(line, temp);
}
strcat(line, "\n");
/* We set all our variables to zero as a default value to
* avoid any issues when accessing variables that are not
* explicitly set. */
printf("all variables are set to zero by default\n");
for (i = 0; i < MAXVARS; ++i)
vars[i] = 0;
while ((type = getop(s)) != '\0') {
switch (type) {
case NUMBER:
pushv(s);
break;
case FUNCTION:
callFunc(s);
printTopOfStack();
break;
case VARIABLE:
pushv(s);
break;
case 'q':
return 0;
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 division\n");
break;
case '%':
op2 = pop();
if (op2 != 0.0) {
int r = fmod(pop(), op2);
push((r * op2 < 0) ? r + op2 : r);
} else
printf("error: cannot mod by zero");
break;
case '\n':
printTopOfStack();
pop();
break;
default:
printf("error: unknown command type: \"%c\", inpt: \"%s\"\n",
type, s);
break;
}
}
return 0;
}
void callFunc(char funcName[]) {
double op2;
char vop2[MAXOP];
if (strcmp(funcName, "sin") == 0)
push(sin(pop()));
else if (strcmp(funcName, "cos") == 0)
push(cos(pop()));
else if (strcmp(funcName, "tan") == 0)
push(tan(pop()));
else if (strcmp(funcName, "exp") == 0)
push(exp(pop()));
else if (strcmp(funcName, "log") == 0)
push(log(pop()));
else if (strcmp(funcName, "pow") == 0) {
op2 = pop();
push(pow(pop(), op2));
}
else if (strcmp(funcName, "sqrt") == 0)
push(sqrt(pop()));
else if (strcmp(funcName, "floor") == 0)
push(floor(pop()));
else if (strcmp(funcName, "ceil") == 0)
push(ceil(pop()));
else if (strcmp(funcName, "abs") == 0)
push(fabs(pop()));
else if (strcmp(funcName, "top") == 0)
; /* this is always called after function calls */
else if (strcmp(funcName, "swap") == 0)
swap();
else if (strcmp(funcName, "duplicate") == 0)
duplicate();
else if (strcmp(funcName, "set") == 0) {
/* We pop both our operands, validate their types and
* set the corresponding index for our variable in the
* vars array. */
strcpy(vop2, popv());
op2 = pop();
if (!isalpha(vop2[0])) {
printf("error: second argument must be variable\n");
} else {
int index = vop2[0] - 'a';
vars[index] = op2;
printf("set var \"%c\" to \"%g\", pushing \"%c\" onto stack\n",
vop2[0], op2, vop2[0]);
pushv(vop2);
}
} else
printf("error: unknown function %s\n", funcName);
}
#define MAXVAL 100 /* maximum depth of val stack */
int sp = 0; /* next free stack position */
char val[MAXOP][MAXVAL]; /* value stack */
/* print: print top two elements of stack */
void printTopOfStack(void) {
if (sp < 1) { /* need minimum of two elements */
printf("not enough elements\n");
return;
}
printf("\t%s\n", val[sp - 1]);
}
/* swap: swap top two stack elements */
void swap(void) {
if (sp < 2) { /* need minimum of two elements */
printf("not enough elements\n");
return;
}
char temp[MAXOP];
strcpy(temp, val[sp - 1]);
strcpy(val[sp - 1], val[sp - 2]);
strcpy(val[sp - 2], temp);
printf("swapped\n\n");
}
/* duplicate: duplicate the stack */
void duplicate(void) {
int j, t = sp;
for (j = 0; j < t; j++)
pushv(val[j]);
printf("duplicated\n\n");
}
/* push: push number d onto value stack */
void push(double d) {
char temp[MAXOP];
if (sp < MAXVAL) {
sprintf(temp, "%g", d);
pushv(temp);
} else
printf("error: stack full, can't push %g\n", d);
}
/* pushv: push variable or number f onto value stack */
void pushv(char f[]) {
if (sp < MAXVAL)
strcpy(val[sp++], f);
else
printf("error: stack full, can't push %s\n", f);
}
/* pop: pop and return top value from stack as number */
double pop(void) {
char r[MAXOP];
if (sp > 0) {
strcpy(r, val[--sp]);
/* If we pop a variable, find its numeric value. */
if (isalpha(r[0]))
return vars[r[0] - 'a'];
else
return atof(r);
} else {
printf("error: stack empty\n");
return 0.0;
}
}
/* popv: pop and return top value from stack as number or variable */
char* popv(void) {
if (sp > 0)
return val[--sp];
else {
printf("error: stack empty\n");
return "0.0";
}
}
/* getop: get next operator or numeric operand */
int getop(char s[]) {
int i, c;
i = 0;
/* skip any whitespace, keep last character
* (since it will not be whitespace) for
* later processing */
while ((s[i] = c = line[lp++]) == ' ' || c == '\t')
;
/* if it was a letter, process as func name. */
if (isalpha(c)) {
int temp = line[lp];
/* if not followed by another letter then it
* is a variable name */
if (!isalpha(temp)) {
s[++i] = '\0';
return VARIABLE;
}
while (isalpha(s[++i] = c = line[lp++]))
;
s[i] = '\0';
return FUNCTION;
}
/* if it was a '-' then check if it is a minus operator
* or a negative number */
if(c == '-') {
int temp = line[lp];
/* it is a operator */
if (!isdigit(temp)) {
return c;
}
c = temp;
s[++i] = c;
}
/* if it wasn't a func name or negative symbol or digit
* then return it */
if (!isdigit(c) && c != '.')
return c;
/* collect integer part */
if (isdigit(c))
while (isdigit(s[++i] = c = line[lp++]))
;
/* collect fraction part */
if (c == '.')
while (isdigit(s[++i] = c = line[lp++]))
;
s[i] = '\0';
lp--;
return NUMBER;
}