-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex4_8.c
239 lines (215 loc) · 3.7 KB
/
ex4_8.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 <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAXSTACK 100
#define BUFSIZE 1
#define MAXOP 50
#define MAXVAR 26
#define NUMBER '0'
#define MATH 'm'
#define VAR 'v'
#define ASGN 'a'
double stack[MAXSTACK];
char buf[BUFSIZE];
double var[MAXVAR];
int buf_pos = 0;
int stack_pos = 0;
void push(double el)
{
if (stack_pos < MAXSTACK) {
stack[stack_pos++] = el;
}
else {
printf("Error : STACK FULL\n");
}
}
double pop(void)
{
if (stack_pos > 0) {
return stack[--stack_pos];
}
else {
printf("Error : STACK EMPTY\n");
return 0;
}
}
double top(void)
{
if (stack_pos > 0)
return stack[stack_pos-1];
else {
printf("Error : STACK EMPTY\n");
return 0;
}
}
void duplicate(void)
{
if (stack_pos < MAXSTACK)
stack[stack_pos++] = top();
else
printf("Error : STACK FULL\n");
}
void swap(void)
{
double temp1, temp2;
temp1 = pop();
temp2 = pop();
push(temp1);
push(temp2);
}
void clear(void)
{
stack_pos = 0;
stack[stack_pos] = '\0';
}
int getch(void)
{
return (buf_pos > 0) ? buf[--buf_pos] : getchar();
}
void ungetch(int c)
{
if (buf_pos >= BUFSIZE) {
printf("Error : BUFFER FULL\n");
}
else {
buf[buf_pos++] = c;
}
}
int getop(char s[])
{
int c, d, i;
while ((c = s[0] = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '.' && c != '-' && !isalpha(c))
return c;
i = 0;
if (c == '-') {
if ((d = getch()) == ' ')
return c;
else {
s[++i] = d;
c = d;
}
}
if (isalpha(c)) {
if (isalpha(d = getch())) {
ungetch(d);
s[++i] = getch();
s[++i] = getch();
s[++i] = '\0';
return MATH;
}
else {
d = getch();
if (d == '=') {
s[++i] = '\0';
return ASGN;
}
else {
ungetch(d);
s[++i] = '\0';
return VAR;
}
}
}
if (isdigit(c))
while (isdigit((c = s[++i] = getch())))
;
if (c == '.')
while (isdigit((c = s[++i] = getch())))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
int main()
{
char s[MAXOP];
int type, ignore_newline = 0;
double op2;
while ((type = getop(s)) != EOF) {
switch (type) {
case NUMBER:
push(atof(s));
break;
case ASGN:
op2 = pop();
var[s[0]-'a'] = op2;
ignore_newline = 1;
break;
case VAR:
push(var[s[0]-'a']);
break;
case MATH:
if (strcmp(s, "sin") == 0)
push(sin(pop()));
else if (strcmp(s, "cos") == 0)
push(cos(pop()));
else if (strcmp(s, "tan") == 0)
push(tan(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 : Invalid Math\n");
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if (op2 == 0)
printf("Error : DIVISION BY ZERO");
else
push(pop() / op2);
break;
case '%':
op2 = pop();
if (op2 == 0)
printf("Error : MODULUS WITH ZERO");
else
push(fmod(pop(), op2));
break;
case '>':
printf("%f\n", top());
ignore_newline = 1;
break;
case '<':
duplicate();
ignore_newline = 1;
break;
case '@':
swap();
ignore_newline = 1;
break;
case '!':
clear();
ignore_newline = 1;
break;
case '\n':
if (ignore_newline)
ignore_newline = 0;
else
printf("\t%.8g\n", pop());
break;
default:
printf("Error : INVALID ENTRY");
break;
}
}
return 0;
}