Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h>
- #include <stack>
- using std::stack;
- struct Pilha
- {
- int topo;
- unsigned capacidade;
- int* vetor;
- };
- struct Pilha* criarPilha( unsigned capacidade )
- {
- struct Pilha* pilha = (struct Pilha*) malloc(sizeof(struct Pilha));
- if (!pilha)
- return NULL;
- pilha->topo = -1;
- pilha->capacidade = capacidade;
- pilha->vetor = (int*) malloc(pilha->capacidade * sizeof(int));
- if (!pilha->vetor)
- return NULL;
- return pilha;
- }
- int isEmpty(struct Pilha* pilha)
- {
- return pilha->topo == -1 ;
- }
- char peek(struct Pilha* pilha)
- {
- return pilha->vetor[pilha->topo];
- }
- char pop(struct Pilha* pilha)
- {
- if (!isEmpty(pilha))
- return pilha->vetor[pilha->topo--] ;
- return '$';
- }
- void push(struct Pilha* pilha, char op)
- {
- pilha->vetor[++pilha->topo] = op;
- }
- int testaOperador(char c)
- {
- if (c == '+' || c == '-' ||
- c == '*' || c == '/' ||
- c == '^')
- return 0;
- return 1;
- }
- int prioridade(char ch)
- {
- switch (ch)
- {
- case '+':
- case '-':
- return 1;
- case '*':
- case '/':
- return 2;
- case '^':
- return 3;
- }
- return -1;
- }
- int shuntingYard(char* expressao)
- {
- int i, k;
- struct Pilha* pilha = criarPilha(strlen(expressao));
- if(pilha==NULL)
- return -1 ;
- for (i = 0, k = -1; expressao[i]; ++i)
- {
- if (testaOperador(expressao[i]))
- expressao[++k] = expressao[i];
- else if (expressao[i] == '(')
- push(pilha, expressao[i]);
- else if (expressao[i] == ')')
- {
- while (!isEmpty(pilha) && peek(pilha) != '(')
- expressao[++k] = pop(pilha);
- if (!isEmpty(pilha) && peek(pilha) != '(')
- return -1;
- else
- pop(pilha);
- }
- else
- {
- while (!isEmpty(pilha) && prioridade(expressao[i]) <= prioridade(peek(pilha)))
- expressao[++k] = pop(pilha);
- push(pilha, expressao[i]);
- }
- }
- while (!isEmpty(pilha))
- expressao[++k] = pop(pilha);
- }
- struct node
- {
- char valor;
- node* esquerda, *direita;
- };
- void emOrdem(node *t)
- {
- if(t)
- {
- emOrdem(t->esquerda);
- printf("%c ", t->valor);
- emOrdem(t->direita);
- }
- }
- node* newNode(int v)
- {
- node *temp = (struct node*)malloc(sizeof(struct node));
- temp->esquerda = temp->direita = NULL;
- temp->valor = v;
- return temp;
- }
- node* construirArvore(char expressao[])
- {
- //usei uma stack do c++ por falta de tempo de fazer outra
- stack<node *> st;
- node *t, *t1, *t2;
- for (int i=0; i<strlen(expressao); i++)
- {
- if (testaOperador(expressao[i]))
- {
- t = newNode(expressao[i]);
- st.push(t);
- }
- else
- {
- t = newNode(expressao[i]);
- t1 = st.top();
- st.pop();
- t2 = st.top();
- st.pop();
- t->direita = t1;
- t->esquerda = t2;
- st.push(t);
- }
- }
- t = st.top();
- st.pop();
- return t;
- }
- int main()
- {
- setlocale(LC_ALL, "Portuguese");
- //o programa não sabe lidar com parenteses
- char expressao[100];
- printf("digite uma expressão matemática válida:\n");
- scanf("%s", expressao);
- shuntingYard(expressao);
- node* r = construirArvore(expressao);
- printf("A expressão é: ");
- emOrdem(r);
- return 0;
- }
Add Comment
Please, Sign In to add comment