#include #include "stack-c.h" int getop(class Stack* pS, TPELEM* n1, TPELEM* 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) { class Stack* pS = Stack::create(); while( 1 ) { char str[31]; TPELEM i; printf("> "); gets_s(str, sizeof(str)); if( sscanf_s(str, " %d", &i, sizeof(i))==1 ) pS->push(i); else { TPELEM 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"); } } class StackIterador it; it.init( pS ); it.percorrePilha(); } }