Ser1ousSAM

polishNotation

Sep 26th, 2022 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <queue>
  4. #include <string>
  5.  
  6. using std::string;
  7. using std::stack;
  8. using std::queue;
  9.  
  10. class reversePolishNotation {
  11.     //rotten code (int, NULL or NULL, char)
  12.     queue <std::pair<int, char>> polishNotationQueue;
  13.     stack <char> operationsStack;
  14.  
  15.     int getPriority(char c) {
  16.         switch (c)
  17.         {
  18.         case '^':
  19.             return 3;
  20.         case '/':
  21.             return 2;
  22.         case '*':
  23.             return 2;
  24.         case '+':
  25.             return 1;
  26.         case '-':
  27.             return 1;
  28.         case '(':
  29.             return 0;
  30.         case ')':
  31.             return 0;
  32.         default:
  33.             return -1;
  34.         }
  35.     }
  36.     int resOfOperation(char c, int first, int second) {
  37.         switch (c)
  38.         {
  39.         case '*' :
  40.             return second * first;
  41.         case '-':
  42.             return second - first;
  43.         case '+':
  44.             return second + first;
  45.         case '/':
  46.             return second / first;
  47.         case '^':
  48.             return pow(second, first);
  49.         default:
  50.             return 0;
  51.         }
  52.     }
  53. public:
  54.     //11-2+3^2+2*(3-4*5^2-6)^2 = 21236
  55.     //11 2 - 3 2 ^ + 2 3 4 5 2 ^ * - 6 - 2 ^ * +
  56.     queue<std::pair<int, char>> TransleteToPolishNotaion(string expression) {
  57.         string tempValue = "";
  58.         for (int i = 0; i < expression.size(); i++) {
  59.             if (i == 7)
  60.                 i = 7;
  61.             if (48 <= int(expression[i]) && int(expression[i]) <= 57) {
  62.                 tempValue += expression[i];
  63.                 if(i==expression.size()-1)
  64.                     polishNotationQueue.push(std::make_pair(stoi(tempValue), NULL));
  65.             }
  66.             else {
  67.                 if (tempValue!="")
  68.                     polishNotationQueue.push(std::make_pair(stoi(tempValue), NULL));
  69.                 tempValue = "";
  70.                 if (expression[i] == '(')
  71.                     operationsStack.push('(');
  72.                 else if(expression[i] == ')'){
  73.                     while (operationsStack.top() != '(') {
  74.                         polishNotationQueue.push(std::make_pair(NULL, operationsStack.top()));
  75.                         operationsStack.pop();
  76.                     }
  77.                     operationsStack.pop();
  78.                 }
  79.                 else {
  80.                     if (!operationsStack.empty()) {
  81.                         while (getPriority(operationsStack.top()) > getPriority(expression[i]) || (getPriority(operationsStack.top()) == getPriority(expression[i]) && expression[i]!='^')) {
  82.                             polishNotationQueue.push(std::make_pair(NULL, operationsStack.top()));
  83.                             operationsStack.pop();
  84.                             if (operationsStack.empty())
  85.                                 break;
  86.                         }
  87.                     }
  88.                     operationsStack.push(expression[i]);
  89.                 }    
  90.             }
  91.         }
  92.         while (!operationsStack.empty()) {
  93.             polishNotationQueue.push(std::make_pair(NULL, operationsStack.top()));
  94.             operationsStack.pop();
  95.         }
  96.         return polishNotationQueue;
  97.     }
  98.  
  99.     void PresentAsPolishNotation(string expression) {
  100.         TransleteToPolishNotaion(expression);
  101.         queue <std::pair<int, char>> tempQueue = polishNotationQueue;
  102.         while (!tempQueue.empty()) {
  103.             if (tempQueue.front().first != NULL)
  104.                 std::cout << tempQueue.front().first << " ";
  105.             else
  106.                 std::cout << tempQueue.front().second << " ";
  107.             tempQueue.pop();
  108.         }
  109.         std::cout << '\n';
  110.     }
  111.  
  112.     int CalculateExpression(string expression) {
  113.         queue <std::pair<int, char>> tempQueue = polishNotationQueue;
  114.         stack <int> tempStack;
  115.         int firstTop, secondTop;
  116.         while (!tempQueue.empty()) {
  117.             if (tempQueue.front().first != NULL)
  118.                 tempStack.push(tempQueue.front().first);
  119.             else {
  120.                 firstTop = tempStack.top();
  121.                 tempStack.pop();
  122.                 secondTop = tempStack.top();
  123.                 tempStack.pop();
  124.                 tempStack.push(resOfOperation(tempQueue.front().second, firstTop, secondTop));
  125.             }
  126.             tempQueue.pop();
  127.         }
  128.         return tempStack.top();
  129.     }
  130. };
  131.  
  132. int main()
  133. {
  134.     string input;
  135.     std::cin >> input;
  136.     reversePolishNotation p;
  137.     p.PresentAsPolishNotation(input);
  138.     std::cout << p.CalculateExpression(input);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment