Advertisement
RobertDeMilo

YB5.11 Описание и обзор общего решения задачи

Apr 15th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4. #include <stack>
  5.  
  6. using namespace std;
  7.  
  8. class Node
  9. {
  10. public:
  11.     virtual int Evaluate() const = 0;
  12. };
  13.  
  14. class Value : public Node
  15. {
  16. public:
  17.     Value(char digit) : _value(digit - '0') {}
  18.  
  19.     int Evaluate() const override { return _value; }
  20.  
  21. private:
  22.     const int _value;
  23. };
  24.  
  25. class Variable : public Node
  26. {
  27. public:
  28.     Variable(const int& x) : _x(x) {}
  29.  
  30.     int Evaluate() const override { return _x; }
  31.  
  32. private:
  33.     const int& _x;
  34. };
  35.  
  36. class Operation : public Node
  37. {
  38. public:
  39.     Operation(char value) : precedence([value] {
  40.         if (value == '+')
  41.         {
  42.             return 2;
  43.         }
  44.         else
  45.         {
  46.             return 1;
  47.         }
  48.     }()), _op(value) {}
  49.  
  50.     const int precedence;
  51.  
  52.     int Evaluate() const override
  53.     {
  54.         if (_op == '*')
  55.         {
  56.             return _left->Evaluate() * _right->Evaluate();
  57.         }
  58.         else if (_op == '+')
  59.         {
  60.             return _left->Evaluate() + _right->Evaluate();
  61.         }
  62.         else if (_op == '-')
  63.         {
  64.             return _left->Evaluate() - _right->Evaluate();
  65.         }
  66.         return 0;
  67.     }
  68.  
  69.     void SetLeft(shared_ptr<Node> node)
  70.     {
  71.         _left = node;
  72.     }
  73.  
  74.     void SetRight(shared_ptr<Node> node)
  75.     {
  76.         _right = node;
  77.     }
  78.  
  79. private:
  80.     const char _op;
  81.     shared_ptr<Node> _left, _right;
  82. };
  83.  
  84. template <class Iterator>
  85. shared_ptr<Node> Parse(Iterator token, Iterator end, const int& x)
  86. {
  87.     if (token == end)
  88.     {
  89.         return make_shared<Value>('0');
  90.     }
  91.  
  92.     stack<shared_ptr<Node>> values;
  93.     stack<shared_ptr<Operation>> ops;
  94.  
  95.     auto PopOps = [&](int precedence) {
  96.  
  97.         while (!ops.empty() && ops.top()->precedence >= precedence)
  98.         {
  99.             auto value1 = values.top();
  100.             values.pop();
  101.  
  102.             auto value2 = values.top();
  103.             values.pop();
  104.  
  105.             auto op = ops.top();
  106.             ops.pop();
  107.  
  108.             op->SetRight(value1);
  109.             op->SetLeft(value2);
  110.  
  111.             values.push(op);
  112.         }
  113.     };
  114.  
  115.     while (token != end)
  116.     {
  117.         const auto& value = *token;
  118.  
  119.         if (value >= '0' && value <= '9')
  120.         {
  121.             values.push(make_shared<Value>(value));
  122.         }
  123.         else if (value == 'x')
  124.         {
  125.             values.push(make_shared<Variable>(x));
  126.         }
  127.         else if (value == '*')
  128.         {
  129.             PopOps(2);
  130.             ops.push(make_shared<Operation>(value));
  131.         }
  132.         else if (value == '+' || value == '-')
  133.         {
  134.             PopOps(1);
  135.             ops.push(make_shared<Operation>(value));
  136.         }
  137.         ++token;
  138.     }
  139.  
  140.     while (!ops.empty())
  141.     {
  142.         PopOps(0);
  143.     }
  144.     return values.top();
  145. }
  146.  
  147. int main()
  148. {
  149.     string tokens;
  150.  
  151.     cout << "Enter expression: ";
  152.     getline(cin, tokens);
  153.  
  154.     int x = 0;
  155.  
  156.     auto node = Parse(tokens.begin(), tokens.end(), x);
  157.  
  158.     cout << "Enter x: ";
  159.     while (cin >> x)
  160.     {
  161.         cout << "Expression value: " << node->Evaluate() << endl;
  162.         cout << "Enter x: ";
  163.     }
  164.  
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement