int main(void) { vars = hashtbl_init(free, NULL, 0); /*fprintf(stderr, "%.15Lg\n", evaluate(argv[1]));*/ check_operators(); check_functions(); check_precedence(); check_longer(); hashtbl_destroy(vars); return 0; }
void infix_to_postfix(char *postfix, const char *infix) { stack_t stack; int i, j, len, p; char topstack, c; char str[2]; char msg[64]; len = strlen(infix); stack_init(&stack); j = 0; str[1] = 0; for (i = 0; i < len; i++) { c = infix[i]; switch (c) { case ' ': case '\t': case '\n': break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': print_postfix(c, j); break; case '(': str[0] = c; stack_push(&stack, str); break; case ')': while (c != '(') { c = stack_pop(&stack, NULL); if (c != '(') { print_postfix(' ', j); print_postfix(c, j); } } break; case '+': case '-': case '*': case '/': print_postfix(' ', j); topstack = stack_top(&stack, NULL); p = check_precedence(topstack, c); while (p && !stack_empty(&stack)) { print_postfix(stack_pop(&stack, NULL), j); print_postfix(' ', j); topstack = stack_top(&stack, NULL); p = check_precedence(topstack, c); } str[0] = c; stack_push(&stack, str); break; default: sprintf(msg, "invalid char %c", c); fatal(msg); } } while (!stack_empty(&stack)) { print_postfix(' ', j); print_postfix(stack_pop(&stack, NULL), j); } print_postfix(0, j); }