void result (char * string, int n) { char exp; char *p; int num, num3; int num1, num2; int i = 0, j = 0; int flag = 0; int tmp[MAXSIZE]; char ope[MAXSIZE]; Operate *operate; Number *number; operate = init_operate(); number = init_number(); p = string; while (*p != '\0') { num = 0; flag = 0; while ((*p) >= '0' && (*p) <= '9') { flag = 1; num = (*p - '0') + num * 10; p++; } if (flag == 1) { tmp[i++] = num; push_num(number, num); // printf("压栈%d大小%d\n", num, number->len); } if (if_operte(*p) == 1) { pop_num(number, &num1); pop_num(number, &num2); // printf("弹出%d %d\n", num1, num2); // printf("还剩%d\n", number->len); push_num(number, calculate(num2, num1, *p)); top_number(number, &num3); /// printf("%d %c %d = %d\n", num2, *p, num1, num3); // printf("压栈%d\n", calculate(num2, num1, *p)); } p++; } top_number(number, &num); printf("结果是%d\n", num); }
// Solves the equation within a set of parenthesis // returns SUCCESS or FAIL int satisfy_paren() { char op; int larg; int rarg; int answer; int ret; ret = peek_op(&op); while (op != '(' && ret == SUCCESS) { // evaluate everything inside of the parens if (pop_num(&rarg) != SUCCESS) return FAIL; if (pop_num(&larg) != SUCCESS) return FAIL; if (pop_op(&op) != SUCCESS) return FAIL; if (evaluate(larg, op, rarg, &answer) != SUCCESS) return FAIL; debug_print("inside paren loop: @d@c@d=@d\n", larg, op, rarg, answer); push_num(answer); ret = peek_op(&op); } if (op == '(') { pop_op(&op); // consume the open paren } return ret; }
void Expressions::calc(void) { tecoInt result; tecoInt vright; Operator op; tecoInt vleft; if (!operators.items() || operators.peek() != OP_NUMBER) throw Error("Missing right operand"); vright = pop_num(); op = pop_op(); if (!operators.items() || operators.peek() != OP_NUMBER) throw Error("Missing left operand"); vleft = pop_num(); switch (op) { case OP_POW: for (result = 1; vright--; result *= vleft); break; case OP_MUL: result = vleft * vright; break; case OP_DIV: if (!vright) throw Error("Division by zero"); result = vleft / vright; break; case OP_MOD: if (!vright) throw Error("Remainder of division by zero"); result = vleft % vright; break; case OP_ADD: result = vleft + vright; break; case OP_SUB: result = vleft - vright; break; case OP_AND: result = vleft & vright; break; case OP_XOR: result = vleft ^ vright; break; case OP_OR: result = vleft | vright; break; default: /* shouldn't happen */ g_assert_not_reached(); } push(result); }
tecoInt Expressions::add_digit(gchar digit) { tecoInt n = args() > 0 ? pop_num() : 0; return push(n*radix + (n < 0 ? -1 : 1)*(digit - '0')); }
void transform(int n, int k) { Stack_num S; int m = 0, r = 0; int x = 0; CreatSatck_num(&S); r = n; while (r) { m = r % k; push_num(&S, m); r = r / k; } printf("转换进制后的值为:"); while (!(S.ebp == S.esp)) { pop_num(&S, &x); if (x<10) printf("%d", x); else { printf("%c", x - 10 + 'a'); } } printf("\n"); }
double rpn_internal(char *expression) { double value; long cycle_counter_stop0; char *expressionCopy; /* this is necessary to prevent UDF processing problems */ cycle_counter_stop0 = cycle_counter_stop; cycle_counter_stop = cycle_counter; cp_str(&expressionCopy, expression); #ifdef DEBUG fprintf(stderr, "rpn_internal: executing %s\n", expression); #endif push_code(expressionCopy, STATIC); execute_code(); free(expressionCopy); #ifdef DEBUG fprintf(stderr, "done\n"); #endif value = pop_num(); #ifdef DEBUG fprintf(stderr, "value = %e\n", value); #endif cycle_counter_stop = cycle_counter_stop0; return value; }
tecoInt Expressions::pop_num_calc(guint index, tecoInt imply) { eval(); if (num_sign < 0) set_num_sign(1); return args() > 0 ? pop_num(index) : imply; }
int EvaluateExpression() { int i = 0; Stack_num S; int ret = 0; int a = 0; int b = 0; CreateStack_num(&S); while (post[i] != '\0') { switch (judge_type(post[i])) { case 0: //读取的是符号 pop_num(&S, &b); //取除数字栈的栈顶元素 pop_num(&S,&a); //取除数字栈的栈顶元素 ret=operation(a,post[i],b); //对这两个数进行运算 push_num(&S, ret); //将运算结果入站 i++; ret = 0; break; case 1: while(judge_type(post[i])==1) { ret = ret * 10 + post[i] - '0'; //将字符数字转换成对应的整数 i++; } push_num(&S, ret); //将这个数字进行压栈 ret = 0; break; case -1: //读取的是空格 i++; break; } } ret = *(--(S.esp)); //弹出栈底数字 free(S.ebp); return ret; }
int EvaluateExpression() { Stack_num S; int ret = 0; int i = 0; int a = 0; int b = 0; CreatSatck_num(&S); while (post[i] != '\0') //读取后缀表达式数组中的符号 { switch (judge_type(post[i])) { case 0: //如果读取的是符号 pop_num(&S, &b); //将数字栈上面两个数字退栈 pop_num(&S, &a); ret = opreation(a, post[i], b); //将这两个数字运算,并将结果返回 push_num(&S, ret); //将结果入栈 ret = 0; i++; break; case 1: while (post[i] != ' ') //将连续的数字全部读取,并转化为相应的整数 { ret = ret * 10 + post[i] - '0'; i++; } push_num(&S, ret); //将结果压栈 ret = 0; break; default: i++; break; } } return *S.ebp; //将数字栈底元素返回 free(S.ebp); }
void Expressions::brace_return(guint keep_braces, guint args) { tecoInt return_numbers[args]; for (guint i = args; i; i--) return_numbers[i-1] = pop_num(); undo.push_var(brace_level); while (brace_level > keep_braces) { discard_args(); eval(true); brace_level--; } for (guint i = 0; i < args; i++) push(return_numbers[i]); }
void calc() { char type; double op2, op1; int i; printf(">>"); for (i = 0; i < sp; ++i) { if (tok[i].type == NUMBER) { printf("%g", tok[i].n); } else { printf("%c", tok[i].type); } switch (tok[i].type) { case NUMBER: push_num(tok[i].n); break; case '+': push_num(pop_num() + pop_num()); break; case '-': op2 = pop_num(); push_num(pop_num() - op2); break; case '*': push_num(pop_num() * pop_num()); break; case '/': op2 = pop_num(); if (op2 != 0.0) push_num(pop_num() / op2); else printf("error: zero divisor\n"); break; case '%': op2 = pop_num(); if (op2 != 0.0) push_num(fmod(pop_num(), op2)); else printf("error: zero divisor\n"); break; default: printf("unknown type: %c\n", type); break; } } printf("\n\t%.8g\n", pop_num()); sp = 0; }
// Solves the equation in 'str' // returns SUCCESS if equation was solved // returns FAIL if equation couldn't be solved // Solution returned in 'answer' int solve_equation(char* str, int *answer) { int j = 0; int ret; char curr; // intialize stack curr_num_stack = -1; curr_op_stack = -1; while (str[j] != 0) { curr = str[j]; // walk through the equation if (curr <= '9' && curr >= '0') { char t; int is_neg = 0; int val; // get the number (could be any number of spots) // this is a number, push it on the stack ret = peek_op(&t); if (ret == SUCCESS && t == '-') { if(pop_op(&t) != SUCCESS) return FAIL; push_op('+'); is_neg = 1; } int moved = 0; val = stoi(str+j, &moved); if (is_neg) val *= -1; is_neg = 0; push_num(val); //tmp = get_str_end(str+j); j += moved; continue; } if (curr == '(') { push_op(str[j]); j++; continue; } if (curr == ')') { if (satisfy_paren() != SUCCESS) return FAIL; j++; continue; } if (curr == '+' || curr == '-') { char p_op; ret = peek_op(&p_op); if (ret == SUCCESS && (p_op == '+' || p_op == '-' || p_op == '*' || p_op == '/')) { char prev_op; int larg, rarg, answer = 0; if (pop_num(&rarg)!= SUCCESS) return FAIL; if (pop_num(&larg)!= SUCCESS) return FAIL; if (pop_op(&prev_op)!= SUCCESS) return FAIL; if (evaluate(larg, prev_op, rarg, &answer) != SUCCESS) return FAIL; push_num(answer); } push_op(str[j]); j++; continue; } if (curr == '*' || curr == '/') { char prev_op; ret = peek_op(&prev_op); if (ret == SUCCESS && (prev_op == '*' || prev_op == '/')) { int larg, rarg, answer = 0; if (pop_num(&rarg)!= SUCCESS) return FAIL; if (pop_num(&larg)!= SUCCESS) return FAIL; if (pop_op(&prev_op)!= SUCCESS) return FAIL; if (evaluate(larg, prev_op, rarg, &answer) != SUCCESS) return FAIL; push_num(answer); } push_op(str[j]); j++; continue; } return FAIL; // Invalid input. } int b,a, tmp; pop_num(&b); while(curr_op_stack != -1) { if (pop_num(&a)!= SUCCESS) return FAIL; if (pop_op(&curr)!= SUCCESS) return FAIL; if (evaluate(a,curr,b, &tmp)!= SUCCESS) return FAIL; b = tmp; } *answer = b; return SUCCESS; }