I took on these exercises related to the reverse polish calculator mentioned in K&R book.

And there is this exercise which is giving me a headache:

Add commands for handling variables. (It's easy to provide twenty-six variables with single-letter names.) Add a variable for the most recently printed value.

How exactly am i supposed to do this and what am i supposed to do? Does this mean enabling = operator for giving values to single letter variables? If so, how to implement that sort of design... i mean it has to work with the stack somehow, im just confused how... and how many variables could be at use in one time, all 26 or just one?

The code for the calculator is below:

(seperated into 5 source files with appropriate functions)

Code:#include "calc.h" #define MAXOP 100 int main(int argc, char *argv[]) { int type; double op2; char s[MAXOP]; while((type = getop(s)) != EOF) { switch(type) { case NUMBER: push(atof(s)); break; case WORD: Handle_word(s); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': if((op2 = pop()) != 0) push(pop() / op2); else printf("zero divisor\n"); break; case '%': if((op2 = pop()) != 0) push(fmod(pop(), op2)); else printf("zero divisor\n"); break; case '\n': printf("\t%g\n", pop()); break; default: printf("unknown command: %s\n", s); break; } } getchar(); return 0; }Code:#include "calc.h" #define MAXVAL 100 double val[MAXVAL]; int sp = 0; void push(double c) { if(sp < MAXVAL) val[sp++] = c; else printf("stack full\n"); } double pop() { if(sp > 0) return val[--sp]; else { printf("stack empty\n"); return 0.0; } } void Handle_word(char s[]) { double op1, op2; if(strcmp(s, "print") == 0) { if(sp > 0) printf("\t%g\n", val[sp-1]); else printf("stack empty, cant print\n"); } else if(strcmp(s, "dup") == 0) { op1 = pop(); push(op1); push(op1); } else if(strcmp(s, "swap") == 0) { op1 = pop(); op2 = pop(); push(op1); push(op2); } else if(strcmp(s, "clear") == 0) { sp = 0; } else if(strcmp(s, "sin") == 0) { push(sin(pop())); } else if(strcmp(s, "exp") == 0) { push(exp(pop())); } else if(strcmp(s, "pow") == 0) { op1 = pop(); push(pow(pop(), op1)); } else printf("unknown command: %s\n", s); }Code:#include <ctype.h> /* isdigit */ #include "calc.h" /* get a operand or operator, or a word */ int getop(char s[]) { int i = 0, c, next; while((s[0] = c = getch()) == ' ' || c == '\t') /* the first character */ ; s[1] = '\0'; /*1. if the first char is a letter, handle the string as a word */ if(isalpha(c)) { while(isalpha(s[++i] = c = getch())) ; ungetch(c); s[i] = '\0'; return WORD; } /*2. Handle numbers, possible unary minus - if not any of that just return */ if(!isdigit(c) && c != '.' && c != '-') return c; /* from now on, c can only be . or digit or - */ if(c == '-') { next = getch(); if(!isdigit(next) && next != '.') { /* . because -.25 is allowed */ ungetch(next); return c; /* not a number, but an operator, so we must return it! */ } c = next; /* if next is a . or a digit, just put it into c */ } else /* if c is a . or a number, get another char since . or number is allready stored in */ c = getch(); /* now c can only be . or - */ while(isdigit(s[++i] = c)) c = getch(); if(c == '.') while(isdigit(s[++i] = c = getch())) ; s[i] = '\0'; if(c!=EOF) ungetch(c); return NUMBER; }Code:#include "calc.h" #define MAXBUF 100 int buf[MAXBUF]; int bufp = 0; int getch() { return (bufp > 0) ? buf[--bufp] : getchar(); } void ungetch(int c) //stores the temp char { if(bufp >= MAXBUF) printf("ungetch: too many characters\n"); else buf[bufp++] = c; }Code:/* headers */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #define NUMBER '0' /* used by main and getop */ #define WORD 'a' int getop(char []); void push(double); double pop(); int getch(); void ungetch(int); void Handle_word(char []);