Advertisement
RobertDeMilo

BB4.2 shared_ptr в дереве выражения

Jun 13th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.71 KB | None | 0 0
  1. //#include<iostream>
  2. //#include<memory>
  3. //#include<string>
  4. //
  5. //using namespace std;
  6. //
  7. ////Базовый класс арифметического выражения
  8. //class Expression
  9. //{
  10. //public:
  11. //  virtual ~Expression() = default;
  12. //  virtual int  Evaluate() const = 0;
  13. //  virtual string ToString() const = 0;
  14. //};
  15. //
  16. //using ExpressionPtr = unique_ptr<Expression>;
  17. //
  18. ////Класс, представляющий конкретное число - лист дерева выражения
  19. //
  20. //class ValueExpr: public Expression
  21. //{
  22. //public:
  23. //  ValueExpr(int value) : value_(value) {}
  24. //  int Evaluate() const override
  25. //  {
  26. //      return value_;
  27. //  }
  28. //  string ToString() const override
  29. //  {
  30. //      return to_string(value_);
  31. //  }
  32. //private:
  33. //  int value_;
  34. //};
  35. //
  36. //class BinaryExpr : public Expression
  37. //{
  38. //public:
  39. //  BinaryExpr(ExpressionPtr left, ExpressionPtr right) :
  40. //      left_(move(left)),
  41. //      right_(move(right)) {}
  42. //
  43. //  string ToString() const final
  44. //  {
  45. //      return '(' + left_->ToString() + ')'+
  46. //          GetSymbol()+
  47. //      '(' + right_->ToString() + ')';
  48. //  }
  49. //
  50. //  int Evaluate() const final
  51. //  {
  52. //      return EvaluateOnValues(left_->Evaluate(), right_->Evaluate());
  53. //  }
  54. //
  55. //private:
  56. //
  57. //  virtual char GetSymbol() const = 0;
  58. //  virtual int EvaluateOnValues(int l, int r) const = 0;
  59. //
  60. //  ExpressionPtr left_;
  61. //  ExpressionPtr right_;
  62. //};
  63. //
  64. //
  65. //class ProductExpr : public BinaryExpr
  66. //{
  67. //public:
  68. //  ProductExpr(ExpressionPtr left, ExpressionPtr right) :
  69. //      BinaryExpr(move(left), move(right)) {}
  70. //private:
  71. //  char GetSymbol() const override
  72. //  {
  73. //      return '*';
  74. //  }
  75. //  int EvaluateOnValues(int left, int right) const override
  76. //  {
  77. //      return left * right;
  78. //  }
  79. //};
  80. //
  81. //class SumExpr : public BinaryExpr
  82. //{
  83. //public:
  84. //  using BinaryExpr::BinaryExpr;
  85. //private:
  86. //  char GetSymbol() const override
  87. //  {
  88. //      return '+';
  89. //  }
  90. //  int EvaluateOnValues(int left, int right) const override
  91. //  {
  92. //      return left + right;
  93. //  }
  94. //
  95. //};
  96. //
  97. //ExpressionPtr Value(int value)
  98. //{
  99. //  return make_unique<ValueExpr>(value);
  100. //}
  101. //
  102. //ExpressionPtr Sum(ExpressionPtr left, ExpressionPtr right)
  103. //{
  104. //  return make_unique<SumExpr>(move(left), move(right));
  105. //}
  106. //
  107. //ExpressionPtr Product(ExpressionPtr left, ExpressionPtr right)
  108. //{
  109. //  return make_unique<ProductExpr>(move(left), move(right));
  110. //}
  111. //
  112. //void Print(const Expression* e)
  113. //{
  114. //  if (!e)
  115. //  {
  116. //      cout << "Null expression provided" << endl;
  117. //      return;
  118. //  }
  119. //  cout << e->ToString() << " = " << e->Evaluate() << endl;
  120. //}
  121. //
  122. //int main()
  123. //{
  124. //
  125. //  ExpressionPtr e1 = Product(Value(2), Sum(Value(3), Value(4)));
  126. //  Print(e1.get());
  127. //
  128. //  ExpressionPtr e2 = Sum(move(e1),Value(5));
  129. //  Print(e2.get());
  130. //
  131. //  Print(e1.get());
  132. //
  133. //  return 0;
  134. //}
  135.  
  136. #include<iostream>
  137. #include<memory>
  138. #include<string>
  139.  
  140. using namespace std;
  141.  
  142. //Базовый класс арифметического выражения
  143. class Expression
  144. {
  145. public:
  146.     virtual ~Expression() = default;
  147.     virtual int  Evaluate() const = 0;
  148.     virtual string ToString() const = 0;
  149. };
  150.  
  151. using ExpressionPtr = shared_ptr<Expression>;
  152.  
  153. //Класс, представляющий конкретное число - лист дерева выражения
  154.  
  155. class ValueExpr : public Expression
  156. {
  157. public:
  158.     ValueExpr(int value) : value_(value) {}
  159.     int Evaluate() const override
  160.     {
  161.         return value_;
  162.     }
  163.     string ToString() const override
  164.     {
  165.         return to_string(value_);
  166.     }
  167. private:
  168.     int value_;
  169. };
  170.  
  171. class BinaryExpr : public Expression
  172. {
  173. public:
  174.     BinaryExpr(ExpressionPtr left, ExpressionPtr right) :
  175.         left_(move(left)),
  176.         right_(move(right)) {}
  177.  
  178.     string ToString() const final
  179.     {
  180.         return '(' + left_->ToString() + ')' +
  181.             GetSymbol() +
  182.             '(' + right_->ToString() + ')';
  183.     }
  184.  
  185.     int Evaluate() const final
  186.     {
  187.         return EvaluateOnValues(left_->Evaluate(), right_->Evaluate());
  188.     }
  189.  
  190. private:
  191.  
  192.     virtual char GetSymbol() const = 0;
  193.     virtual int EvaluateOnValues(int l, int r) const = 0;
  194.  
  195.     ExpressionPtr left_;
  196.     ExpressionPtr right_;
  197. };
  198.  
  199.  
  200. class ProductExpr : public BinaryExpr
  201. {
  202. public:
  203.     ProductExpr(ExpressionPtr left, ExpressionPtr right) :
  204.         BinaryExpr(move(left), move(right)) {}
  205. private:
  206.     char GetSymbol() const override
  207.     {
  208.         return '*';
  209.     }
  210.     int EvaluateOnValues(int left, int right) const override
  211.     {
  212.         return left * right;
  213.     }
  214. };
  215.  
  216. class SumExpr : public BinaryExpr
  217. {
  218. public:
  219.     using BinaryExpr::BinaryExpr;
  220. private:
  221.     char GetSymbol() const override
  222.     {
  223.         return '+';
  224.     }
  225.     int EvaluateOnValues(int left, int right) const override
  226.     {
  227.         return left + right;
  228.     }
  229.  
  230. };
  231.  
  232. ExpressionPtr Value(int value)
  233. {
  234.     return make_shared<ValueExpr>(value);
  235. }
  236.  
  237. ExpressionPtr Sum(ExpressionPtr left, ExpressionPtr right)
  238. {
  239.     return make_shared<SumExpr>(move(left), move(right));
  240. }
  241.  
  242. ExpressionPtr Product(ExpressionPtr left, ExpressionPtr right)
  243. {
  244.     return make_shared<ProductExpr>(move(left), move(right));
  245. }
  246.  
  247. void Print(const Expression* e)
  248. {
  249.     if (!e)
  250.     {
  251.         cout << "Null expression provided" << endl;
  252.         return;
  253.     }
  254.     cout << e->ToString() << " = " << e->Evaluate() << endl;
  255. }
  256.  
  257. int main()
  258. {
  259.  
  260.     ExpressionPtr e1 = Product(Value(2), Sum(Value(3), Value(4)));
  261.     Print(e1.get());
  262.  
  263.     ExpressionPtr e2 = Sum(e1, Value(5));
  264.     Print(e2.get());
  265.  
  266.     Print(e1.get());
  267.  
  268.     ExpressionPtr e3 = Sum(e1, e2);
  269.     Print(e3.get());
  270.  
  271.     return 0;
  272. }
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement