Skip to content

加入722基本计算器3的cpp代码与注释 #3517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 124 additions & 1 deletion solution/0700-0799/0772.Basic Calculator III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,130 @@ tags:
#### C++

```cpp

// 逆波兰表示法求解
class Solution {
public:
// 定义一个操作函数,根据操作符进行数学运算
int operate(int b, char ch, int a) {
// 注意ab顺序
switch (ch) {
case '+':
return a + b; // 加法
case '-':
return a - b; // 减法
case '*':
return a * b; // 乘法
case '/':
return a / b; // 除法
default:
break;
}
return 0; // 默认返回0,处理无效操作符
}

// 计算字符串表达式的值
int calculate(string s) {
int preority[250]; // 操作符优先级数组
preority['+'] = 1;
preority['-'] = 1;
preority['*'] = 2;
preority['/'] = 2;
preority['('] = 0;
preority[')'] = 0;

stack<char> op; // 操作符栈
stack<int> num; // 操作数栈
int stringsize = s.size(); // 字符串长度
int i = 0;
char ch;

// 遍历字符串
for (; i < stringsize; i++) {
ch = s[i];
if (ch == ' ') {
continue; // 跳过空格
}
if (ch >= '0' && ch <= '9') {
int realnum = ch - '0'; // 将字符转换为数字
// 处理多位数字
while (s[i + 1] >= '0' && s[i + 1] <= '9') {
i++;
realnum *= 10;
realnum += s[i] - '0';
}
num.push(realnum); // 将数字压入栈
} else {
// 处理操作符
if (op.empty() || ch == '(' || preority[ch] > preority[op.top()]) {
// 特殊情况,处理首个字符为'-'或'+'的情况
if (num.empty() && (ch == '-' || ch == '+')) {
num.push(0);
}
op.push(ch); // 将操作符压入栈
// 处理括号内的表达式
if (ch == '(') {
int j = i;
while (j + 1 < stringsize) {
// 预处理括号内的首个操作符
if (s[j + 1] == '-' || s[j + 1] == '+') {
num.push(0);
}
if (s[j + 1] != ' ') {
break;
}
j++;
}
}
} else if (ch == ')') {
// 处理右括号
char ch2 = ')';
ch2 = op.top();
op.pop();
while (ch2 != '(') {
int a = num.top();
num.pop();
int b = num.top();
num.pop();
num.push(operate(a, ch2, b)); // 计算并压入结果
ch2 = op.top();
op.pop();
}
} else if (preority[ch] <= preority[op.top()]) {
// 处理优先级小于等于栈顶操作符的情况
char ch2;
ch2 = op.top();
while (!op.empty() && preority[ch] <= preority[op.top()] && ch2 != '(') {
op.pop();
int a = num.top();
num.pop();
int b = num.top();
num.pop();
num.push(operate(a, ch2, b)); // 计算并压入结果
if (!op.empty()) {
ch2 = op.top();
} else {
break;
}
}
op.push(ch); // 将当前操作符压入栈
}
}
}

// 处理剩余在栈中的表达式
while (!op.empty()) {
ch = op.top();
op.pop();
int a = num.top();
num.pop();
int b = num.top();
num.pop();
num.push(operate(a, ch, b)); // 计算并压入结果
}

return num.top(); // 返回最终结果
}
};
```

#### Go
Expand Down
124 changes: 123 additions & 1 deletion solution/0700-0799/0772.Basic Calculator III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,129 @@ tags:
#### C++

