-
Notifications
You must be signed in to change notification settings - Fork 0
/
1935.cpp
57 lines (49 loc) · 1.2 KB
/
1935.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
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
cin.ignore();
string s;
getline(cin, s);
vector<int> operands;
while (N-- > 0) {
int operand;
cin >> operand;
operands.push_back(operand);
}
stack<double> st;
for (string::size_type i = 0; i < s.size(); ++i) {
string::value_type ch = s[i];
if ('A' <= ch && ch <= 'Z') {
st.push(operands[ch - 'A']);
} else {
double rhs = st.top();
st.pop();
double lhs = st.top();
st.pop();
double result = 0;
switch (ch) {
case '+':
result = lhs + rhs;
break;
case '-':
result = lhs - rhs;
break;
case '*':
result = lhs * rhs;
break;
case '/':
result = lhs / rhs;
break;
}
st.push(result);
}
}
cout << fixed << setprecision(2) << st.top();
return 0;
}