2 * Exercise 4-6. Add commands for handling variables. (It's easy to provide
3 * twenty-six variables with single-letter names.) Add a variable for the most
4 * recently printed value.
8 #include <stdlib.h> /* for atof() */
9 #include <math.h> /* for math functions */
10 #include <string.h> /* for strcmp() */
11 #include <ctype.h> /* for tolower() */
13 #define MAXOP 100 /* max size of operand or operator */
14 #define NUMBER '0' /* signal that a number was found */
15 #define IDENTIFIER '1' /* signal that an identifier was found */
16 #define VARIABLE '2' /* signal that a variable was found */
17 #define ASSIGNMENT '3' /* signal that a variable was found */
18 #define LETTERS 26 /* number of letters in alphabet */
23 double printtop(void);
27 int assign(int, double);
28 double eval(int index);
30 /* reverse Polish calculator */
36 while ((type = getop(s)) != EOF) {
42 if (strcmp(s, "sin") == 0)
44 else if (strcmp(s, "cos") == 0)
46 else if (strcmp(s, "exp") == 0)
50 index = tolower(s[0]) - 'a';
51 if (0 <= index && index < LETTERS) {
54 printf("error: variable must be single letter\n");
58 index = tolower(s[0]) - 'a';
60 if (!assign(index, val)) {
61 printf("error: assignment failed, variable needs to be single letter\n");
79 printf("error: zero divisor\n");
84 push((int) pop() % (int) op2);
86 printf("error: zero divisor\n");
89 printf("\t%.8g\n", printtop());
93 printf("error: can't duplicate\n");
97 printf("error: swap failed, stack needs two numbers\n");
103 printf("\t%.8g\n", pop());
106 printf("error: unknown command %s\n", s);
112 #define MAXVAL 100 /* maximum depth of val stack */
114 int sp = 0; /* next free stack position */
115 double val[MAXVAL]; /* value stack */
116 double var[LETTERS]; /* variable stack */
118 /* push: push f onto value stack */
119 void push (double f) {
123 printf ("error: stack full, can't push %g\n", f);
126 /* pop: pop and return top value from stack */
131 printf("error: stack empty\n");
136 /* printtop: print the top value of the stack without popping it */
137 double printtop(void) {
141 printf("error: stack empty\n");
146 /* duplicate: duplicate the top value of the stack
147 * return 1 if successful, 0 on failure */
148 int duplicate(void) {
151 } else if (sp >= MAXVAL) {
160 /* swap: swap the top two values of the stack
161 * return 1 if successful, 0 on failure */
164 int temp = val[sp-1];
165 val[sp-1] = val[sp-2];
172 /* clear: clear the stack */
177 /* assign: assign a double to a variable, letters represented by int from 0-25
178 * return 1 on success, 0 on failure */
179 int assign(int index, double value) {
180 if (0 <= index && index < LETTERS) {
187 /* eval: evaluate variable, letters represented by int from 0-25 */
188 double eval(int index) {
189 if (0 <= index && index < LETTERS) {
200 /* getop: get next operator or numeric operand */
201 int getop(char s[]) {
203 while ((s[0] = c = getch()) == ' ' || c == '\t')
209 while (isalpha(s[++i] = c = getch()))
212 while ((s[++i] = c = getch()) != ' ' && c != '\t' && c != '\n')
219 } else if (s[1] == '\0') {
225 /* negative number or minus */
227 s[++i] = c = getch();
228 if (!isdigit(c) && c != '.') {
233 if (!isdigit(c) && c != '.')
234 return c; /* not a number */
235 if (isdigit(c)) /* collect integer part */
236 while (isdigit(s[++i] = c = getch()))
238 if (c == '.') /* collect fraction part */
239 while (isdigit(s[++i] = c = getch()))
249 char buf[BUFSIZE]; /* buffer for ungetch */
252 /* get a (possibly pushed back) character */
254 return (bufp > 0) ? buf[--bufp] : getchar();
257 /* push character back on input */
258 void ungetch(int c) {
260 printf("ungetch: too many characters\n");