```cpp

class Solution {
public:
// Define an operation function that performs mathematical operations based on the operator
int operate(int b, char ch, int a) {
// Note the order of ab
switch (ch) {
case '+':
return a + b; // Addition
case '-':
return a - b; // Subtraction
case '*':
return a * b; // Multiplication
case '/':
return a / b; // Division
default:
break;
}
return 0; // Default return 0, handle invalid operators
}

// Calculate the value of the string expression
int calculate(string s) {
int preority[250]; // Operator precedence array
preority['+'] = 1;
preority['-'] = 1;
preority['*'] = 2;
preority['/'] = 2;
preority['('] = 0;
preority[')'] = 0;

stack<char> op; // Operator stack
stack<int> num; // Operand stack
int stringsize = s.size(); // Length of the string
int i = 0;
char ch;

// Traverse the string
for (; i < stringsize; i++) {
ch = s[i];
if (ch == ' ') {
continue; // Skip spaces
}
if (ch >= '0' && ch <= '9') {
int realnum = ch - '0'; // Convert character to number
// Handle multi-digit numbers
while (s[i + 1] >= '0' && s[i + 1] <= '9') {
i++;
realnum *= 10;
realnum += s[i] - '0';
}
num.push(realnum); // Push the number onto the stack
} else {
// Handle operators
if (op.empty() || ch == '(' || preority[ch] > preority[op.top()]) {
// Special case, handle the first character being '-' or '+'
if (num.empty() && (ch == '-' || ch == '+')) {
num.push(0);
}
op.push(ch); // Push the operator onto the stack
// Handle expressions inside parentheses
if (ch == '(') {
int j = i;
while (j + 1 < stringsize) {
// Preprocess the first operator inside the parentheses
if (s[j + 1] == '-' || s[j + 1] == '+') {
num.push(0);
}
if (s[j + 1] != ' ') {
break;
}
j++;
}
}
} else if (ch == ')') {
// Handle right parentheses
char ch2 = ')';
ch2 = op.top();
op.pop();
while (ch2 != '(') {
int a = num.top();
num.pop();
int b = num.top();
num.pop();
num.push(operate(a, ch2, b)); // Calculate and push the result
ch2 = op.top();
op.pop();
}
} else if (preority[ch] <= preority[op.top()]) {
// Handle cases where the precedence is less than or equal to the top of the stack
char ch2;
ch2 = op.top();
while (!op.empty() && preority[ch] <= preority[op.top()] && ch2 != '(') {
op.pop();
int a = num.top();
num.pop();
int b = num.top();
num.pop();
num.push(operate(a, ch2, b)); // Calculate and push the result
if (!op.empty()) {
ch2 = op.top();
} else {
break;
}
}
op.push(ch); // Push the current operator onto the stack
}
}
}

// Handle the remaining expressions in the stack
while (!op.empty()) {
ch = op.top();
op.pop();
int a = num.top();
num.pop();
int b = num.top();
num.pop();
num.push(operate(a, ch, b)); // Calculate and push the result
}

return num.top(); // Return the final result
}
};
```

#### Go
Expand Down
123 changes: 123 additions & 0 deletions solution/0700-0799/0772.Basic Calculator III/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
class Solution {
public:
// Define an operation function that performs mathematical operations based on the operator
int operate(int b, char ch, int a) {
// Note the order of ab
switch (ch) {
case '+':
return a + b; // Addition
case '-':
return a - b; // Subtraction
case '*':
return a * b; // Multiplication
case '/':
return a / b; // Division
default:
break;
}
return 0; // Default return 0, handle invalid operators
}

// Calculate the value of the string expression
int calculate(string s) {
int preority[250]; // Operator precedence array
preority['+'] = 1;
preority['-'] = 1;
preority['*'] = 2;
preority['/'] = 2;
preority['('] = 0;
preority[')'] = 0;

stack<char> op; // Operator stack
stack<int> num; // Operand stack
int stringsize = s.size(); // Length of the string
int i = 0;
char ch;

// Traverse the string
for (; i < stringsize; i++) {
ch = s[i];
if (ch == ' ') {
continue; // Skip spaces
}
if (ch >= '0' && ch <= '9') {
int realnum = ch - '0'; // Convert character to number
// Handle multi-digit numbers
while (s[i + 1] >= '0' && s[i + 1] <= '9') {
i++;
realnum *= 10;
realnum += s[i] - '0';
}
num.push(realnum); // Push the number onto the stack
} else {
// Handle operators
if (op.empty() || ch == '(' || preority[ch] > preority[op.top()]) {
// Special case, handle the first character being '-' or '+'
if (num.empty() && (ch == '-' || ch == '+')) {
num.push(0);
}
op.push(ch); // Push the operator onto the stack
// Handle expressions inside parentheses
if (ch == '(') {
int j = i;
while (j + 1 < stringsize) {
// Preprocess the first operator inside the parentheses
if (s[j + 1] == '-' || s[j + 1] == '+') {
num.push(0);
}
if (s[j + 1] != ' ') {
break;
}
j++;
}
}
} else if (ch == ')') {
// Handle right parentheses
char ch2 = ')';
ch2 = op.top();
op.pop();
while (ch2 != '(') {
int a = num.top();
num.pop();
int b = num.top();
num.pop();
num.push(operate(a, ch2, b)); // Calculate and push the result
ch2 = op.top();
op.pop();
}
} else if (preority[ch] <= preority[op.top()]) {
// Handle cases where the precedence is less than or equal to the top of the stack
char ch2;
ch2 = op.top();
while (!op.empty() && preority[ch] <= preority[op.top()] && ch2 != '(') {
op.pop();
int a = num.top();
num.pop();
int b = num.top();
num.pop();
num.push(operate(a, ch2, b)); // Calculate and push the result
if (!op.empty()) {
ch2 = op.top();
} else {
break;
}
}
op.push(ch); // Push the current operator onto the stack
}
}
}

// Handle the remaining expressions in the stack
while (!op.empty()) {
ch = op.top();
op.pop();
int a = num.top();
num.pop();
int b = num.top();
num.pop();
num.push(operate(a, ch, b)); // Calculate and push the result
}

return num.top(); // Return the final result
}
};
Loading