Skip to content

Instantly share code, notes, and snippets.

@VergilSkye
Created October 26, 2018 03:59
Show Gist options
  • Save VergilSkye/e570d3e2b4901f8dd6001d4941e18441 to your computer and use it in GitHub Desktop.
Save VergilSkye/e570d3e2b4901f8dd6001d4941e18441 to your computer and use it in GitHub Desktop.
int resolverNPN(char *str){
char *eq = strrev(str);
int l = strlen(str);
// temporario que armazena a conversão do caracter para o numero tipo inteiro
int tmp;
// variaveis utiliza para as operações matematicas
int x, y, result;
stack_t *int_stack;
stack_initialize(&int_stack, constructor_int, destructor_int);
for (int i = 0; i < l; i++){
if(isdigit(eq[i])){
tmp = eq[i]-'0';
stack_push(int_stack,&tmp);
}
if(eq[i]=='+'){
x = *(int*)stack_top(int_stack);
stack_pop(int_stack);
y = *(int*)stack_top(int_stack);;
stack_pop(int_stack);
result = x+y;
stack_push(int_stack,&result);
}
if(eq[i]=='-'){
x = *(int*)stack_top(int_stack);
stack_pop(int_stack);
y = *(int*)stack_top(int_stack);;
stack_pop(int_stack);
result = x-y;
stack_push(int_stack,&result);
}
if(eq[i]=='*'){
x = *(int*)stack_top(int_stack);
stack_pop(int_stack);
y = *(int*)stack_top(int_stack);;
stack_pop(int_stack);
result = x*y;
stack_push(int_stack,&result);
}
if(eq[i]=='/'){
x = *(int*)stack_top(int_stack);
stack_pop(int_stack);
y = *(int*)stack_top(int_stack);;
stack_pop(int_stack);
result = x/y;
stack_push(int_stack,&result);
}
}
return *(int*)stack_top(int_stack);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment