From ea3b8953c3ffa3a1b7d37fe1e55866a3a9853c4a Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:19:33 +0800 Subject: [PATCH 01/11] =?UTF-8?q?Update=20772=E5=9F=BA=E6=9C=AC=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=99=A83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0772.Basic Calculator III/README.md | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/solution/0700-0799/0772.Basic Calculator III/README.md b/solution/0700-0799/0772.Basic Calculator III/README.md index 74168905b4403..ffea04acb3e69 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README.md +++ b/solution/0700-0799/0772.Basic Calculator III/README.md @@ -85,6 +85,133 @@ tags: #### C++ ```cpp +class Solution { +public: + // 定义一个操作函数,根据操作符进行数学运算 + int operate(int b, char ch, int a) { + switch (ch) { + case '+': + return a + b; // 加法 + break; + case '-': + return a - b; // 减法 + break; + case '*': + return a * b; // 乘法 + break; + case '/': + return a / b; // 除法 + break; + 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(); // 返回最终结果 + } +}; + ``` From b19b24c0b447103d537a3f09c1fc437d0e89a68d Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:20:09 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solution/0700-0799/0772.Basic Calculator III/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0700-0799/0772.Basic Calculator III/README.md b/solution/0700-0799/0772.Basic Calculator III/README.md index ffea04acb3e69..f424da53f119b 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README.md +++ b/solution/0700-0799/0772.Basic Calculator III/README.md @@ -108,7 +108,7 @@ public: return 0; // 默认返回0,处理无效操作符 } - + // 计算字符串表达式的值 int calculate(string s) { int preority[250]; // 操作符优先级数组 preority['+'] = 1; From a857c455d8e642e417ce1388657c442b4009f086 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:23:05 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solution/0700-0799/0772.Basic Calculator III/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/solution/0700-0799/0772.Basic Calculator III/README.md b/solution/0700-0799/0772.Basic Calculator III/README.md index f424da53f119b..f956f688e2c8c 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README.md +++ b/solution/0700-0799/0772.Basic Calculator III/README.md @@ -85,6 +85,7 @@ tags: #### C++ ```cpp +//逆波兰表示法求解 class Solution { public: // 定义一个操作函数,根据操作符进行数学运算 From b3ffe34b0592b26948d4d664303940c6ccc39dfd Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:24:14 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solution/0700-0799/0772.Basic Calculator III/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0700-0799/0772.Basic Calculator III/README.md b/solution/0700-0799/0772.Basic Calculator III/README.md index f956f688e2c8c..ec9c2854dcb34 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README.md +++ b/solution/0700-0799/0772.Basic Calculator III/README.md @@ -198,7 +198,7 @@ public: } } - // 处理剩余的操作符 + // 处理剩余在栈中的表达式 while (!op.empty()) { ch = op.top(); op.pop(); From e33fa0b212576589ee52ea51ae3392bffb482ed6 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:26:27 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solution/0700-0799/0772.Basic Calculator III/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/solution/0700-0799/0772.Basic Calculator III/README.md b/solution/0700-0799/0772.Basic Calculator III/README.md index ec9c2854dcb34..bd1b27fa6c863 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README.md +++ b/solution/0700-0799/0772.Basic Calculator III/README.md @@ -90,6 +90,7 @@ class Solution { public: // 定义一个操作函数,根据操作符进行数学运算 int operate(int b, char ch, int a) { + //注意ab顺序 switch (ch) { case '+': return a + b; // 加法 From 655fcf4d861d9b04b47cdd3184b4e90c41d5f541 Mon Sep 17 00:00:00 2001 From: Leehouc <152672308+Leehouc@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:27:05 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solution/0700-0799/0772.Basic Calculator III/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0700-0799/0772.Basic Calculator III/README.md b/solution/0700-0799/0772.Basic Calculator III/README.md index bd1b27fa6c863..aea967a6315d7 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README.md +++ b/solution/0700-0799/0772.Basic Calculator III/README.md @@ -121,7 +121,7 @@ public: preority[')'] = 0; stack op; // 操作符栈 - stack num; // 数字栈 + stack num; // 操作数栈 int stringsize = s.size(); // 字符串长度 int i = 0; char ch; From 8b0ff3e49498852bae04c453225ec4eaf579988c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 12 Sep 2024 19:39:12 +0800 Subject: [PATCH 07/11] Update README.md --- solution/0700-0799/0772.Basic Calculator III/README.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/solution/0700-0799/0772.Basic Calculator III/README.md b/solution/0700-0799/0772.Basic Calculator III/README.md index aea967a6315d7..3a7dd9aabbe76 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README.md +++ b/solution/0700-0799/0772.Basic Calculator III/README.md @@ -85,25 +85,21 @@ tags: #### C++ ```cpp -//逆波兰表示法求解 +// 逆波兰表示法求解 class Solution { public: // 定义一个操作函数,根据操作符进行数学运算 int operate(int b, char ch, int a) { - //注意ab顺序 + // 注意ab顺序 switch (ch) { case '+': return a + b; // 加法 - break; case '-': return a - b; // 减法 - break; case '*': return a * b; // 乘法 - break; case '/': return a / b; // 除法 - break; default: break; } @@ -213,8 +209,6 @@ public: return num.top(); // 返回最终结果 } }; - - ``` #### Go From 0ec6afbea615c5fea939c08044d322cd1a1aa717 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 12 Sep 2024 19:40:54 +0800 Subject: [PATCH 08/11] Update README_EN.md --- .../0772.Basic Calculator III/README_EN.md | 128 +++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) 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..a284fdab0beef 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,133 @@ tags: #### C++ ```cpp - +#include +#include +using namespace std; + +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 From eeb421bf531bd96dc76c09e30de30a478ace80c2 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 12 Sep 2024 19:41:06 +0800 Subject: [PATCH 09/11] Create Solution.cpp --- .../0772.Basic Calculator III/Solution.cpp | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 solution/0700-0799/0772.Basic Calculator III/Solution.cpp 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..3468955d208d0 --- /dev/null +++ b/solution/0700-0799/0772.Basic Calculator III/Solution.cpp @@ -0,0 +1,127 @@ +#include +#include +using namespace std; + +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 + } +}; From 5553923915eace3d2cf902dd6f4ba86f51552ffe Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 12 Sep 2024 19:42:38 +0800 Subject: [PATCH 10/11] Update README_EN.md --- solution/0700-0799/0772.Basic Calculator III/README_EN.md | 4 ---- 1 file changed, 4 deletions(-) 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 a284fdab0beef..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,10 +83,6 @@ tags: #### C++ ```cpp -#include -#include -using namespace std; - class Solution { public: // Define an operation function that performs mathematical operations based on the operator From 5c1bb25591be70638a887254f954371b7f75de78 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 12 Sep 2024 19:42:51 +0800 Subject: [PATCH 11/11] Update Solution.cpp --- solution/0700-0799/0772.Basic Calculator III/Solution.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/solution/0700-0799/0772.Basic Calculator III/Solution.cpp b/solution/0700-0799/0772.Basic Calculator III/Solution.cpp index 3468955d208d0..505ba466b238c 100644 --- a/solution/0700-0799/0772.Basic Calculator III/Solution.cpp +++ b/solution/0700-0799/0772.Basic Calculator III/Solution.cpp @@ -1,7 +1,3 @@ -#include -#include -using namespace std; - class Solution { public: // Define an operation function that performs mathematical operations based on the operator