-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostfix-notation.cpp
More file actions
57 lines (51 loc) · 1.12 KB
/
postfix-notation.cpp
File metadata and controls
57 lines (51 loc) · 1.12 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
// This code was created with the support of Sergo.
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
typedef long long ll;
typedef double ld;
typedef unsigned long long ull;
using namespace std;
void file() {
freopen("postfix.in", "r", stdin);
freopen("postfix.out", "w", stdout);
}
// void ...
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
file();
char x;
int ans[1000000 + 1], j = 0;
while(cin >> x) {
if(x >= '0' && x <= '9') {
ans[j++] = x - '0';
}
else if(x != ' '){
j--;
int y = ans[j--];
int q = ans[j];
if(x == '+') {
ans[j] = y + q;
j++;
ans[j] = 0;
}
else if(x == '*') {
ans[j] = y * q;
j++;
ans[j] = 0;
}
else if(x == '-') {
ans[j] = q - y;
j++;
ans[j] = 0;
}
}
}
cout << ans[0] << endl;
}