#include #include #include "stack2.h" int getop(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) { Stack* s = createStack(); while (1) { char str[31]; int n; printf("> "); gets(str); if (sscanf(str, " %d", &n)==1) s->push(n); 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"); } } for (int i=0; itop; i++) printf("%d:%6d\n", i, s->elems[i]); } }