-
Notifications
You must be signed in to change notification settings - Fork 0
/
calulator.c
215 lines (185 loc) · 4.18 KB
/
calulator.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
/* write a calculator program that provides the operators +, -, and /. Because
* it is easier to implement, the calculator will use reverse Polish notation
* instead of infix. */
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "math.h"
#include "stdlib.h"
#define MAX 100 // max length of operators
enum TYPE {NONE, NUMBER, SIN, EXP, POW}; // flags
int getop(char s[]);
void push(double op);
double pop(void);
void clrstk(void);
int getch(void);
void ungetch(int c);
void strsqz(char s[], size_t dist);
int main(void) {
int type;
char s[MAX], varflag[26];
double op, op0, varval[26];
size_t i;
// zero varflag
for (i = 0; i < 26; i++)
varflag[i] = 0;
while ((type=getop(s)) != EOF) {
switch (type) {
case NONE:
printf("Error: wrong operators\n");
return 0;
case NUMBER:
push(atof(s));
break;
case SIN:
push(sin(pop()));
break;
case EXP:
push(exp(pop()));
break;
case POW:
op = pop();
push(pow(op, pop()));
break;
case '+':
push(pop() + pop());
break;
case '-':
op = pop();
push(pop() - op);
break;
case '*':
push(pop() * pop());
break;
case '/':
op = pop();
if (op == 0)
printf("Error: zero divisor.\n");
else
push(pop() / op);
break;
case '%':
op = pop();
if (op == 0)
printf("Error: zero divisor.\n");
else
push((int)pop() % (int)op);
break;
case '=':
op = pop();
if (!(op >= 'a' && op <= 'z')) {
printf("error: use a variable before defining\n");
return 0;
}
varval[(char)op - 'a'] = pop();
break;
case 't': // print the top element of the stack
printf("top element of the stack: %g\n", pop());
return 0;
case 'd': // duplicate the top element of the stack
op = pop();
push(op);
push(op);
break;
case 's': // swap the top 2 elements of the stack
op = pop();
op0 = pop();
push(op);
push(op0);
break;
case 'c': // clear operator stack
clrstk();
break;
case 'v':
if (strlen(s) == 1 && s[0] >= 'a' && s[0] <= 'z') {
if (varflag[s[0] - 'a'] == 0) {
varflag[s[0] - 'a'] = 1;
push(s[0]);
} else
push(varval[s[0] - 'a']);
} else {
printf("invalid variable\n");
return 0;
}
break;
case '\n':
printf("result: %g\n", pop());
return 0;
default:
printf("Error: abnormal operators.\n");
return 0;
}
}
return 0;
}
/* get operations */
int getop(char s[]) {
size_t i;
char c;
// skip blanks
for (i = 0; isblank(s[i] = c = getch());)
;
// the character after '\n' is unclear, so return in advance.
if (c == '\n')
return '\n';
// get non-blank characters
for (i++; !isblank(s[i] = c = getch()) && c!='\n'; i++)
;
// only for the situation "<some character>\n\000"
ungetch(s[i]);
s[i] = '\0';
// usage of variables: 'value v+<one lowercase> ='
// example: 2 va = 5 vb = va vb *
if (isdigit(s[0]))
return NUMBER;
else if (strcmp(s, "sin") == 0 || strcmp(s, "SIN") == 0)
return SIN;
else if (strcmp(s, "exp") == 0 || strcmp(s, "EXP") == 0)
return EXP;
else if (strcmp(s, "pow") == 0 || strcmp(s, "POW") == 0)
return POW;
else if (s[0]=='v' && strlen(s)>1) {
strsqz(s, 1);
return 'v';
} else if (strlen(s) == 1)
return s[0];
else
return NONE;
}
/* push operators */
double stack[MAX];
size_t sh;
void push(double op) {
if (sh < MAX)
stack[sh++] = op;
else
printf("No more space to store.\n");
}
/* pop operators */
double pop(void) {
if (sh > 0)
return stack[--sh];
else
printf("Stack is empty.\n");
return 0;
}
/* clear stack */
void clrstk(void) {
sh = 0;
}
/* `getch`与`ungetch`函数对实质上是带有缓存的文本流处理工具 */
int buffer[MAX];
size_t bh;
int getch(void) {
return bh > 0 ? buffer[--bh] : getchar();
}
void ungetch(int c) {
buffer[bh++] = c;
}
/* squeeze a string */
void strsqz(char s[], size_t dist) {
size_t i;
for (i = dist; s[i] != '\0'; i++)
s[i-dist] = s[i];
s[i] = '\0';
}