-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0227_calculate.cc
99 lines (92 loc) · 1.74 KB
/
Problem_0227_calculate.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <deque>
#include <iostream>
#include <string>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int calculate(string s)
{
deque<string> st;
string digit;
for (int i = 0; i < s.length(); i++)
{
if (s[i] != ' ')
{
if (s[i] >= '0' && s[i] <= '9')
{
digit.push_back(s[i]);
}
else
{
handleStack(st, digit, s[i]);
digit.clear();
}
}
}
handleStack(st, digit, ' ');
// 最后计算加减法
return computeStack(st);
}
void handleStack(deque<string> &st, string digit, char op)
{
if (st.empty() || st.back() == "+" || st.back() == "-")
{
// 先不计算加减法,全部入栈
st.push_back(digit);
}
else
{
// 计算乘除法
int num = stoi(digit);
string preOp = st.back();
st.pop_back();
int preNum = stoi(st.back());
st.pop_back();
if (preOp == "*")
{
st.push_back(std::to_string(preNum * num));
}
else
{
st.push_back(std::to_string(preNum / num));
}
}
st.push_back(string(1, op));
}
int computeStack(deque<string> &st)
{
int ans = stoi(st.front());
st.pop_front();
while (st.size() != 1)
{
string op = st.front();
st.pop_front();
int cur = stoi(st.front());
st.pop_front();
if (op == "+")
{
ans += cur;
}
else
{
ans -= cur;
}
}
return ans;
}
};
void testCalculate()
{
Solution s;
EXPECT_EQ_INT(7, s.calculate("3+2*2"));
EXPECT_EQ_INT(1, s.calculate("3/2"));
EXPECT_EQ_INT(5, s.calculate("3+5/2"));
EXPECT_SUMMARY;
}
int main()
{
testCalculate();
return 0;
}