Advertisement
Derga

Untitled

Aug 1st, 2023
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. // CPP program to evaluate a given
  2. // expression where tokens are
  3. // separated by space.
  4. #include <stack>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. // Function to find precedence of
  10. // operators.
  11. int precedence(char op) {
  12.     if (op == '+' || op == '-')
  13.         return 1;
  14.     if (op == '*' || op == '/')
  15.         return 2;
  16.     return 0;
  17. }
  18.  
  19. // Function to perform arithmetic operations.
  20. int applyOp(int a, int b, char op) {
  21.     switch (op) {
  22.     case '+': return a + b;
  23.     case '-': return a - b;
  24.     case '*': return a * b;
  25.     case '/': return a / b;
  26.     }
  27. }
  28.  
  29. // Function that returns value of
  30. // expression after evaluation.
  31. int evaluate(string tokens) {
  32.     int i;
  33.  
  34.     // stack to store integer values.
  35.     stack <int> values;
  36.  
  37.     // stack to store operators.
  38.     stack <char> ops;
  39.  
  40.     for (i = 0; i < tokens.length(); i++) {
  41.  
  42.         // Current token is a whitespace,
  43.         // skip it.
  44.         if (tokens[i] == ' ')
  45.             continue;
  46.  
  47.         // Current token is an opening
  48.         // brace, push it to 'ops'
  49.         else if (tokens[i] == '(') {
  50.             ops.push(tokens[i]);
  51.         }
  52.  
  53.         // Current token is a number, push
  54.         // it to stack for numbers.
  55.         else if (isdigit(tokens[i])) {
  56.             int val = 0;
  57.  
  58.             // There may be more than one
  59.             // digits in number.
  60.             while (i < tokens.length() &&
  61.                 isdigit(tokens[i]))
  62.             {
  63.                 val = (val * 10) + (tokens[i] - '0');
  64.                 i++;
  65.             }
  66.  
  67.             values.push(val);
  68.  
  69.             // right now the i points to
  70.             // the character next to the digit,
  71.             // since the for loop also increases
  72.             // the i, we would skip one
  73.             //  token position; we need to
  74.             // decrease the value of i by 1 to
  75.             // correct the offset.
  76.             i--;
  77.         }
  78.  
  79.         // Closing brace encountered, solve
  80.         // entire brace.
  81.         else if (tokens[i] == ')')
  82.         {
  83.             while (!ops.empty() && ops.top() != '(')
  84.             {
  85.                 int val2 = values.top();
  86.                 values.pop();
  87.  
  88.                 int val1 = values.top();
  89.                 values.pop();
  90.  
  91.                 char op = ops.top();
  92.                 ops.pop();
  93.  
  94.                 values.push(applyOp(val1, val2, op));
  95.             }
  96.  
  97.             // pop opening brace.
  98.             if (!ops.empty())
  99.                 ops.pop();
  100.         }
  101.  
  102.         // Current token is an operator.
  103.         else
  104.         {
  105.             // While top of 'ops' has same or greater
  106.             // precedence to current token, which
  107.             // is an operator. Apply operator on top
  108.             // of 'ops' to top two elements in values stack.
  109.             while (!ops.empty() && precedence(ops.top())
  110.                 >= precedence(tokens[i])) {
  111.                 int val2 = values.top();
  112.                 values.pop();
  113.  
  114.                 int val1 = values.top();
  115.                 values.pop();
  116.  
  117.                 char op = ops.top();
  118.                 ops.pop();
  119.  
  120.                 values.push(applyOp(val1, val2, op));
  121.             }
  122.  
  123.             // Push current token to 'ops'.
  124.             ops.push(tokens[i]);
  125.         }
  126.     }
  127.  
  128.     // Entire expression has been parsed at this
  129.     // point, apply remaining ops to remaining
  130.     // values.
  131.     while (!ops.empty()) {
  132.         int val2 = values.top();
  133.         values.pop();
  134.  
  135.         int val1 = values.top();
  136.         values.pop();
  137.  
  138.         char op = ops.top();
  139.         ops.pop();
  140.  
  141.         values.push(applyOp(val1, val2, op));
  142.     }
  143.  
  144.     // Top of 'values' contains result, return it.
  145.     return values.top();
  146. }
  147.  
  148. int main() {
  149.     cout << evaluate("10 + 2 * 6") << "\n";
  150.     cout << evaluate("100 * 2 + 12") << "\n";
  151.     cout << evaluate("100 * ( 2 + 12 )") << "\n";
  152.     cout << evaluate("100 * ( 2 + 12 ) / 14") << '\n';
  153.     cout << evaluate("(41 + 5) * 10") << '\n';
  154.     return 0;
  155. }
  156.  
  157. // This code is contributed by Nikhil jindal.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement