#include #include #include "stack1.h" int getop(struct Stack* s, int* n1, int* n2) { if (s->empty()) { printf("empty stack!\n"); return 0; } *n2 = s->pop(); if (s->empty()) { s->push(*n2); printf("two operands needed!\n"); return 0; } *n1 = s->pop(); return 1; } int main(void) { struct Stack* s = createStack(); while (1) { char str[31]; int i; printf("> "); gets(str); if (sscanf(str, " %d", &i)==1) s->push(i); else { int n1, n2; char c; sscanf(str, "%c", &c); switch(c) { case '+': if (getop(s, &n1, &n2)) s->push(n1+n2); break; case '-': if (getop(s, &n1, &n2)) s->push(n1-n2); break; case '/': if (getop(s, &n1, &n2)) s->push(n1/n2); break; case '*': if (getop(s, &n1, &n2)) s->push(n1*n2); break; case 'q': return 0; default: printf("error\n"); } } { int i; for (i=0; itop; i++) printf("%d:%6d\n", i, s->elems[i]); } } }