#include #include struct Pilha { int topo; //Posição do elemento topo int capacidade; float *pElem; //Elemento da pilha. No caso, float }; void criarpilha( struct Pilha *p, int c ) { p->topo = -1; p->capacidade = c; p->pElem = (float*) malloc (c * sizeof(float)); } void push( struct Pilha *p, float v) { p->topo++; p->pElem [p->topo] = v; } float pop( struct Pilha *p ) { float aux = p->pElem [p->topo]; p->topo--; return aux; } float retornatopo ( struct Pilha *p ) { return p->pElem [p->topo]; } int isEmpty( struct Pilha *p ) { if( p-> topo == -1 ) return 1; // true else return 0; // false } int isFull( struct Pilha *p ){ if(p->topo == p->capacidade - 1) return 1; else return 0; } int main() { struct Pilha minhapilha; int capacidade, op; float valor; printf( "\nCapacidade da pilha? " ); scanf( "%d", &capacidade ); criarpilha (&minhapilha, capacidade); while( 1 ){ /* loop infinito */ printf("\n1- empilhar (push)\n"); printf("2- desempilhar (POP)\n"); printf("3- Mostrar o topo \n"); printf("4- sair\n"); printf("\nopcao? "); scanf("%d", &op); switch (op){ case 1: //push if( isFull( &minhapilha ) == 1 ) printf("\nPILHA CHEIA! \n"); else { printf("\nVALOR?"); scanf("%f", &valor); push(&minhapilha, valor); } break; case 2: //pop if ( isEmpty(&minhapilha) == 1 ) printf( "\nPILHA VAZIA! \n" ); else { valor = pop(&minhapilha); printf ( "\n%.1f DESEMPILHADO!\n", valor ); } break; case 3: // mostrar o topo if ( isEmpty(&minhapilha) == 1 ) printf( "\nPILHA VAZIA!\n" ); else { valor = retornatopo (&minhapilha); printf ( "\nTOPO: %.1f\n", valor ); } break; case 4: exit(0); default: printf( "\nOPCAO INVALIDA! \n" ); break; } } return 0; }