-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1935.cpp
61 lines (46 loc) · 1.09 KB
/
P1935.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 <stack>
using namespace std;
double value[26] = {};
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(2);
int N;
string exp;
cin >> N >> exp;
for (int i = 0; i < N; i++) {
cin >> value[i];
}
stack<double> stack;
for (int i = 0; i < exp.length(); i++) {
if (exp[i] != '*' && exp[i] != '+' && exp[i] != '/' && exp[i] != '-') {
stack.push(value[exp[i] - 'A']);
continue;
}
double one = stack.top();
stack.pop();
double two = stack.top();
stack.pop();
if (exp[i] == '*') {
stack.push(one * two);
continue;
}
if (exp[i] == '+') {
stack.push(one + two);
continue;
}
if (exp[i] == '/') {
stack.push(two / one);
continue;
}
if (exp[i] == '-') {
stack.push(two - one);
continue;
}
}
cout << fixed << stack.top();
return 0;
}