Programa diseñado para convertir una expresion infija a postfija..(Ejm. a+b -> ab+)
#include < conio.h > #include < iostream.h > #include < stdio.h > #include < string.h > #define MAX 25 class pila { public: int tope; char x[MAX]; public: void posfija(char ei[],char x[],int tope); int prioridad(char ei); char operador(char ei); }; void main() { char ei[MAX],dato; pila ob; ob.tope=-1; char op; do { clrscr(); gotoxy(29,7);cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(29,8);cout<<"³ APLICACION PILAS ³"; gotoxy(29,9);cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; gotoxy(20,12);cout<<"Tratamiento de Expresiones Aritmeticas"; gotoxy(20,13);cout<<"ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ"; gotoxy(26,16);cout<<"ingrese expresion aritmetica:"; gotoxy(18,19);cout<<"Notacion INFIJA: ";gotoxy(35,19);cin>>ei; gotoxy(18,22);ob.posfija(ei,ob.x,ob.tope); gotoxy(15,25);cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"; gotoxy(15,26);cout<<"³ ¨Desea transformar otra expresion (S/N)? [ ] ³"; gotoxy(15,27);cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"; gotoxy(59,26);cin>>op; }while(op=='s' || op=='S'); } void pila::posfija(char ei[],char x[],int tope) { char epos[MAX]=" "; int i,j; char dato; i=0; j=0; while(ei[i]!=NULL) { if(ei[i]=='(') { tope=tope+1; x[tope]=ei[i]; } else { if(ei[i]==')') { while(x[tope]!='(') { epos[j]=x[tope]; tope=tope-1; j++; } tope=tope-1; } else { dato=operador(ei[i]); if(ei[i]!=dato) { epos[j]=ei[i]; j++; } else { while(tope>-1 && prioridad(ei[i])<=prioridad(x[tope])) { epos[j]=x[tope]; tope=tope-1; j++; } tope=tope+1; x[tope]=ei[i]; } } } i++; } while(tope>-1) { epos[j]=x[tope]; tope=tope-1; j++; } cout<<"Notacion POSTFIJA: "<< epos; getch(); } int pila::prioridad(char s) { if (s=='+' || s=='-') return 0; else if (s=='*' || s=='/') return 1; else if (s=='^') return 2; return -1; } char pila::operador(char ei) { char operadores[5], op=' '; int i,cen; operadores[1]='+'; operadores[2]='-'; operadores[3]='*'; operadores[4]='/'; operadores[5]='^'; i=1; cen=0; while(i<=5 && cen==0) { if(operadores[i]==ei) { cen=1; op=operadores[i]; } i++; } return (op); }
0 Responses So Far: