Skip to content

Commit a35b475

Browse files
authored
feat: add cpp solution to lc problem: No.0772 (#3517)
1 parent bafa484 commit a35b475

File tree

3 files changed

+370
-2
lines changed

3 files changed

+370
-2
lines changed

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

+124-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,130 @@ tags:
8585
#### C++
8686

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

91214
#### Go

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

+123-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,129 @@ tags:
8383
#### C++
8484

8585
```cpp
86-
86+
class Solution {
87+
public:
88+
// Define an operation function that performs mathematical operations based on the operator
89+
int operate(int b, char ch, int a) {
90+
// Note the order of ab
91+
switch (ch) {
92+
case '+':
93+
return a + b; // Addition
94+
case '-':
95+
return a - b; // Subtraction
96+
case '*':
97+
return a * b; // Multiplication
98+
case '/':
99+
return a / b; // Division
100+
default:
101+
break;
102+
}
103+
return 0; // Default return 0, handle invalid operators
104+
}
105+
106+
// Calculate the value of the string expression
107+
int calculate(string s) {
108+
int preority[250]; // Operator precedence array
109+
preority['+'] = 1;
110+
preority['-'] = 1;
111+
preority['*'] = 2;
112+
preority['/'] = 2;
113+
preority['('] = 0;
114+
preority[')'] = 0;
115+
116+
stack<char> op; // Operator stack
117+
stack<int> num; // Operand stack
118+
int stringsize = s.size(); // Length of the string
119+
int i = 0;
120+
char ch;
121+
122+
// Traverse the string
123+
for (; i < stringsize; i++) {
124+
ch = s[i];
125+
if (ch == ' ') {
126+
continue; // Skip spaces
127+
}
128+
if (ch >= '0' && ch <= '9') {
129+
int realnum = ch - '0'; // Convert character to number
130+
// Handle multi-digit numbers
131+
while (s[i + 1] >= '0' && s[i + 1] <= '9') {
132+
i++;
133+
realnum *= 10;
134+
realnum += s[i] - '0';
135+
}
136+
num.push(realnum); // Push the number onto the stack
137+
} else {
138+
// Handle operators
139+
if (op.empty() || ch == '(' || preority[ch] > preority[op.top()]) {
140+
// Special case, handle the first character being '-' or '+'
141+
if (num.empty() && (ch == '-' || ch == '+')) {
142+
num.push(0);
143+
}
144+
op.push(ch); // Push the operator onto the stack
145+
// Handle expressions inside parentheses
146+
if (ch == '(') {
147+
int j = i;
148+
while (j + 1 < stringsize) {
149+
// Preprocess the first operator inside the parentheses
150+
if (s[j + 1] == '-' || s[j + 1] == '+') {
151+
num.push(0);
152+
}
153+
if (s[j + 1] != ' ') {
154+
break;
155+
}
156+
j++;
157+
}
158+
}
159+
} else if (ch == ')') {
160+
// Handle right parentheses
161+
char ch2 = ')';
162+
ch2 = op.top();
163+
op.pop();
164+
while (ch2 != '(') {
165+
int a = num.top();
166+
num.pop();
167+
int b = num.top();
168+
num.pop();
169+
num.push(operate(a, ch2, b)); // Calculate and push the result
170+
ch2 = op.top();
171+
op.pop();
172+
}
173+
} else if (preority[ch] <= preority[op.top()]) {
174+
// Handle cases where the precedence is less than or equal to the top of the stack
175+
char ch2;
176+
ch2 = op.top();
177+
while (!op.empty() && preority[ch] <= preority[op.top()] && ch2 != '(') {
178+
op.pop();
179+
int a = num.top();
180+
num.pop();
181+
int b = num.top();
182+
num.pop();
183+
num.push(operate(a, ch2, b)); // Calculate and push the result
184+
if (!op.empty()) {
185+
ch2 = op.top();
186+
} else {
187+
break;
188+
}
189+
}
190+
op.push(ch); // Push the current operator onto the stack
191+
}
192+
}
193+
}
194+
195+
// Handle the remaining expressions in the stack
196+
while (!op.empty()) {
197+
ch = op.top();
198+
op.pop();
199+
int a = num.top();
200+
num.pop();
201+
int b = num.top();
202+
num.pop();
203+
num.push(operate(a, ch, b)); // Calculate and push the result
204+
}
205+
206+
return num.top(); // Return the final result
207+
}
208+
};
87209
```
88210

89211
#### Go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
class Solution {
2+
public:
3+
// Define an operation function that performs mathematical operations based on the operator
4+
int operate(int b, char ch, int a) {
5+
// Note the order of ab
6+
switch (ch) {
7+
case '+':
8+
return a + b; // Addition
9+
case '-':
10+
return a - b; // Subtraction
11+
case '*':
12+
return a * b; // Multiplication
13+
case '/':
14+
return a / b; // Division
15+
default:
16+
break;
17+
}
18+
return 0; // Default return 0, handle invalid operators
19+
}
20+
21+
// Calculate the value of the string expression
22+
int calculate(string s) {
23+
int preority[250]; // Operator precedence array
24+
preority['+'] = 1;
25+
preority['-'] = 1;
26+
preority['*'] = 2;
27+
preority['/'] = 2;
28+
preority['('] = 0;
29+
preority[')'] = 0;
30+
31+
stack<char> op; // Operator stack
32+
stack<int> num; // Operand stack
33+
int stringsize = s.size(); // Length of the string
34+
int i = 0;
35+
char ch;
36+
37+
// Traverse the string
38+
for (; i < stringsize; i++) {
39+
ch = s[i];
40+
if (ch == ' ') {
41+
continue; // Skip spaces
42+
}
43+
if (ch >= '0' && ch <= '9') {
44+
int realnum = ch - '0'; // Convert character to number
45+
// Handle multi-digit numbers
46+
while (s[i + 1] >= '0' && s[i + 1] <= '9') {
47+
i++;
48+
realnum *= 10;
49+
realnum += s[i] - '0';
50+
}
51+
num.push(realnum); // Push the number onto the stack
52+
} else {
53+
// Handle operators
54+
if (op.empty() || ch == '(' || preority[ch] > preority[op.top()]) {
55+
// Special case, handle the first character being '-' or '+'
56+
if (num.empty() && (ch == '-' || ch == '+')) {
57+
num.push(0);
58+
}
59+
op.push(ch); // Push the operator onto the stack
60+
// Handle expressions inside parentheses
61+
if (ch == '(') {
62+
int j = i;
63+
while (j + 1 < stringsize) {
64+
// Preprocess the first operator inside the parentheses
65+
if (s[j + 1] == '-' || s[j + 1] == '+') {
66+
num.push(0);
67+
}
68+
if (s[j + 1] != ' ') {
69+
break;
70+
}
71+
j++;
72+
}
73+
}
74+
} else if (ch == ')') {
75+
// Handle right parentheses
76+
char ch2 = ')';
77+
ch2 = op.top();
78+
op.pop();
79+
while (ch2 != '(') {
80+
int a = num.top();
81+
num.pop();
82+
int b = num.top();
83+
num.pop();
84+
num.push(operate(a, ch2, b)); // Calculate and push the result
85+
ch2 = op.top();
86+
op.pop();
87+
}
88+
} else if (preority[ch] <= preority[op.top()]) {
89+
// Handle cases where the precedence is less than or equal to the top of the stack
90+
char ch2;
91+
ch2 = op.top();
92+
while (!op.empty() && preority[ch] <= preority[op.top()] && ch2 != '(') {
93+
op.pop();
94+
int a = num.top();
95+
num.pop();
96+
int b = num.top();
97+
num.pop();
98+
num.push(operate(a, ch2, b)); // Calculate and push the result
99+
if (!op.empty()) {
100+
ch2 = op.top();
101+
} else {
102+
break;
103+
}
104+
}
105+
op.push(ch); // Push the current operator onto the stack
106+
}
107+
}
108+
}
109+
110+
// Handle the remaining expressions in the stack
111+
while (!op.empty()) {
112+
ch = op.top();
113+
op.pop();
114+
int a = num.top();
115+
num.pop();
116+
int b = num.top();
117+
num.pop();
118+
num.push(operate(a, ch, b)); // Calculate and push the result
119+
}
120+
121+
return num.top(); // Return the final result
122+
}
123+
};

0 commit comments

Comments
 (0)