#include #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("Invalid operator\n"); } } class StackIterador it; it.init( pS ); // it.percorrePilha(); // for( i=0; ! it.fim(); ++i ) // printf("%d:%6d\n", i++, it.prox() ); i=0; while( ! it.fim() ) printf("%d:%6d\n", i++, it.prox() ); // if( !it.fim() ) // { // printf("it.fim()=%d\n", it.fim() ); // i=0; printf("%d:%6d\n", i, it.prox() ); // } // if( !it.fim() ) // { // printf("it.fim()=%d\n", it.fim() ); // } // getchar(); // if( !it.fim() ) // { // i=1; printf("%d:%6d\n", i, it.prox() ); // } // if( !it.fim() ) // { // i=2; printf("%d:%6d\n", i, it.prox() ); // } } }