Skip to content

Commit ea3b895

Browse files
authored
Update 772基本计算器3
1 parent bafa484 commit ea3b895

File tree

1 file changed

+127
-0
lines changed
  • solution/0700-0799/0772.Basic Calculator III

1 file changed

+127
-0
lines changed

solution/0700-0799/0772.Basic Calculator III/README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,133 @@ tags:
8585
#### C++
8686

8787
```cpp
88+
class Solution {
89+
public:
90+
// 定义一个操作函数,根据操作符进行数学运算
91+
int operate(int b, char ch, int a) {
92+
switch (ch) {
93+
case '+':
94+
return a + b; // 加法
95+
break;
96+
case '-':
97+
return a - b; // 减法
98+
break;
99+
case '*':
100+
return a * b; // 乘法
101+
break;
102+
case '/':
103+
return a / b; // 除法
104+
break;
105+
default:
106+
break;
107+
}
108+
return 0; // 默认返回0,处理无效操作符
109+
}
110+
111+
112+
int calculate(string s) {
113+
int preority[250]; // 操作符优先级数组
114+
preority['+'] = 1;
115+
preority['-'] = 1;
116+
preority['*'] = 2;
117+
preority['/'] = 2;
118+
preority['('] = 0;
119+
preority[')'] = 0;
120+
121+
stack<char> op; // 操作符栈
122+
stack<int> num; // 数字栈
123+
int stringsize = s.size(); // 字符串长度
124+
int i = 0;
125+
char ch;
126+
127+
// 遍历字符串
128+
for (; i < stringsize; i++) {
129+
ch = s[i];
130+
if (ch == ' ') {
131+
continue; // 跳过空格
132+
}
133+
if (ch >= '0' && ch <= '9') {
134+
int realnum = ch - '0'; // 将字符转换为数字
135+
// 处理多位数字
136+
while (s[i + 1] >= '0' && s[i + 1] <= '9') {
137+
i++;
138+
realnum *= 10;
139+
realnum += s[i] - '0';
140+
}
141+
num.push(realnum); // 将数字压入栈
142+
} else {
143+
// 处理操作符
144+
if (op.empty() || ch == '(' || preority[ch] > preority[op.top()]) {
145+
// 特殊情况,处理首个字符为'-'或'+'的情况
146+
if (num.empty() && (ch == '-' || ch == '+')) {
147+
num.push(0);
148+
}
149+
op.push(ch); // 将操作符压入栈
150+
// 处理括号内的表达式
151+
if (ch == '(') {
152+
int j = i;
153+
while (j + 1 < stringsize) {
154+
// 预处理括号内的首个操作符
155+
if (s[j + 1] == '-' || s[j + 1] == '+') {
156+
num.push(0);
157+
}
158+
if (s[j + 1] != ' ') {
159+
break;
160+
}
161+
j++;
162+
}
163+
}
164+
} else if (ch == ')') {
165+
// 处理右括号
166+
char ch2 = ')';
167+
ch2 = op.top();
168+
op.pop();
169+
while (ch2 != '(') {
170+
int a = num.top();
171+
num.pop();
172+
int b = num.top();
173+
num.pop();
174+
num.push(operate(a, ch2, b)); // 计算并压入结果
175+
ch2 = op.top();
176+
op.pop();
177+
}
178+
} else if (preority[ch] <= preority[op.top()]) {
179+
// 处理优先级小于等于栈顶操作符的情况
180+
char ch2;
181+
ch2 = op.top();
182+
while (!op.empty() && preority[ch] <= preority[op.top()] && ch2 != '(') {
183+
op.pop();
184+
int a = num.top();
185+
num.pop();
186+
int b = num.top();
187+
num.pop();
188+
num.push(operate(a, ch2, b)); // 计算并压入结果
189+
if (!op.empty()) {
190+
ch2 = op.top();
191+
} else {
192+
break;
193+
}
194+
}
195+
op.push(ch); // 将当前操作符压入栈
196+
}
197+
}
198+
}
199+
200+
// 处理剩余的操作符
201+
while (!op.empty()) {
202+
ch = op.top();
203+
op.pop();
204+
int a = num.top();
205+
num.pop();
206+
int b = num.top();
207+
num.pop();
208+
num.push(operate(a, ch, b)); // 计算并压入结果
209+
}
210+
211+
return num.top(); // 返回最终结果
212+
}
213+
};
214+
88215

89216
```
90217

0 commit comments

Comments
 (0)