-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicCalculator227.cpp
More file actions
83 lines (81 loc) · 2.14 KB
/
basicCalculator227.cpp
File metadata and controls
83 lines (81 loc) · 2.14 KB
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
class Solution {
public:
int calculate(string s) {
stack<char>op;
stack<int>num;
s.push_back('$');
op.push('+');
int number = 0, ans = 0;
for (int i = 0; i < s.size(); i++) {
if (isdigit(s[i])) {
number = number*10 + s[i]-'0';
} else if (isspace(s[i])) continue;
else {
if (op.top() == '*') {
number*=num.top();
op.pop();
number.pop();
} else if (op.top() == '/') {
number = num.top()/number;
op.pop();
number.pop();
}
num.push(number);
number = 0;
op.push(s[i]);
}
}
op.pop();
while (!op.empty()) {
if (op.top() == '-') {
ans-=num.top();
} else {
ans+=num.top();
}
num.pop();op.pop();
}
return ans;
}
};
//the fatest method
static const auto speedup = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return 0; }();
class Solution {
public:
int calculate(string s) {
stack<int> st;
int num = 0, res = 0;
char sign = '+';
for (int i = 0; i < s.length(); i++) {
// cout << "loop char[" << i << "]: " << s[i] << endl;
if (isdigit(s[i])) {
num = num*10 + s[i] - '0';
// cout << "-- it's a digit. num: " << num << endl;
}
if ((!isdigit(s[i]) && s[i] != ' ') || i == s.length() - 1) {
if (sign == '+')
st.push(num);
else if (sign == '-')
st.push(-num);
else if (sign == '*') {
num *= st.top();
st.pop();
st.push(num);
}
else if (sign == '/') {
num = st.top() / num;
st.pop();
st.push(num);
}
sign = s[i];
num = 0;
// cout << "-- inserted: " << st.top() << endl;
}
}
while (!st.empty()) {
// cout << "stack element: " << st.top() << endl;
res += st.top();
st.pop();
}
return res;
}
};