Skip to content

Instantly share code, notes, and snippets.

@VergilSkye
Last active October 26, 2018 03:05
Show Gist options
  • Save VergilSkye/7314679583886909190ca8e635b33295 to your computer and use it in GitHub Desktop.
Save VergilSkye/7314679583886909190ca8e635b33295 to your computer and use it in GitHub Desktop.
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