darkstar97

arvore binaria expressao

Sep 20th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <locale.h>
  5. #include <stack>
  6.  
  7. using std::stack;
  8.  
  9. struct Pilha
  10. {
  11.     int topo;
  12.     unsigned capacidade;
  13.     int* vetor;
  14. };
  15.  
  16. struct Pilha* criarPilha( unsigned capacidade )
  17. {
  18.     struct Pilha* pilha = (struct Pilha*) malloc(sizeof(struct Pilha));
  19.  
  20.     if (!pilha)
  21.         return NULL;
  22.  
  23.     pilha->topo = -1;
  24.     pilha->capacidade = capacidade;
  25.  
  26.     pilha->vetor = (int*) malloc(pilha->capacidade * sizeof(int));
  27.  
  28.     if (!pilha->vetor)
  29.         return NULL;
  30.     return pilha;
  31. }
  32.  
  33. int isEmpty(struct Pilha* pilha)
  34. {
  35.     return pilha->topo == -1 ;
  36. }
  37.  
  38. char peek(struct Pilha* pilha)
  39. {
  40.     return pilha->vetor[pilha->topo];
  41. }
  42.  
  43. char pop(struct Pilha* pilha)
  44. {
  45.     if (!isEmpty(pilha))
  46.         return pilha->vetor[pilha->topo--] ;
  47.     return '$';
  48. }
  49.  
  50. void push(struct Pilha* pilha, char op)
  51. {
  52.     pilha->vetor[++pilha->topo] = op;
  53. }
  54.  
  55. int testaOperador(char c)
  56. {
  57.     if (c == '+' || c == '-' ||
  58.             c == '*' || c == '/' ||
  59.             c == '^')
  60.         return 0;
  61.     return 1;
  62. }
  63.  
  64. int prioridade(char ch)
  65. {
  66.     switch (ch)
  67.     {
  68.     case '+':
  69.     case '-':
  70.         return 1;
  71.  
  72.     case '*':
  73.     case '/':
  74.         return 2;
  75.    
  76.     case '^':
  77.         return 3;
  78.  
  79.     }
  80.     return -1;
  81. }
  82.  
  83. int shuntingYard(char* expressao)
  84. {
  85.     int i, k;
  86.  
  87.     struct Pilha* pilha = criarPilha(strlen(expressao));
  88.     if(pilha==NULL)
  89.         return -1 ;
  90.  
  91.     for (i = 0, k = -1; expressao[i]; ++i)
  92.     {
  93.         if (testaOperador(expressao[i]))
  94.             expressao[++k] = expressao[i];
  95.  
  96.         else if (expressao[i] == '(')
  97.             push(pilha, expressao[i]);
  98.  
  99.         else if (expressao[i] == ')')
  100.         {
  101.             while (!isEmpty(pilha) && peek(pilha) != '(')
  102.                 expressao[++k] = pop(pilha);
  103.             if (!isEmpty(pilha) && peek(pilha) != '(')
  104.                 return -1;
  105.             else
  106.                 pop(pilha);
  107.         }
  108.         else
  109.         {
  110.             while (!isEmpty(pilha) && prioridade(expressao[i]) <= prioridade(peek(pilha)))
  111.                 expressao[++k] = pop(pilha);
  112.             push(pilha, expressao[i]);
  113.         }
  114.     }
  115.  
  116.     while (!isEmpty(pilha))
  117.         expressao[++k] = pop(pilha);
  118.  
  119. }
  120.  
  121. struct node
  122. {
  123.     char valor;
  124.     node* esquerda, *direita;
  125. };
  126.  
  127. void emOrdem(node *t)
  128. {
  129.     if(t)
  130.     {
  131.         emOrdem(t->esquerda);
  132.         printf("%c ", t->valor);
  133.         emOrdem(t->direita);
  134.     }
  135. }
  136.  
  137. node* newNode(int v)
  138. {
  139.     node *temp = (struct node*)malloc(sizeof(struct node));
  140.     temp->esquerda = temp->direita = NULL;
  141.     temp->valor = v;
  142.     return temp;
  143. }
  144.  
  145. node* construirArvore(char expressao[])
  146. {
  147.     //usei uma stack do c++ por falta de tempo de fazer outra
  148.     stack<node *> st;
  149.     node *t, *t1, *t2;
  150.  
  151.     for (int i=0; i<strlen(expressao); i++)
  152.     {
  153.         if (testaOperador(expressao[i]))
  154.         {
  155.             t = newNode(expressao[i]);
  156.             st.push(t);
  157.         }
  158.         else
  159.         {
  160.             t = newNode(expressao[i]);
  161.  
  162.             t1 = st.top();
  163.             st.pop();
  164.             t2 = st.top();
  165.             st.pop();
  166.  
  167.  
  168.             t->direita = t1;
  169.             t->esquerda = t2;
  170.  
  171.  
  172.             st.push(t);
  173.         }
  174.     }
  175.  
  176.     t = st.top();
  177.     st.pop();
  178.  
  179.     return t;
  180. }
  181.  
  182. int main()
  183. {
  184.     setlocale(LC_ALL, "Portuguese");
  185.     //o programa não sabe lidar com parenteses
  186.     char expressao[100];
  187.  
  188.     printf("digite uma expressão matemática válida:\n");
  189.     scanf("%s", expressao);
  190.  
  191.     shuntingYard(expressao);
  192.     node* r = construirArvore(expressao);
  193.  
  194.     printf("A expressão é: ");
  195.     emOrdem(r);
  196.  
  197.     return 0;
  198. }
Add Comment
Please, Sign In to add comment