2 * Exercise 4-11. Modify getop so that it doesn't need to use ungetch. Hint:
3 * use an internal static variable.
7 #include <stdlib.h> /* for atof() */
8 #include <math.h> /* for math functions */
9 #include <string.h> /* for strcmp() */
10 #include <ctype.h> /* for tolower() */
12 #define MAXOP 100 /* max size of operand or operator */
13 #define NUMBER '0' /* signal that a number was found */
14 #define IDENTIFIER '1' /* signal that an identifier was found */
15 #define VARIABLE '2' /* signal that a variable was found */
16 #define ASSIGNMENT '3' /* signal that a variable was found */
17 #define LETTERS 26 /* number of letters in alphabet */
22 double printtop(void);
26 int assign(int, double);
27 double eval(int index);
29 /* reverse Polish calculator */
35 while ((type = getop(s)) != EOF) {
41 if (strcmp(s, "sin") == 0)
43 else if (strcmp(s, "cos") == 0)
45 else if (strcmp(s, "exp") == 0)
49 index = tolower(s[0]) - 'a';
50 if (0 <= index && index < LETTERS) {
53 printf("error: variable must be single letter\n");
57 index = tolower(s[0]) - 'a';
59 if (!assign(index, val)) {
60 printf("error: assignment failed, variable needs to be single letter\n");
78 printf("error: zero divisor\n");
83 push((int) pop() % (int) op2);
85 printf("error: zero divisor\n");
88 printf("\t%.8g\n", printtop());
92 printf("error: can't duplicate\n");
96 printf("error: swap failed, stack needs two numbers\n");
102 printf("\t%.8g\n", pop());
105 printf("error: unknown command %s\n", s);
111 #define MAXVAL 100 /* maximum depth of val stack */
113 int sp = 0; /* next free stack position */
114 double val[MAXVAL]; /* value stack */
115 double var[LETTERS]; /* variable stack */
117 /* push: push f onto value stack */
118 void push (double f) {
122 printf ("error: stack full, can't push %g\n", f);
125 /* pop: pop and return top value from stack */
130 printf("error: stack empty\n");
135 /* printtop: print the top value of the stack without popping it */
136 double printtop(void) {
140 printf("error: stack empty\n");
145 /* duplicate: duplicate the top value of the stack
146 * return 1 if successful, 0 on failure */
147 int duplicate(void) {
150 } else if (sp >= MAXVAL) {
159 /* swap: swap the top two values of the stack
160 * return 1 if successful, 0 on failure */
163 int temp = val[sp-1];
164 val[sp-1] = val[sp-2];
171 /* clear: clear the stack */
176 /* assign: assign a double to a variable, letters represented by int from 0-25
177 * return 1 on success, 0 on failure */
178 int assign(int index, double value) {
179 if (0 <= index && index < LETTERS) {
186 /* eval: evaluate variable, letters represented by int from 0-25 */
187 double eval(int index) {
188 if (0 <= index && index < LETTERS) {
199 /* getop: get next operator or numeric operand */
200 int getop(char s[]) {
204 while (c == ' ' || c == '\t') {
211 while ((s[++i] = c = getch()) != ' ' && c != '\t' && c != '\n')
216 } else if (s[1] == '\0') {
222 /* negative number or minus */
224 s[++i] = c = getch();
225 if (!isdigit(c) && c != '.') {
229 if (!isdigit(c) && c != '.') {
232 return op; /* not a number */
234 if (isdigit(c)) /* collect integer part */
235 while (isdigit(s[++i] = c = getch()))
237 if (c == '.') /* collect fraction part */
238 while (isdigit(s[++i] = c = getch()))
246 char buf[BUFSIZE]; /* buffer for ungetch */
249 /* get a (possibly pushed back) character */
251 return (bufp > 0) ? buf[--bufp] : getchar();
254 /* push character back on input */
255 void ungetch(int c) {
257 printf("ungetch: too many characters\n");