diff --git a/solution/0700-0799/0772.Basic Calculator III/README.md b/solution/0700-0799/0772.Basic Calculator III/README.md index 74168905b4403..3a7dd9aabbe76 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README.md +++ b/solution/0700-0799/0772.Basic Calculator III/README.md @@ -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 op; // 操作符栈 + stack 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 diff --git a/solution/0700-0799/0772.Basic Calculator III/README_EN.md b/solution/0700-0799/0772.Basic Calculator III/README_EN.md index 6e5dbf9169bb7..82619b07a78c1 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README_EN.md +++ b/solution/0700-0799/0772.Basic Calculator III/README_EN.md @@ -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 op; // Operator stack + stack 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 diff --git a/solution/0700-0799/0772.Basic Calculator III/Solution.cpp b/solution/0700-0799/0772.Basic Calculator III/Solution.cpp new file mode 100644 index 0000000000000..505ba466b238c --- /dev/null +++ b/solution/0700-0799/0772.Basic Calculator III/Solution.cpp @@ -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 op; // Operator stack + stack 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 + } +};