static unsigned long get_result(const char *expr) { int position = 0; unsigned long a, result = 0; const char *expression; nodelink *operator = NULL; nodelink *operand = NULL; expression = expr; while (*(expression + position) != '\0' && *(expression + position) != '\n') { switch (operator_class(*(expression + position))) { case c_digit: push(&operand, get_operand(expression + position, &position)); break; case c_binop: eval(&operator, &operand, *(expression + position)); push(&operator, *(expression + position)); break; case c_rpar: eval(&operator, &operand, ')'); if (operator->data == '(') pop(&operator); break; case c_lpar: push(&operator, '('); break; default: printf("\nInvalid character in expression:"); a = 0; while (*(expression + (int) a) != '\n' && *(expression + (int) a) != '\0') { if (a != position) printf("%c", *(expression + (int) a)); else printf("<%c>", *(expression + (int) a)); a++; } return 0; } /* end switch */ position++; } eval(&operator, &operand, ')'); result = pop(&operand); DEBUGMSG(("expValueTable", "%s: %s -> %ld\n", __func__, expr, result)); return result; }
unsigned long get_result(char *expr) { int position = 0; unsigned long op = 0, a = 0, b = 0, result = 0; char *expression; expression = expr; while (*(expression + position) != '\0' && *(expression + position) != '\n') { switch (operator_class(*(expression + position))) { case 1: push(&operand, get_operand(expression + position, &position)); break; case 2: if (operater != NULL) while (operater != NULL && priority(*(expression + position)) <= priority(operater->data)) { a = pop(&operand); b = pop(&operand); op = pop(&operater); push(&operand, calculate(op, b, a)); } push(&operater, *(expression + position)); break; case 3: while (operater != NULL && operater->data != '(') { a = pop(&operand); b = pop(&operand); op = pop(&operater); push(&operand, calculate(op, b, a)); } if (operater->data == '(') pop(&operater); break; case 4: push(&operater, '('); break; default: printf("\nInvalid character in expression:"); a = 0; while (*(expression + (int) a) != '\n' && *(expression + (int) a) != '\0') { if (a != position) printf("%c", *(expression + (int) a)); else printf("<%c>", *(expression + (int) a)); a++; } exit(0); } /* end switch */ position++; } while (operater != NULL) { op = pop(&operater); a = pop(&operand); b = pop(&operand); push(&operand, calculate(op, b, a)); } result = pop(&operand); return result; }