forked from algorithm024/algorithm024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicCalculator.java
More file actions
54 lines (49 loc) · 1.54 KB
/
BasicCalculator.java
File metadata and controls
54 lines (49 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.ArrayDeque;
import java.util.Deque;
/**
* @author JackWu
* @version 1.0
*/
public class BasicCalculator {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.calculate("(1+(4+5+2)-3)+(6+8)"));
}
static class Solution {
public int calculate(String s) {
// 计算结果
int res = 0;
// 当前数字
int num = 0;
// + - 符号
int sign = 1;
Deque<Integer> stack = new ArrayDeque<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == ' ') {
continue;
} else if(Character.isDigit(c)) {
while (i < s.length() && Character.isDigit(s.charAt(i))) {
num = num * 10 + Integer.parseInt(String.valueOf(s.charAt(i)));
i++;
}
i--;
}else if (c == '+' || c == '-'){
sign = c == '+' ? 1 : -1;
num = 0;
}else if (c == '(') {
stack.push(res);
stack.push(sign);
res = 0;
sign = 1;
}else if (c == ')'){
sign = stack.pop();
num = res;
res = stack.pop();
}
res += sign * num;
}
return res;
}
}
}