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
/
calculator.c
239 lines (210 loc) · 6.52 KB
/
calculator.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
#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[]);
void duplicate(void);
void swap(void);
void printTopOfStack(void);
void push(double d);
void pushv(char f[]);
double pop(void);
char* popv(void);
/* must compile with 'gcc calculator.c -lm -o a.out' */
double vars[MAXVARS];
/* reverse Polish calculator */
int main() {
/* used for variables */
int i;
/* used by calculations */
double op2;
/* used by scanf */
double a = 0;
char s[MAXOP], c, e = '\0';
/* 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 (scanf("%s%c", s, &e) == 2) {
if (sscanf(s, " %lf", &a) == 1) { /* is it a number */
/* push the number onto the stack */
push(a);
} else if (sscanf(s, " %s", s) == 1) { /* is it a string */
/* if it has a length > 1, then consider it a function */
if (strlen(s) > 1) {
callFunc(s);
printTopOfStack();
continue;
}
c = s[0];
/* If c is a letter, then treat it like a variable */
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
pushv(s);
continue;
}
/* Else consider it a operator. */
switch (c) {
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;
default:
printf("error: unknown command type: \"%c\", inpt: \"%s\"\n",
c, s);
break;
}
/* Report result of the operation */
printTopOfStack();
}
}
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";
}
}