Skip to content

Commit eeb421b

Browse files
authored
Create Solution.cpp
1 parent 0ec6afb commit eeb421b

File tree

1 file changed

+127
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)