document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.  
  3.   Name: Convertir expresion matematica en notacion infija a postfija
  4.   Author:   Joel Fernandez
  5.   Date: 16/11/12 20:29
  6.   Description: expresion matematica en notacion infija y la transforma a postfija
  7.   web: http://codebotic.blogspot.com
  8.  
  9.   Compartido para  casicodigo.blogspot.com
  10. */
  11. #include<iostream>
  12. #include<cstdlib>
  13. #include<stdio.h>
  14. #include<string.h>
  15. #define max 50
  16.  
  17. using namespace std;
  18.  
  19. struct nodo {     //ESTRUCTURA DE LA PILA
  20.        char palabra;
  21.        struct nodo *sgte;
  22.        };
  23.  
  24. typedef struct nodo *Ptrpila; //definimos estructura tipo pila
  25. typedef struct nodo *Tlista; //definimos estructura tipo lista
  26.  
  27.  
  28. void push(Ptrpila &,char);
  29. char pop(Ptrpila &);
  30. void agregar_atras(Tlista &,char);
  31. void destruir(Ptrpila &);
  32. int  prioridad_infija(char );
  33. int  prioridad_pila(char );
  34. void imprimir( Tlista &);
  35. void balanceoSimbolos( Ptrpila &, char []);
  36.  
  37.  
  38. /*                 Funcion Principal
  39. -----------------------------------------------------*/
  40.   int main(void)
  41.     {   Ptrpila p=NULL;
  42.         Ptrpila M=NULL;
  43.         Tlista lista=NULL;
  44.         char cad[max], c,x;
  45.         int tam;
  46.  
  47.         system("color 0b");
  48.  
  49.         cout<<"CONVERSION DE EXPRESIONES MATEMATICAS DE INFIJA A POSTFIJA\\n\\n";
  50.         do{
  51.            cout<<"INGRESE EXPRESION:";
  52.            gets(cad);
  53.               if(M!=NULL)
  54.                  destruir(M);
  55.            balanceoSimbolos(M,cad); //verificamos si los simbolos de agrupacion estan
  56.            }while(M!=NULL);         //correctamente valanceados
  57.  
  58.         tam=strlen(cad);
  59.         for(int i=0;i<tam;i++)
  60.         {
  61.             if((cad[i]>=49&&cad[i]<=57)||(cad[i]>=97&&cad[i]<=122))//validado para numeros de 1-9 y letras
  62.                 agregar_atras(lista,cad[i]);
  63.             if(cad[i]==\'+\'||cad[i]==\'-\'||cad[i]==\'*\'||cad[i]==\'/\'||cad[i]==\'(\'||cad[i]==\'^\')
  64.             {   if(p==NULL)
  65.                     push(p,cad[i]);
  66.                 else
  67.                 {
  68.                     if(prioridad_infija(cad[i])>prioridad_pila(p->palabra))//compara prioridad de operadores
  69.                         push(p,cad[i]);
  70.                     else
  71.                     {   if(prioridad_infija(cad[i])==prioridad_pila(p->palabra))
  72.                           {
  73.                             c=pop(p);
  74.                             agregar_atras(lista,c);
  75.                             push(p,cad[i]);
  76.                           }
  77.                         else
  78.                           {
  79.                             c=pop(p);
  80.                             agregar_atras(lista,c);
  81.                           }
  82.                     }
  83.                 }
  84.             }
  85.             if(cad[i]==\')\')
  86.                {
  87.                 while(p->palabra!=\'(\'&&p!=NULL)//desempilamos y agregamos a lista
  88.                    {
  89.                        c=pop(p);
  90.                        agregar_atras(lista,c);
  91.                     }
  92.                 if(p->palabra==\'(\')
  93.                         c=pop(p);
  94.                 }
  95.         }
  96.         while(p!=NULL)//si es que la pila aun no esta nula pasamos los operadores a lista
  97.             {
  98.                 c=pop(p);
  99.                 agregar_atras(lista,c);
  100.             }
  101.  
  102.         imprimir(lista);
  103.         system("pause");
  104.         return 0;
  105.     }
  106.  
  107. /*                 Apilar
  108. -------------------------------------------------*/
  109. void push(Ptrpila &p,char a)
  110. {
  111.     Ptrpila q=new struct nodo;
  112.     q->palabra=a;
  113.     q->sgte=p;
  114.     p=q;
  115.  }
  116.  
  117. /*                 Desempilar
  118. -------------------------------------------------*/
  119. char pop(Ptrpila &p)
  120. {
  121.     int n;
  122.     Ptrpila aux;
  123.  
  124.     n=p->palabra;
  125.     aux=p;
  126.     p=p->sgte;
  127.     delete(aux);
  128.     return n;
  129.  
  130. }
  131. /*                 Agregar a la Lista
  132. --------------------------------------------------
  133. funcion para agregar caracter a la lista de salida*/
  134. void agregar_atras(Tlista &lista,char a)
  135. {
  136.     Tlista t, q = new(struct nodo);
  137.  
  138.     q->palabra  = a;
  139.     q->sgte = NULL;
  140.  
  141.     if(lista==NULL)
  142.       {
  143.         lista = q;
  144.       }
  145.     else
  146.       {
  147.         t = lista;
  148.         while(t->sgte!=NULL)
  149.         {
  150.             t = t->sgte;
  151.         }
  152.         t->sgte = q;
  153.       }
  154.  
  155. }
  156. /*                 Destruir Pila
  157. --------------------------------------------------*/
  158. void destruir(Ptrpila &M)
  159. {    Ptrpila aux;
  160.  
  161.      if(M !=NULL)
  162.      {
  163.          while(M!=NULL)
  164.          {
  165.              aux=M;
  166.              M=M->sgte;
  167.              delete(aux);
  168.          }
  169.  
  170.       }
  171. }
  172.  
  173. /*          Prioridad en Notacion Infija
  174. ----------------------------------------------------
  175. esta prioridad se usa al momento de leer el caracter
  176. de la cadena*/
  177. int prioridad_infija(char a)
  178. {
  179.     if(a==\'^\')
  180.         return 4;
  181.     if( a==\'*\')
  182.         return 2;
  183.     if( a==\'/\')
  184.         return 2;
  185.     if( a==\'+\')
  186.         return 1;
  187.     if( a==\'-\')
  188.         return 1;
  189.     if(a==\'(\')
  190.         return 5;
  191. }
  192.  
  193. /*                 Prioridad en Pila
  194. ---------------------------------------------------
  195. esta prioridad es usada para los elementos que se
  196. encuentran en la pila */
  197. int prioridad_pila(char a)
  198. {
  199.     if(a==\'^\')
  200.         return 3;
  201.     if( a==\'*\')
  202.         return 2;
  203.     if( a==\'/\')
  204.         return 2;
  205.     if( a==\'+\')
  206.         return 1;
  207.     if( a==\'-\')
  208.         return 1;
  209.     if(a==\'(\')
  210.         return 0;
  211. }
  212. /*               Imprimir Lista
  213. ----------------------------------------------------*/
  214. void imprimir( Tlista &lista)
  215. {
  216.     Ptrpila aux;
  217.     aux=lista;
  218.  
  219.     if(lista!=NULL)
  220.      {    cout<<"\\t\\nNOTACION POSTFIJA\\n\\n";
  221.           while(aux!=NULL)
  222.           {    cout<<aux->palabra;
  223.                aux = aux->sgte;
  224.           }
  225.       }
  226.       cout<<endl<<endl;
  227. }
  228.  
  229. /*                Balanceo de simbolos de agrupacion
  230. ---------------------------------------------------------------------*/
  231. void balanceoSimbolos( Ptrpila &p, char cad[])
  232. {
  233.      Ptrpila aux;
  234.      int i = 0;
  235.  
  236.      while( cad[i] != \'\\0\')
  237.      {
  238.             if( cad[i]==\'(\' || cad[i]==\'[\' || cad[i]==\'{\' )
  239.             {
  240.                  push( p, cad[i] );
  241.             }
  242.             else if( cad[i]==\')\' || cad[i]==\']\' || cad[i]==\'}\' )
  243.             {
  244.                  aux = p;
  245.  
  246.                  if(aux!=NULL)
  247.                  {
  248.                       if( cad[i]==\')\' )
  249.                       {
  250.                            if( aux->palabra == \'(\')
  251.                               pop( p );
  252.                       }
  253.                       else if( cad[i]==\']\' )
  254.                       {
  255.                            if( aux->palabra == \'[\')
  256.                               pop( p );
  257.                       }
  258.                       else if( cad[i]==\'}\' )
  259.                       {
  260.                            if( aux->palabra == \'{\')
  261.                               pop( p );
  262.                       }
  263.                  }
  264.                  else
  265.                      push( p, cad[i] );
  266.             }
  267.             i++;
  268.      }
  269.  
  270.      if(p==NULL)
  271.          cout<<"\\n\\tBalanceo CORRECTO..."<<endl<<endl;
  272.      else
  273.          cout<<"\\n\\t Balanceo INCORRECTO, faltan simbolos de agrupacion..."<<endl;
  274.  
  275.  
  276. }
');