-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1935.cc
53 lines (42 loc) · 1018 Bytes
/
P1935.cc
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
#include <iostream>
#include <iomanip>
#include <array>
#include <stack>
using namespace std;
array<double, 26> names{};
double process(stack<char>& program) {
char top = program.top();
program.pop();
if (top >= 'A' && top <= 'Z') {
return names[top - 'A'];
}
auto B = process(program);
auto A = process(program);
switch(top) {
case '+':
return A + B;
case '-':
return A - B;
case '/':
return A / B;
case '*':
return A * B;
default:
exit(-1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
stack<char> program;
int many = 0;
cin >> many;
cin.ignore();
string raw_program;
getline(cin, raw_program);
for (auto a: raw_program) program.push(a);
raw_program.clear(); // put expression to stack
for(int i = 0; i < many; i++) cin >> names[i];
cout << fixed << setprecision(2);
cout << process(program);
}