#include<stdio.h> #define MAXVAL 100// maximum depth of val stack int sp = 0; // new free stack position double val[MAXVAL]; // value stack // pop: pop and return top value from stack doublepop() { if (sp) return val[--sp]; else { printf("ERROR: empty stack.\n"); return0.0; } } // push: push f onto value stack voidpush(double v) { if (sp < MAXVAL) val[sp++] = v; else printf("ERROR: stack full, can not push %g\n", v); }
intgetop(char s[]) { int i, c; while ((s[0] = c = getch()) == ' ' || c == '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.') return c; /* not a number */ i = 0; if (isdigit(c)) /* collect integer part */ while (isdigit(s[++i] = c = getch())) ; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = getch())) ; s[i] = '\0'; if (c != EOF) ungetch(c); return NUMBER; }
#include<stdio.h> #include<string.h> #define BUFSIZE 100 char buf[BUFSIZE]; /* buffer for ungetch */ int bufp = 0; /* next free position in buf */ intgetch(void)/* get a (possibly pushed-back) character */ { if (buf[bufp - 1] == EOF) printf("EOF!"); return (bufp > 0) ? buf[--bufp] : getchar(); } voidungetch(int c)/* push character back on input */ { if (bufp >= BUFSIZE) printf("ungetch: too many characters\n"); else buf[bufp++] = c; }