Advertisement
vencinachev

PismenZad1

Jun 16th, 2021
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. int evaluate(queue<char> expr);
  8.  
  9. int main()
  10. {
  11.     queue<char> expression;
  12.     string input;
  13.     cout << "Enter expression: ";
  14.     cin >> input;
  15.     for (int i = 0; i < input.size(); i++)
  16.     {
  17.         expression.push(input[i]);
  18.     }
  19.     cout << "Result = " << evaluate(expression) << endl;
  20.     return 0;
  21. }
  22.  
  23. int evaluate(queue<char> expr)
  24. {
  25.     stack<int> nums;
  26.     stack<char> funcs;
  27.  
  28.     while (!expr.empty())
  29.     {
  30.         char sym = expr.front();
  31.         expr.pop();
  32.         if (sym == 'f' || sym == 'g')
  33.         {
  34.             funcs.push(sym);
  35.         }
  36.         else if (sym >= '0' && sym <= '9')
  37.         {
  38.             nums.push(sym - '0');
  39.         }
  40.         else if (sym == ')')
  41.         {
  42.             int result = 0;
  43.             int x = nums.top();
  44.             nums.pop();
  45.             if (funcs.top() == 'f')
  46.             {
  47.                 result = (x*x + 3*x + 7) % 10;
  48.             }
  49.             else if (funcs.top() == 'g')
  50.             {
  51.                 result = (3*x*x + 2*x + 4) % 10;
  52.             }
  53.             funcs.pop();
  54.             nums.push(result);
  55.         }
  56.     }
  57.     return nums.top();
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement