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
|
#define _XOPEN_SOURCE 700
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define RETURN_IF_FEWER_THAN(n) if (stack_len() < n) { fprintf(stderr, "stack too short!\n"); return; }
#define STACK_LEN 50
int stack[STACK_LEN] = {0};
int *stackp = stack - 1;
int ibase = 10;
int obase = 10;
void oprint(int a) {
char fmt[4] = {'%', 0, '\n', '\0'};
switch (obase) {
case 16:
fmt[1] = 'X';
break;
case 10:
fmt[1] = 'd';
break;
case 8:
fmt[1] = 'o';
break;
default:
fprintf(stderr, "output base %d not supported!\n", obase);
return;
}
printf(fmt, a);
}
void push(int a) {
stackp++;
if (stackp - stack >= STACK_LEN) {
fprintf(stderr, "stack overflow!\n");
stackp--;
} else {
*stackp = a;
}
}
int pop(void) {
if (stackp == stack - 1) {
fprintf(stderr, "no value on stack!\n");
return 0;
}
return *stackp--;
}
int peek(void) {
if (stackp == stack - 1) {
fprintf(stderr, "no value on stack!\n");
return 0;
}
return *stackp;
}
int stack_len(void) {
return 1 + (stackp - stack);
}
void view_top(void) {
RETURN_IF_FEWER_THAN(1);
oprint(peek());
}
void view_stack(void) {
for (int *p = stackp; p - stack >= 0; p--)
oprint(*p);
}
void duplicate(void) {
RETURN_IF_FEWER_THAN(1);
push(peek());
}
void clear(void) {
stackp = stack - 1;
}
void add(void) {
RETURN_IF_FEWER_THAN(2);
push(pop() + pop());
}
void subtract(void) {
RETURN_IF_FEWER_THAN(2);
int a = pop();
push(-a + pop());
}
void multiply(void) {
RETURN_IF_FEWER_THAN(2);
push(pop() * pop());
}
void divide(void) {
RETURN_IF_FEWER_THAN(2);
int div = pop();
if (div == 0) {
fprintf(stderr, "division by 0!\n");
pop();
push(0);
return;
}
push(pop() / div);
}
void i_base(void) {
RETURN_IF_FEWER_THAN(1);
int i = pop();
if (i > 16 || i < 1)
fprintf(stderr, "input base %d not supported!\n", i);
else
ibase = i;
}
void I_base(void) {
push(ibase);
}
void o_base(void) {
RETURN_IF_FEWER_THAN(1);
int o = pop();
if (o != 8 && o != 10 && o != 16)
fprintf(stderr, "output base %d not supported!\n", o);
else
obase = o;
}
void O_base(void) {
push(obase);
}
void mod(void) {
RETURN_IF_FEWER_THAN(2);
int a = pop();
if (a == 0) {
fprintf(stderr, "division by 0!\n");
pop();
push(0);
return;
}
push(pop() % a);
}
void power(void) {
RETURN_IF_FEWER_THAN(2);
int a = pop();
push((int)pow((double)pop(), (double)a));
}
void root(void) {
RETURN_IF_FEWER_THAN(1);
int a = pop();
if (a < 0) {
fprintf(stderr, "root of negative number!\n");
}
push((int)sqrt((double)a));
}
void drop(void) {
RETURN_IF_FEWER_THAN(1);
pop();
}
struct {
char op;
void (*f)(void);
} ops[] = {
{ 'p', view_top },
{ 'f', view_stack },
{ 'd', duplicate },
{ 'c', clear },
{ '+', add },
{ '-', subtract },
{ '*', multiply },
{ '/', divide },
{ 'i', i_base },
{ 'I', I_base },
{ 'o', o_base },
{ 'O', O_base },
{ '%', mod },
{ '^', power },
{ 'v', root },
{ 'r', drop },
};
#define NUM_OPS ((sizeof(ops)) / (sizeof(*ops)))
int main(int argc, char **argv) {
int c;
while ((c = getopt(argc, argv, ":")) != -1) {
fprintf(stderr, "usage: %s [file]\nsupported operations:\n", argv[0]);
for (size_t i = 0; i < NUM_OPS; i++)
fprintf(stderr, "%c\n", ops[i].op);
return 1;
}
argc -= optind;
argv += optind - 1;
FILE *fp = NULL;
if (argc == 0) {
fp = stdin;
} else {
fp = fopen(argv[1], "r");
if (fp == NULL) {
perror(argv[1]);
return 1;
}
}
char *buf = NULL;
size_t buflen = 0;
ssize_t n = 0;
int curnum = 0;
int numready = 0;
int neg = 1;
while ((n = getline(&buf, &buflen, fp)) != -1) {
for (ssize_t i = 0; i < n - 1; i++) {
if (buf[i] == 'q')
goto end;
if (buf[i] == '_')
neg = -1;
else if (buf[i] == '.')
fprintf(stderr, "no floating point please!\n");
else if (isdigit(buf[i])) {
curnum = (curnum * ibase) + (buf[i] - '0');
numready = 1;
} else if (buf[i] >= 'A' && buf[i] <= 'F') {
curnum = (curnum * ibase) + (buf[i] - 'A' + 10);
numready = 1;
} else {
if (numready) {
push(curnum * neg);
numready = 0;
neg = 1;
}
curnum = 0;
if (isspace(buf[i])) {
continue;
} else if (isdigit(buf[i])) {
push(buf[i] - '0');
} else {
for (size_t j = 0; j < NUM_OPS; j++) {
if (buf[i] == ops[j].op) {
ops[j].f();
break;
}
}
}
}
}
}
end:
free(buf);
if (fp != stdin)
fclose(fp);
return 0;
}
|