-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
71 lines (64 loc) · 1.74 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
// Author : Qi Zhang
// Date : 2018-12-11
#include <bits/stdc++.h>
using namespace std;
vector<string> mysplit(string &s, char t){
vector<string> info;
string cur;
for(auto c: s){
if(c == t){
if(cur != "") info.push_back(cur);
cur = "";
}
else cur += c;
}
if(cur != "") info.push_back(cur);
return info;
}
int main()
{
string line;
while (getline(cin, line)) {
vector<string> vv = mysplit(line, ' ');
stack<int> nums;
stack<string> ops;
bool err = false;
for(auto v: vv){
if(v == "+" || v == "-" || v == "*" || v == "/") ops.push(v);
else{
int val = stoi(v);
if(!ops.empty() && (ops.top() == "*" || ops.top() == "/")){
if(ops.top() == "*") {
int tmp = val * nums.top();
nums.pop();
nums.push(tmp);
}
if(ops.top() == "/") {
if(val == 0){
err = true;
break;
}
int tmp = nums.top() / val;
nums.pop();
nums.push(tmp);
}
ops.pop();
}
else nums.push(val);
}
}
if(err){
cout << "err" << endl;
continue;
}
int ans = 0;
while(!ops.empty()){
int b = nums.top(); nums.pop();
if(ops.top() == "+") ans += b;
else ans -= b;
ops.pop();
}
cout << ans + nums.top() << endl;
}
return 0;
}