include iostream include stack using namespace std int parse string in

 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <stack>
using namespace std;
/*
int parse(string input)
{
while(input.find('(')!=string::npos)
{
int start;
int len;
start = input.find('(');
stack<char> brackets;
brackets.push('(');
for(int i=start+1;i<input.length()+1;i++)
{
if (input[i]=='(') brackets.push('(');
if (input[i]==')') brackets.pop();
if (brackets.empty()) { len = start-i; break; }
}
string inbrackets;
input.copy(inbrackets,start+1,len-1);
input.replace(start,len,atoi(parse(inbrackets)));
}
}*/
struct oper
{
int priority;
char type;
};
struct op
{
int type;
int val;
}
int count(string input)
{
stack<op> operands;
stack<oper> operations;
int pos = 0;
while (pos<input.length())
{
int op =0;
while (isdigit(input[pos]))
{
op*=10;
op+=input[pos++]-48;
}
operands.push(op);
if (pos<input.length())
{
operations.push(input[pos++]);
}
}
int res=0;
//while (!operands.empty()) { cout<<operands.top() << " "; operands.pop(); }
//cout << endl;
//while (!operations.empty()) { cout<<operations.top() << " "; operations.pop(); }
while (!operations.empty())
{
char op = operations.top();
operations.pop();
int op1=operands.top();
operands.pop();
int op2=operands.top();
operands.pop();
switch(op)
{
case '+':
operands.push(op1+op2);
break;
case '-':
operands.push(op1-op2);
break;
case '*':
operands.push(op1*op2);
break;
}
}
return operands.top();
}
int main()
{
string input;
while (getline(cin,input))
{
cout << count(input) << endl;
}
}