-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicCalculatorII.cpp
60 lines (49 loc) · 1.43 KB
/
BasicCalculatorII.cpp
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
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
int calculate(string s) {
vector<int> nums;
vector<char> operators;
for(int i=0;i<s.length();i++){
char c = s[i];
if(c == ' ') continue;
if(c >= '0' && c <= '9'){
int num = 0;
while(i < s.length() && s[i] >= '0' && s[i] <= '9'){
num = num * 10 + s[i] - '0';
i ++;
}
i --;
if(!operators.empty() && (operators.back() == '*' || operators.back() == '/')){
int pre = nums.back();
nums.pop_back();
if(operators.back() == '*') num = pre * num;
else num = pre / num;
operators.pop_back();
}
nums.push_back(num);
}else{
operators.push_back(c);
}
}
int result = nums.front();
for(int i=0;i<operators.size();i++){
if(operators[i] == '+'){
result += nums[i+1];
}else{
result -= nums[i+1];
}
}
return result;
}
};
int main(){
string s;
getline(cin, s);
Solution *solution = new Solution();
cout<<solution->calculate(s);
return 0;
}