#include #include "stack-c.h" int getop(struct Stack* pS, int* n1, int* n2) { if( pS->empty() ) { printf("empty stack!\n"); return 0; } *n2 = pS->pop(); if( pS->empty() ) { pS->push(*n2); printf("two operands needed!\n"); return 0; } *n1 = pS->pop(); return 1; } int main(void) { struct Stack* pS = createStack(); while (1) { char str[31]; int i; printf("> "); gets_s(str, sizeof(str)); if( sscanf_s(str, " %d", &i, sizeof(i))==1 ) pS->push(i); else { int n1, n2; char c; sscanf_s(str, "%c", &c, sizeof(char)); switch(c) { case '+': if (getop(pS, &n1, &n2)) pS->push(n1+n2); break; case '-': if (getop(pS, &n1, &n2)) pS->push(n1-n2); break; case '/': if (getop(pS, &n1, &n2)) { if( n2 != 0 ) pS->push(n1/n2); else printf(" nao divide por zero \n"); } break; case '*': if (getop(pS, &n1, &n2)) pS->push(n1*n2); break; case 'q': return 0; default: printf("error\n"); } } pS->listaElems(); } }