-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicCalculator3.cpp
45 lines (35 loc) · 967 Bytes
/
BasicCalculator3.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
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
int calculate(string s) {
vector<int> sign(2, 1);
int result = 0;
for(int i=0;i<s.length();i++){
if(s[i] == ' ') continue;
if(s[i] >= '0' && s[i] <= '9'){
int num = 0;
while(i < s.length() && s[i] >= '0' && s[i] <= '9'){
num = num * 10 + s[i++] - '0';
}
i --;
result += sign.back() * num;
sign.pop_back();
}else if(s[i] == ')') sign.pop_back();
else{
sign.push_back(sign.back() * (s[i] == '-' ? -1 : 1));
}
}
return result;
}
};
int main(){
string s;
getline(cin, s);
//cout<<s;
Solution *solution = new Solution();
cout<<solution->calculate(s);
return 0;
}