223 {
224 int x;
225 char y;
226 unordered_map<char, int> priority = {
227 {'+', 1},
228 {'-', 1},
229 {'*', 2},
230 {'/', 2}};
231 stack<char> op = stack<char>();
232 stack<int> num = stack<int>();
233 while(!cin.eof() || !cin.fail() || !cin.bad()) {
234 if(cin.peek() == EOF)
235 break;
236 if(isdigit(cin.peek())) {
237 cin >> x;
238 num.push(x);
239 } else {
240 cin >> y;
241 if(y == '(') {
242 op.push(y);
243 } else if(y == ')') {
244 while(!op.empty() && op.top() != '(') {
245 int b = num.top();
246 num.pop();
247 int a = num.top();
248 num.pop();
249 char c = op.top();
250 op.pop();
251 if(c == '+') {
252 num.push(a + b);
253 } else if(c == '-') {
254 num.push(a - b);
255 } else if(c == '*') {
256 num.push(a * b);
257 } else if(c == '/') {
258 num.push(a / b);
259 }
260 }
261 op.pop();
262 } else if(priority.find(y) != priority.end()) {
263 while(!op.empty() && op.top() != '(' && priority[op.top()] >= priority[y]) {
264 int b = num.top();
265 num.pop();
266 int a = num.top();
267 num.pop();
268 char c = op.top();
269 op.pop();
270 if(c == '+') {
271 num.push(a + b);
272 } else if(c == '-') {
273 num.push(a - b);
274 } else if(c == '*') {
275 num.push(a * b);
276 } else if(c == '/') {
277 num.push(a / b);
278 }
279 }
280 op.push(y);
281 }
282 }
283 }
284 while(!op.empty()) {
285 int b = num.top();
286 num.pop();
287 int a = num.top();
288 num.pop();
289 char c = op.top();
290 op.pop();
291 if(c == '+') {
292 num.push(a + b);
293 } else if(c == '-') {
294 num.push(a - b);
295 } else if(c == '*') {
296 num.push(a * b);
297 } else if(c == '/') {
298 num.push(a / b);
299 }
300 }
301 cout << num.top();
302 return 0;
303 }