Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char *infixa_posfixa(char *inf){
- char *posfixa;
- char *pilha;
- int topo, n, i, j;
- n = strlen(inf);
- posfixa = malloc(n*sizeof(char));
- pilha = malloc(n*sizeof(char));
- topo = 0;
- pilha[topo++] = inf[0];
- for (j=0, i=1; inf[i] != 0; i++){
- switch (inf[i]) {
- case '(':
- pilha[topo++] = int[i];
- break;
- case ')':
- while(1){
- x = pilha[--topo];
- if (x == '(')
- break;
- posfixa[j++] = x;
- }
- break;
- case '+':
- case '-':
- while(1){
- x = pilha[topo-1];
- if(x=='(')
- break;
- topo--;
- posfixa[j++] = x;
- }
- pilha[topo++] = inf[i];
- break;
- case '*':
- case '/':
- while(1){
- x = pilha[topo-1];
- if(x == '(' || x == '+' || x == '-')
- break;
- topo--;
- posfixa[j++]= x;
- }
- pilha[topo++] = inf[i];
- break;
- default;
- posfixa[j++] = inf[i];
- }
- }
- free(pilha);
- posfixa[j] = '\0';
- return posfixa;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement