Código
#include <iostream> #include <string.h> #include <stdlib.h> using namespace std; struct Nodo{ int Elemento; Nodo *Sig; }; class Pila{ private: Nodo *Tope; public: void Crear(); bool Vacia(); void Meter(int); void Sacar(int &); //int Cima(); void Mostrar(); void Analizar(); }; void Pila::Crear(){ Tope=NULL; } bool Pila::Vacia(){ return Tope==NULL; } //int Pila::Cima(){ //if(!Vacia()){ //return Tope->Elemento; //} //} void Pila::Meter(int E){ Nodo *aux=new Nodo(); if(aux!=NULL){ aux->Elemento=E; aux->Sig=Tope; Tope=aux; } } void Pila::Sacar(int &E){ if(!Vacia()){ Nodo *aux=Tope; E=Tope->Elemento; Tope=Tope->Sig; delete(aux); } } void Pila::Analizar(){ Pila p; char frase [30]; int x,c,d; p.Crear(); c=0; d=0; cout<<"Escriba un texto: "<<endl; cin>>frase; int i=0; while(frase[i]!='\0'){ if(frase[i]=='('){ p.Meter(frase[i]); c++; } if(frase[i]==')'){ p.Sacar(x); d++; } i++; } if(c==d) cout<<"Los parentesis estan correctos"<<endl; else cout<<"Los parentesis estan incorrectos"<<endl; } void Pila::Mostrar(){ Nodo *aux=Tope; if (!Vacia()) { while (aux!=NULL){ cout<<"||\t"; cout<<aux->Elemento; cout<<"\t ||\n"; aux=aux->Sig; } }else{ cout<<" ||\t \t||\n"; } } int main(){ bool menu=true;int i;Pila P; while(menu){ cout<<"\t\t*********************************"<<endl; cout<<"\t\t*\tMENU\t\t\t*"<<endl; cout<<"\t\t*********************************"<<endl; cout<<"\t\t*\t1.-Analizar Frase: \t*"<<endl; cout<<"\t\t*\t2.-Mostrar Pila\t\t*"<<endl; cout<<"\t\t*\t3.-Salir\t\t*"<<endl; cout<<"\t\t*********************************"<<endl; cin>>i; if(i==1){ P.Analizar(); }else{ if(i==2){ P.Mostrar(); }else{ if(i==3){ exit(0); } } } } return 0; }