Advertisement
Kwwiker

Untitled

Apr 14th, 2021
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. int preCalc(string line) {
  2.     Stack stack(line.length());
  3.     for (int i = line.length() - 1; i >= 0; i--) {
  4.         if (line[i] >= '0' and line[i] <= '9') {
  5.             stack.push(line[i] - '0');
  6.         }
  7.         else {
  8.             int x, y;
  9.             switch (line[i])
  10.             {
  11.             case '+':
  12.                 x = stack.peek();
  13.                 stack.pop();
  14.                 y = stack.peek();
  15.                 stack.pop();
  16.                 stack.push(x + y);
  17.                 break;
  18.             case '-':
  19.                 x = stack.peek();
  20.                 stack.pop();
  21.                 y = stack.peek();
  22.                 stack.pop();
  23.                 stack.push(x - y);
  24.                 break;
  25.             case '*':
  26.                 x = stack.peek();
  27.                 stack.pop();
  28.                 y = stack.peek();
  29.                 stack.pop();
  30.                 stack.push(x * y);
  31.                 break;
  32.             case '/':
  33.                 x = stack.peek();
  34.                 stack.pop();
  35.                 y = stack.peek();
  36.                 stack.pop();
  37.                 stack.push(x / y);
  38.                 break;
  39.             default:
  40.                 break;
  41.             }
  42.         }
  43.     }
  44.     return stack.peek();
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement