-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-11.c
230 lines (197 loc) · 3.85 KB
/
4-11.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
// 4.11 : RPN calculator + static variables
#include <math.h>
#include "header.h"
#define NUMBER '0'
#define VAR 'a'
int getop(char s[]);
void push(double f);
double pop(void);
double vars[26];
bool var_used[26];
double last = 0.0;
int main()
{
const size_t MAX_OP_SIZE = 100;
for (size_t i = 0; i < 26; i++) {
vars[i] = -1;
var_used[i] = false;
}
char s[MAX_OP_SIZE];
double op2 = 0.0;
size_t index = 0;
int type;
while ((type = getop(s)) != EOF) {
switch (type) {
case NUMBER:
push(atof(s));
break;
case VAR:
push((double) s[0] - 'a');
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
perror("dividing by zero");
break;
case '%':
op2 = pop();
if (op2 != 0.0)
push(fmod(pop(), op2));
else
perror("dividing by zero");
break;
case 's':
push(sin(pop()));
break;
case 'e':
push(exp(pop()));
break;
case 'p':
op2 = pop();
push(pow(pop(), op2));
break;
case '=':
op2 = pop();
index = (int) pop();
vars[index] = op2;
var_used[index] = true;
push(op2);
break;
case ';':
push(last);
break;
case '\n':
last = pop();
putf(last);
break;
default:
fprintf(stderr, "Unknown command '%s' of type '%d'\n", s, type);
break;
}
}
return EXIT_SUCCESS;
}
// Cannot be a constant because C constants not constants, just RO
// Memory addresses can change for constants
#define STACK_SIZE 100
double stack[STACK_SIZE];
size_t stackpos = 0;
void push(double f)
{
if (stackpos < STACK_SIZE)
stack[stackpos++] = f;
else
fprintf(stderr, "Stack full, cannot push %f\n", f);
}
double pop(void)
{
if (stackpos > 0)
return stack[--stackpos];
else {
perror("Stack empty, cannot pop");
return 0.0;
}
}
void peek(void)
{
if (stackpos > 1) {
double first = stack[stackpos - 1];
double second = stack[stackpos - 2];
printf("Top two elements: %f, %f\n", first, second);
} else
perror("The stack contains less than two elements");
}
void dup(double to[])
{
for (size_t i = 0; i < stackpos; i++)
to[i] = stack[i];
}
void swap()
{
if (stackpos > 1) {
double tmp = stack[stackpos - 1];
stack[stackpos - 1] = stack[stackpos - 2];
stack[stackpos - 2] = tmp;
} else
perror("The stack contains less than two elements");
}
void clear()
{
stackpos = 0;
}
#include <ctype.h>
int getch(void);
void ungetch(int c);
int getop(char s[])
{
int c;
while ((s[0] = c = getch()) == ' ' || c == '\t');
s[1] = '\0';
static int buf = -99;
if (islower(c)) {
if (var_used[c - 'a']) {
sprintf(s, "%f", vars[c - 'a']);
return NUMBER;
} else
return VAR;
}
if (c == ';') {
sprintf(s, "%f", last);
return NUMBER;
}
if (!isdigit(c) && c != '.' && c != '-' && c != '+')
return c;
if (c == '=')
return '=';
size_t i = 0;
if (c == '-' || c == '+') {
char next = (buf == -99) ? getch() : buf;
if (buf != -99)
buf = 99;
if (next != EOF)
buf = next;
if (!isdigit(next) && next != '.')
return c;
s[++i] = c = (buf == -99) ? getch() : buf;
if (buf != -99)
buf = 99;
}
if (isdigit(c))
while (isdigit(s[++i] = c = (buf == -99) ? getch() : buf));
if (buf != -99)
buf = 99;
if (c == '.')
while (isdigit(s[++i] = c = (buf == -99) ? getch() : buf))
if (buf != -99)
buf = 99;
s[i] = '\0';
if (c != EOF)
buf = c;
return NUMBER;
}
#define BUFSIZE 100
char buf[BUFSIZE];
size_t bufpos = 0;
int getch(void)
{
return (bufpos > 0) ? buf[--bufpos] : getchar();
}
void ungetch(int c)
{
if (bufpos >= BUFSIZE)
perror("Cannot ungetch, too many characters");
else
buf[bufpos++] = c;
}