blob: fde664c09fdc337ebc7d1698a5f68001030befcf (
plain)
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
|
#include <iostream>
#include "stack.h"
using namespace std;
int main()
{
int n;
cout << "n = ";
cin >> n;
stack *s = NULL;
char* buf = new char[32];
for (int i = 0; i < n; i++)
{
cin >> buf;
if ('0' <= buf[0] && buf[0] <= '9')
{
int i = atoi(buf);
push(s, i);
}
else
{
switch (buf[0])
{
case '+': {
int o1 = pop(s);
int o2 = pop(s);
push(s, o2 + o1);
} break;
case '-': {
int o1 = pop(s);
int o2 = pop(s);
push(s, o2 - o1);
} break;
case '*': {
int o1 = pop(s);
int o2 = pop(s);
push(s, o2 * o1);
} break;
}
}
}
int result = pop(s);
cout << "Result = " << result << endl;
return 0;
}
|