-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicCalculator.java
More file actions
153 lines (142 loc) · 4.21 KB
/
BasicCalculator.java
File metadata and controls
153 lines (142 loc) · 4.21 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package hard;
import java.util.Stack;
/**
* ClassName: BasicCalculator.java
* Author: chenyiAlone
* Create Time: 2019/5/13 16:39
* Description: No.224
* 思路:
* 1. 思路就是处理优先级的问题
* 扫描到一个字符,就将 operate stack 中之前 连续的并且优先级相等或者更高的操作符 弹出并计算压入数组栈中
* `(` 的优先级最低
* `*` `/` 的优先级高于 `+` `-`
* 2. 递归写法
*
*
*
*
*
* Implement a basic calculator to evaluate a simple expression string.
*
* The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
*
* Example 1:
*
* Input: "1 + 1"
* Output: 2
* Example 2:
*
* Input: " 2-1 + 2 "
* Output: 3
* Example 3:
*
* Input: "(1+(4+5+2)-3)+(6+8)"
* Output: 23
* Note:
* You may assume that the given expression is always valid.
* Do not use the eval built-in library function.
*
*
*/
public class BasicCalculator {
private char[] str;
private int index;
// 2. 递归写法
private int cacul() {
int ret = 0, flag = 1;
while (index < str.length && str[index] != ')') {
if ('0' <= str[index] && str[index] <= '9') {
int num = 0;
while (index < str.length && '0' <= str[index] && str[index] <= '9') {
num = num * 10 + str[index] - '0';
index++;
}
ret += flag * num;
} else {
switch(str[index++]) {
case '+':
flag = 1;
break;
case '-':
flag = -1;
break;
case '(':
ret += flag * cacul();
break;
case ' ':
break;
}
}
}
if (index < str.length && str[index] == ')') {
index++;
}
return ret;
}
public int calculateByRec(String s) {
str = s.toCharArray();
return cacul();
}
// 1. 优先级递归
private Stack<Integer> numbers = new Stack<>();
private Stack<Character> operate = new Stack<>();
public int calculate(String s) {
char[] str = s.toCharArray();
int len = str.length;
for (int i = 0; i < len; i++) {
if (str[i] == ' ')
continue;
if (isNumber(str[i])) {
int N = str[i] - '0';
while (i + 1 < len && isNumber(str[i + 1])) {
N = N * 10 + (str[i + 1] - '0');
i++;
}
numbers.push(N);
} else {
if (str[i] == ')') {
while (!operate.isEmpty() && operate.peek() != '(')
pop_operate();
operate.pop(); // pop '('
} else {
if (str[i] == '+' || str[i] == '-') {
while (!operate.isEmpty() && operate.peek() != '(')
pop_operate();
operate.push(str[i]);
} else {
while (!operate.isEmpty() && (operate.peek() == '*' || operate.peek() == '/') && operate.peek() != '(')
pop_operate();
operate.push(str[i]);
}
}
}
}
while (!operate.isEmpty())
pop_operate();
return numbers.pop();
}
private boolean isNumber(Character c) {
return '0' <= c && c <= '9';
}
private void pop_operate() {
int a = numbers.pop();
int b = numbers.pop();
char c = operate.pop();
switch(c) {
case '+':
numbers.push(a + b);
break;
case '-':
numbers.push(b - a);
break;
case '*':
numbers.push(a * b);
break;
case '/':
numbers.push(b / a);
break;
default:
break;
}
}
}