2 5dc21454 2023-07-14 jrmu * Exercise 4-11. Modify getop so that it doesn't need to use ungetch. Hint:
3 5dc21454 2023-07-14 jrmu * use an internal static variable.
6 5dc21454 2023-07-14 jrmu #include <stdio.h>
7 5dc21454 2023-07-14 jrmu #include <stdlib.h> /* for atof() */
8 5dc21454 2023-07-14 jrmu #include <math.h> /* for math functions */
9 5dc21454 2023-07-14 jrmu #include <string.h> /* for strcmp() */
10 5dc21454 2023-07-14 jrmu #include <ctype.h> /* for tolower() */
12 5dc21454 2023-07-14 jrmu #define MAXOP 100 /* max size of operand or operator */
13 5dc21454 2023-07-14 jrmu #define NUMBER '0' /* signal that a number was found */
14 5dc21454 2023-07-14 jrmu #define IDENTIFIER '1' /* signal that an identifier was found */
15 5dc21454 2023-07-14 jrmu #define VARIABLE '2' /* signal that a variable was found */
16 5dc21454 2023-07-14 jrmu #define ASSIGNMENT '3' /* signal that a variable was found */
17 5dc21454 2023-07-14 jrmu #define LETTERS 26 /* number of letters in alphabet */
19 5dc21454 2023-07-14 jrmu int getop(char []);
20 5dc21454 2023-07-14 jrmu void push(double);
21 5dc21454 2023-07-14 jrmu double pop(void);
22 5dc21454 2023-07-14 jrmu double printtop(void);
23 5dc21454 2023-07-14 jrmu int duplicate(void);
24 5dc21454 2023-07-14 jrmu int swap(void);
25 5dc21454 2023-07-14 jrmu void clear(void);
26 5dc21454 2023-07-14 jrmu int assign(int, double);
27 5dc21454 2023-07-14 jrmu double eval(int index);
29 5dc21454 2023-07-14 jrmu /* reverse Polish calculator */
30 5dc21454 2023-07-14 jrmu int main() {
32 5dc21454 2023-07-14 jrmu double op2, val;
33 5dc21454 2023-07-14 jrmu char s[MAXOP];
35 5dc21454 2023-07-14 jrmu while ((type = getop(s)) != EOF) {
36 5dc21454 2023-07-14 jrmu switch (type) {
37 5dc21454 2023-07-14 jrmu case NUMBER:
38 5dc21454 2023-07-14 jrmu push(atof(s));
40 5dc21454 2023-07-14 jrmu case IDENTIFIER:
41 5dc21454 2023-07-14 jrmu if (strcmp(s, "sin") == 0)
42 5dc21454 2023-07-14 jrmu push(sin(pop()));
43 5dc21454 2023-07-14 jrmu else if (strcmp(s, "cos") == 0)
44 5dc21454 2023-07-14 jrmu push(cos(pop()));
45 5dc21454 2023-07-14 jrmu else if (strcmp(s, "exp") == 0)
46 5dc21454 2023-07-14 jrmu push(exp(pop()));
48 5dc21454 2023-07-14 jrmu case VARIABLE:
49 5dc21454 2023-07-14 jrmu index = tolower(s[0]) - 'a';
50 5dc21454 2023-07-14 jrmu if (0 <= index && index < LETTERS) {
51 5dc21454 2023-07-14 jrmu push(eval(index));
53 5dc21454 2023-07-14 jrmu printf("error: variable must be single letter\n");
56 5dc21454 2023-07-14 jrmu case ASSIGNMENT:
57 5dc21454 2023-07-14 jrmu index = tolower(s[0]) - 'a';
58 5dc21454 2023-07-14 jrmu val = atof(s+2);
59 5dc21454 2023-07-14 jrmu if (!assign(index, val)) {
60 5dc21454 2023-07-14 jrmu printf("error: assignment failed, variable needs to be single letter\n");
64 5dc21454 2023-07-14 jrmu push(pop() + pop());
67 5dc21454 2023-07-14 jrmu push(pop() * pop());
70 5dc21454 2023-07-14 jrmu op2 = pop();
71 5dc21454 2023-07-14 jrmu push(pop() - op2);
74 5dc21454 2023-07-14 jrmu op2 = pop();
75 5dc21454 2023-07-14 jrmu if (op2 != 0.0)
76 5dc21454 2023-07-14 jrmu push(pop() / op2);
78 5dc21454 2023-07-14 jrmu printf("error: zero divisor\n");
81 5dc21454 2023-07-14 jrmu op2 = pop();
82 5dc21454 2023-07-14 jrmu if (op2 != 0.0)
83 5dc21454 2023-07-14 jrmu push((int) pop() % (int) op2);
85 5dc21454 2023-07-14 jrmu printf("error: zero divisor\n");
88 5dc21454 2023-07-14 jrmu printf("\t%.8g\n", printtop());
91 5dc21454 2023-07-14 jrmu if (duplicate() == 0)
92 5dc21454 2023-07-14 jrmu printf("error: can't duplicate\n");
95 5dc21454 2023-07-14 jrmu if (swap() == 0)
96 5dc21454 2023-07-14 jrmu printf("error: swap failed, stack needs two numbers\n");
102 5dc21454 2023-07-14 jrmu printf("\t%.8g\n", pop());
105 5dc21454 2023-07-14 jrmu printf("error: unknown command %s\n", s);
111 5dc21454 2023-07-14 jrmu #define MAXVAL 100 /* maximum depth of val stack */
113 5dc21454 2023-07-14 jrmu int sp = 0; /* next free stack position */
114 5dc21454 2023-07-14 jrmu double val[MAXVAL]; /* value stack */
115 5dc21454 2023-07-14 jrmu double var[LETTERS]; /* variable stack */
117 5dc21454 2023-07-14 jrmu /* push: push f onto value stack */
118 5dc21454 2023-07-14 jrmu void push (double f) {
119 5dc21454 2023-07-14 jrmu if (sp < MAXVAL)
120 5dc21454 2023-07-14 jrmu val[sp++] = f;
122 5dc21454 2023-07-14 jrmu printf ("error: stack full, can't push %g\n", f);
125 5dc21454 2023-07-14 jrmu /* pop: pop and return top value from stack */
126 5dc21454 2023-07-14 jrmu double pop(void) {
127 5dc21454 2023-07-14 jrmu if (sp > 0)
128 5dc21454 2023-07-14 jrmu return val[--sp];
130 5dc21454 2023-07-14 jrmu printf("error: stack empty\n");
131 5dc21454 2023-07-14 jrmu return 0.0;
135 5dc21454 2023-07-14 jrmu /* printtop: print the top value of the stack without popping it */
136 5dc21454 2023-07-14 jrmu double printtop(void) {
137 5dc21454 2023-07-14 jrmu if (sp > 0)
138 5dc21454 2023-07-14 jrmu return val[sp-1];
140 5dc21454 2023-07-14 jrmu printf("error: stack empty\n");
141 5dc21454 2023-07-14 jrmu return 0.0;
145 5dc21454 2023-07-14 jrmu /* duplicate: duplicate the top value of the stack
146 5dc21454 2023-07-14 jrmu * return 1 if successful, 0 on failure */
147 5dc21454 2023-07-14 jrmu int duplicate(void) {
148 5dc21454 2023-07-14 jrmu if (sp <= 0) {
150 5dc21454 2023-07-14 jrmu } else if (sp >= MAXVAL) {
153 5dc21454 2023-07-14 jrmu val[sp] = val[sp-1];
159 5dc21454 2023-07-14 jrmu /* swap: swap the top two values of the stack
160 5dc21454 2023-07-14 jrmu * return 1 if successful, 0 on failure */
161 5dc21454 2023-07-14 jrmu int swap(void) {
162 5dc21454 2023-07-14 jrmu if (sp > 1) {
163 5dc21454 2023-07-14 jrmu int temp = val[sp-1];
164 5dc21454 2023-07-14 jrmu val[sp-1] = val[sp-2];
165 5dc21454 2023-07-14 jrmu val[sp-2] = temp;
171 5dc21454 2023-07-14 jrmu /* clear: clear the stack */
172 5dc21454 2023-07-14 jrmu void clear(void) {
176 5dc21454 2023-07-14 jrmu /* assign: assign a double to a variable, letters represented by int from 0-25
177 5dc21454 2023-07-14 jrmu * return 1 on success, 0 on failure */
178 5dc21454 2023-07-14 jrmu int assign(int index, double value) {
179 5dc21454 2023-07-14 jrmu if (0 <= index && index < LETTERS) {
180 5dc21454 2023-07-14 jrmu var[index] = value;
186 5dc21454 2023-07-14 jrmu /* eval: evaluate variable, letters represented by int from 0-25 */
187 5dc21454 2023-07-14 jrmu double eval(int index) {
188 5dc21454 2023-07-14 jrmu if (0 <= index && index < LETTERS) {
189 5dc21454 2023-07-14 jrmu return var[index];
191 5dc21454 2023-07-14 jrmu return 0.0;
194 5dc21454 2023-07-14 jrmu #include <ctype.h>
196 5dc21454 2023-07-14 jrmu int getch(void);
197 5dc21454 2023-07-14 jrmu void ungetch(int);
199 5dc21454 2023-07-14 jrmu /* getop: get next operator or numeric operand */
200 5dc21454 2023-07-14 jrmu int getop(char s[]) {
202 5dc21454 2023-07-14 jrmu static int c = ' ';
204 5dc21454 2023-07-14 jrmu while (c == ' ' || c == '\t') {
205 5dc21454 2023-07-14 jrmu c = getchar();
208 5dc21454 2023-07-14 jrmu s[1] = '\0';
210 5dc21454 2023-07-14 jrmu if (isalpha(c)) {
211 5dc21454 2023-07-14 jrmu while ((s[++i] = c = getch()) != ' ' && c != '\t' && c != '\n')
213 5dc21454 2023-07-14 jrmu s[i] = '\0';
214 5dc21454 2023-07-14 jrmu if (s[1] == '=') {
215 5dc21454 2023-07-14 jrmu return ASSIGNMENT;
216 5dc21454 2023-07-14 jrmu } else if (s[1] == '\0') {
217 5dc21454 2023-07-14 jrmu return VARIABLE;
219 5dc21454 2023-07-14 jrmu return IDENTIFIER;
222 5dc21454 2023-07-14 jrmu /* negative number or minus */
223 5dc21454 2023-07-14 jrmu if (c == '-') {
224 5dc21454 2023-07-14 jrmu s[++i] = c = getch();
225 5dc21454 2023-07-14 jrmu if (!isdigit(c) && c != '.') {
226 5dc21454 2023-07-14 jrmu return '-';
229 5dc21454 2023-07-14 jrmu if (!isdigit(c) && c != '.') {
232 5dc21454 2023-07-14 jrmu return op; /* not a number */
234 5dc21454 2023-07-14 jrmu if (isdigit(c)) /* collect integer part */
235 5dc21454 2023-07-14 jrmu while (isdigit(s[++i] = c = getch()))
237 5dc21454 2023-07-14 jrmu if (c == '.') /* collect fraction part */
238 5dc21454 2023-07-14 jrmu while (isdigit(s[++i] = c = getch()))
240 5dc21454 2023-07-14 jrmu s[i] = '\0';
241 5dc21454 2023-07-14 jrmu return NUMBER;
244 5dc21454 2023-07-14 jrmu #define BUFSIZE 100
246 5dc21454 2023-07-14 jrmu char buf[BUFSIZE]; /* buffer for ungetch */
247 5dc21454 2023-07-14 jrmu int bufp = 0;
249 5dc21454 2023-07-14 jrmu /* get a (possibly pushed back) character */
250 5dc21454 2023-07-14 jrmu int getch(void) {
251 5dc21454 2023-07-14 jrmu return (bufp > 0) ? buf[--bufp] : getchar();
254 5dc21454 2023-07-14 jrmu /* push character back on input */
255 5dc21454 2023-07-14 jrmu void ungetch(int c) {
256 5dc21454 2023-07-14 jrmu if (bufp >= BUFSIZE)
257 5dc21454 2023-07-14 jrmu printf("ungetch: too many characters\n");
259 5dc21454 2023-07-14 jrmu buf[bufp++] = c;