-
-
Save VergilSkye/7314679583886909190ca8e635b33295 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char *infixToPostfix(char *infix){ | |
int l = strlen(infix); | |
stack_t *char_stack; | |
stack_initialize(&char_stack, constructor_char, destructor_char); | |
char *output = malloc(l *sizeof(char)); | |
char aux; | |
for (int i = 0; i < l; i++){ | |
//Caso seja um numero coloque direto na variavel outpur | |
if (isdigit(infix[i]) || isalpha(infix[i])) { | |
aux = infix[i]; | |
append(output, aux); | |
} | |
//Caso seja um Parenteses direito adiciona a pilha de operandos | |
else if (infix[i] == '(' { | |
stack_push(char_stack, &infix[i]); | |
} | |
//Caso seja um Parenteses esquerdo, ')', vá retirando os operandos | |
//até encontra um Parenteses direito, '(', retirando ele também. | |
else if (infix[i] == ')'){ | |
aux = *(char*)stack_top(char_stack); | |
while (aux != '('){ | |
append(output, aux); | |
stack_pop(char_stack); | |
aux = *(char *)stack_top(char_stack); | |
} | |
stack_pop(char_stack); | |
} | |
// Caso seja um operador | |
else{ | |
aux = *(char *)stack_top(char_stack); | |
//Existe algum operador na pilha? se sim entra no if | |
if (isOperator(aux)){ | |
//O operador na pilha, aux, tem maior prioridade? | |
// Se sim retira da pilha e coloca no output | |
while (getPriority(infix[i]) <= getPriority(aux)){ | |
append(output, aux); | |
stack_pop(char_stack); | |
aux = *(char *)stack_top(char_stack); | |
} | |
} | |
stack_push(char_stack, &infix[i]); | |
} | |
} | |
//Retorna string da notação polonesa reversa | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment