Skip to content

Commit 0ec6afb

Browse files
authored
Update README_EN.md
1 parent 8b0ff3e commit 0ec6afb

File tree

1 file changed

+127
-1
lines changed

1 file changed

+127
-1
lines changed

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

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,133 @@ tags:
8383
#### C++
8484

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

89215
#### Go

0 commit comments

Comments
 (0)