include iostream include stack include queue include vector include ma

  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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cstdlib>
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 lexem
{
string lex;
string type;
};
queue<lexem> split_to_lexems(string input)
{
queue<lexem> series;
string lex;
int dgt=0,opr=0,min=0;;
for(int i=0;i<input.length();i++)
{
if (!dgt&&!opr)
{
if (isdigit(input[i]))
{
dgt=1;
lex=input[i];
}
else if(input[i]=='(' || input[i]==')')
{
lex = input[i];
lexem l;
l.lex = lex;
l.type="bracket";
series.push(l);
lex.clear();
}
else
{
opr=1;
lex=input[i];
}
}
else if(dgt)
{
if (isdigit(input[i]))
{
lex+=input[i];
}
else
{
if (min) { lex="-"+lex; min=0; }
lexem l;
l.lex = lex;
l.type = "number";
series.push(l);
lex.clear();
dgt=0;
i-=1;
}
}
else if(opr)
{
if (isdigit(input[i]) || input[i]=='(' || input[i]==')')
{
opr=0;
lexem l;
l.type = "operation";
if (lex.length()>1 && lex[0]=='-')
{
min=1;
lex.erase(0,1);
l.lex=lex;
series.push(l);
}
else if (lex.length()>1 && lex[lex.length()-1]=='-')
{
min=1;
lex.erase(lex.length()-1,1);
l.lex=lex;
series.push(l);
}
else { l.lex = lex; series.push(l); }
lex.clear();
i-=1;
}
else
{
lex+=input[i];
}
}
}
if (lex.length())
{
lexem l;
l.lex = lex;
if (dgt) { l.type="number";
if (min) l.lex="-"+l.lex; }
else if(opr) l.type="operation";
else l.type="bracket";
series.push(l);
}
return series;
}
queue<lexem> to_postfix(queue<lexem> series)//string input)
{
map<string,int> prior;
prior["+"]=0;
prior["-"]=0;
prior["*"]=1;
prior["/"]=1;
prior["^"]=2;
//queue<lexem> series = split_to_lexems(input);
queue<lexem> output;
stack<lexem> stk;
while(series.size())
{
if (series.front().type=="number")
{
output.push(series.front());
series.pop();
}
else if (series.front().type=="bracket")
{
if (series.front().lex=="(")
{
stk.push(series.front());
series.pop();
}
else if (series.front().lex==")")
{
while (!(stk.top().type=="bracket"))
{
output.push(stk.top());
stk.pop();
}
stk.pop();
//if (stk.top().lex=="(") {cout << stk.size() << endl; stk.pop(); }
//stk.pop();
//if(!stk.empty()) stk.pop();
series.pop();
}
}
else if (series.front().type=="operation")
{
while (!stk.empty() &&
prior[stk.top().lex]>=prior[series.front().lex]
&& stk.top().type=="operation")
{
output.push(stk.top());
stk.pop();
}
stk.push(series.front());
series.pop();
}
else series.pop();
}
while (!stk.empty())
{
output.push(stk.top());
stk.pop();
}
return output;/*
while (!output.empty())
{
cout << output.front().lex << " ";
output.pop();
}
cout << endl;
return series.size();*/
}
int add(int a, int b)
{ return a+b; }
int sub(int a, int b)
{ return a-b; }
int fdiv(int a, int b)
{ return a/b; }
int mul(int a, int b)
{ return a*b; }
int calc(string input)
{
map<string,int(*)(int,int)> ops;
ops["+"]=add;
ops["-"]=sub;
ops["*"]=mul;
queue<lexem> series = to_postfix(split_to_lexems(input));
stack<int> result;
while (series.size())
{
if (series.front().type=="number")
{
result.push(atoi(series.front().lex.c_str()));
series.pop();
}
else if (series.front().type=="operation")
{
int op1 = result.top();
result.pop();
int op2 = result.top();
result.pop();
result.push(ops[series.front().lex](op2,op1));
series.pop();
}
}
return result.top();
}
/*
int count(string input)
{
map<string,int> priority;
priority["+"]=0;
priority["-"]=0;
priority["*"]=1;
priority["/"]=1;
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 << calc(input) << endl;
}
}