import java.util.*;
/**
* @author hll[yellowdradra@foxmail.com]
* @description 给定一个字符串描述的算术表达式,计算出结果值。 输入字符串长度不超过100,合法的字符包括”+, -, *, /, (, )
* ”,”0-9”,字符串内容的合法性及表达式语法的合法性由做题者检查。本题目只涉及整型计算。
* @date 2021-05-27 00:42
**/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String expression = sc.nextLine();
System.out.println(calculate(expression));
}
/**
* 使用 map 维护一个运算符优先级
* 这里的优先级划分按照「数学」进行划分即可
*/
public static Map<Character, Integer> map = new HashMap<Character, Integer>(){{
put('-', 1);
put('+', 1);
put('*', 2);
put('/', 2);
}};
public static int calculate(String s) {
// 将所有的空格去掉,并将 (- 替换为 (0-,(+ 替换为 (0+
// 当然这里也可以不预处理,而是放到循环里面去做判断
s = s.replaceAll(" ", "");
s = s.replaceAll("\\(-", "(0-");
s = s.replaceAll("\\(\\+", "(0+");
char[] cs = s.toCharArray();
int n = s.length();
// 存放所有的数字
Deque<Integer> nums = new ArrayDeque<>();
// 为了防止第一个数为负数,先往 nums 加个 0
nums.addLast(0);
// 存放所有「非数字以外」的操作
Deque<Character> ops = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
char c = cs[i];
if (c == '(') {
ops.addLast(c);
} else if (c == ')') {
// 计算到最近一个左括号为止
while (!ops.isEmpty()) {
if (ops.peekLast() != '(') {
calc(nums, ops);
} else {
ops.pollLast();
break;
}
}
} else {
if (isNumber(c)) {
int u = 0;
int j = i;
// 将从 i 位置开始后面的连续数字整体取出,加入 nums
while (j < n && isNumber(cs[j])) {
u = u * 10 + (cs[j++] - '0');
}
nums.addLast(u);
i = j - 1;
} else {
// 有一个新操作要入栈时,先把栈内可以算的都算了
// 只有满足「栈内运算符」比「当前运算符」优先级高/同等,才进行运算
while (!ops.isEmpty() && ops.peekLast() != '(') {
char prev = ops.peekLast();
if (map.get(prev) >= map.get(c)) {
calc(nums, ops);
} else {
break;
}
}
ops.addLast(c);
}
}
}
// 将剩余的计算完
while (!ops.isEmpty()) {
calc(nums, ops);
}
return nums.peekLast();
}
public static void calc(Deque<Integer> nums, Deque<Character> ops) {
if (nums.isEmpty() || nums.size() < 2) {
return;
}
if (ops.isEmpty()) {
return;
}
int b = nums.pollLast(), a = nums.pollLast();
char op = ops.pollLast();
int ans = 0;
switch (op) {
case '+':
ans = a + b;
break;
case '-':
ans = a - b;
break;
case '*':
ans = a * b;
break;
case '/':
ans = a / b;
default:
break;
}
nums.addLast(ans);
}
public static boolean isNumber(char c) {
return Character.isDigit(c);
}
}