int stack_num[MAXLEN]; //记录数字的栈 int top_num = -1; //记录数字栈的栈顶
intcal(int a, char op, int b) { switch (op) { case'+':return a + b; case'-':return a - b; case'*':return a * b; case'/':return a / b; case'%':return a % b; default: return0; } }
intresult(char output[])//output[] 中是后缀表达式 { int i, a, b, c, tmp_num; char tmp_op; for (i = 0; output[i] != '\0'; i++) { c = output[i]; if (isdigit(c)) { tmp_num = c - '0'; while ((c = output[++i]) && isdigit(c)) tmp_num = tmp_num * 10 + c - '0'; stack_num[++top_num] = tmp_num; } if (isop(c)) { //出栈两个数字,计算后将结果入栈 a = stack_num[top_num--]; b = stack_num[top_num--]; //注意 a,b 的顺序,不要反了 stack_num[++top_num] = cal(b, c, a); } } return stack_num[top_num]; }
intpriority(char c) { if (c == '(') return3; if (c == '*' || c == '/' || c == '%') return2; if (c == '+' || c == '-') return1; return0; }
intisop(char c) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '('; }
intcal(int a, char op, int b) { switch (op) { case'+':return a + b; case'-':return a - b; case'*':return a * b; case'/':return a / b; case'%':return a % b; default: return0; } }
int stack_num[MAXLEN]; //记录数字的栈 char stack_op[MAXLEN]; //记录运算符的栈 int top_num = -1; //记录数字栈的栈顶 int top_op = -1; //记录运算符栈的栈顶
intmain() { int c, a, b, tmp_num; char tmp_op; while ((c = fgetc(stdin)) != EOF) { if (isdigit(c)) { tmp_num = c - '0'; while ((c = fgetc(stdin)) != EOF && isdigit(c)) tmp_num = tmp_num * 10 + c - '0'; stack_num[++top_num] = tmp_num; }
classSolution{ privateintpriority(char c){ if (c == '(') return3; if (c == '*' || c == '/' || c == '%') return2; if (c == '+' || c == '-') return1; return0; }
privatebooleanisop(char c){ return c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '('; }
privateintcal(int a, char op, int b){ switch (op) { case'+': return a + b; case'-': return a - b; case'*': return a * b; case'/': return a / b; case'%': return a % b; default: return0; } }
publicintcalculate(String s){ Deque<Integer> stack_num = new LinkedList<>(); Deque<Character> stack_op = new LinkedList<>();
for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (Character.isDigit(c)) { int tmp_num = 0; while (Character.isDigit(c)) { tmp_num = tmp_num * 10 + c - '0'; ++i; if (i >= s.length()) break; c = s.charAt(i); } stack_num.push(tmp_num); }
if (isop(c)) { if (!stack_op.isEmpty() && priority(stack_op.peek()) >= priority(c)) { while (!stack_op.isEmpty() && priority(stack_op.peek()) >= priority(c) && stack_op.peek() != '(') { char tmp_op = stack_op.pop(); int a = stack_num.pop(); int b = stack_num.pop(); stack_num.push(cal(b, tmp_op, a)); } } stack_op.push(c); } elseif (c == ')') { while (!stack_op.isEmpty() && stack_op.peek() != '(') { char tmp_op = stack_op.pop(); int a = stack_num.pop(); int b = stack_num.pop(); stack_num.push(cal(b, tmp_op, a)); } stack_op.pop(); } }
while (!stack_op.isEmpty()) { char tmp_op = stack_op.pop(); int a = stack_num.pop(); int b = stack_num.pop(); stack_num.push(cal(b, tmp_op, a)); } return stack_num.pop(); } }