Advertisement
cacodemon665

Reverse polish notation

May 19th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <unordered_map>
  5. #include <stack>
  6.  
  7. using namespace std;
  8.  
  9. bool isOperator(char c)
  10. {
  11.     return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')';
  12. }
  13.  
  14. string convert(string expr)
  15. {
  16.     stack<char> temp;
  17.     string result = "";
  18.  
  19.     expr.erase(remove_if(expr.begin(), expr.end(), isspace), expr.end());
  20.  
  21.     for (char ch : expr)
  22.     {
  23.         switch (ch)
  24.         {
  25.         case '+':
  26.         case '-':
  27.         {
  28.             while (!temp.empty() && temp.top() != '(')
  29.             {
  30.                 result.push_back(temp.top());
  31.                 temp.pop();
  32.             }
  33.  
  34.             temp.push(ch);
  35.             break;
  36.         }
  37.         case '/':
  38.         case '*':
  39.         {
  40.             while (!temp.empty() && temp.top() != '(' && temp.top() != '+' && temp.top() != '-') {
  41.                 result.push_back(temp.top());
  42.                 temp.pop();
  43.             }
  44.  
  45.             temp.push(ch);
  46.             break;
  47.         }
  48.         case '(': {
  49.             temp.push('(');
  50.             break;
  51.         }
  52.         case ')':
  53.         {
  54.             while (!temp.empty())
  55.             {
  56.                 char t = temp.top();
  57.                 temp.pop();
  58.  
  59.                 if (t != '(')
  60.                 {
  61.                     result.push_back(t);
  62.                 }
  63.                 else break;
  64.             }
  65.  
  66.             break;
  67.         }
  68.         default:
  69.             result += ch;
  70.         }
  71.     }
  72.  
  73.     while (!temp.empty())
  74.     {
  75.         result.push_back(temp.top());
  76.         temp.pop();
  77.     }
  78.  
  79.     return result;
  80. }
  81.  
  82. double eval(string expression, unordered_map<char, double> vars)
  83. {
  84.     stack<double> values;
  85.  
  86.     for (char c : expression)
  87.     {
  88.         if (!isOperator(c))
  89.         {
  90.             values.push(vars[c]);
  91.         }
  92.         else
  93.         {
  94.             double v1 = values.top();
  95.             values.pop();
  96.             double v2 = values.top();
  97.             values.pop();
  98.  
  99.             switch (c)
  100.             {
  101.             case '+': values.push(v2 + v1); break;
  102.             case '-': values.push(v2 - v1); break;
  103.             case '*': values.push(v2 * v1); break;
  104.             case '/': values.push(v2 / v1); break;
  105.             default:
  106.                 cerr << "Error!";
  107.             };
  108.         }
  109.     }
  110.  
  111.     return values.top();
  112. }
  113.  
  114. int main()
  115. {
  116.     unordered_map<char, double> vars;
  117.     string input; // test = "( 8 + 2 * 5 ) / ( 1 + 3 * 2 - 4 )";
  118.  
  119.     cout << "Enter string: " << endl;
  120.     cin >> input;
  121.  
  122.     for (char c = 'a'; c <= 'e'; c++)
  123.     {
  124.         cout << c << "=";
  125.         cin >> vars[c];
  126.     }
  127.  
  128.     string rpn = convert(input);
  129.  
  130.     std::cout << rpn << endl;
  131.     std::cout << eval(rpn, vars);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement