Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2.  
  3. #include <cmath>
  4. #include <string>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <map>
  8. #include <vector>
  9. #include <algorithm>
  10.  
  11. class Figure
  12. {
  13. public:
  14.     virtual double get_square() const = 0;
  15.  
  16.     virtual ~Figure() = default;
  17. };
  18.  
  19. class Circle : public Figure
  20. {
  21. private:
  22.     double r = 0;
  23. public:
  24.     Circle(double r) : r(r) {}
  25.  
  26.     static Circle *make(const std::string &str)
  27.     {
  28.         std::stringstream ss;
  29.         ss << str;
  30.         double r;
  31.         ss >> r;
  32.         Circle *tmp = new Circle(r);
  33.         return tmp;
  34.     }
  35.  
  36.     double get_square() const override
  37.     {
  38.         return r * r * M_PI;
  39.     }
  40. };
  41.  
  42. class Rectangle : public Figure
  43. {
  44. private:
  45.     double a = 0;
  46.     double b = 0;
  47. public:
  48.     Rectangle(double a, double b) : a(a), b(b) {}
  49.  
  50.     static Rectangle *make(const std::string &str)
  51.     {
  52.         std::stringstream ss;
  53.         ss << str;
  54.         double a, b;
  55.         ss >> a >> b;
  56.         Rectangle *tmp = new Rectangle(a, b);
  57.         return tmp;
  58.     }
  59.  
  60.     double get_square() const override
  61.     {
  62.         return a * b;
  63.     }
  64. };
  65.  
  66. class Square : public Figure
  67. {
  68. private:
  69.     double a = 0;
  70. public:
  71.     explicit Square(double a) : a(a) {}
  72.  
  73.     static Square *make(const std::string &str)
  74.     {
  75.         std::stringstream ss;
  76.         ss << str;
  77.         double a = 0;
  78.         ss >> a;
  79.         Square *tmp = new Square(a);
  80.         return tmp;
  81.     }
  82.  
  83.     double get_square() const override
  84.     {
  85.         return a * a;
  86.     }
  87. };
  88.  
  89.  
  90. class FigCreator
  91. {
  92. public:
  93.     virtual Figure *create(const std::string &str) = 0;
  94.  
  95.     virtual ~FigCreator() = default;
  96. };
  97.  
  98. class CircleCreator : public FigCreator
  99. {
  100. public:
  101.     Figure *create(const std::string &str) override
  102.     {
  103.         return Circle::make(str);
  104.     }
  105.  
  106. };
  107.  
  108. class RectangleCreator : public FigCreator
  109. {
  110. public:
  111.     Figure *create(const std::string &str) override
  112.     {
  113.         return Rectangle::make(str);
  114.     }
  115. };
  116.  
  117. class SquareCreator : public FigCreator
  118. {
  119. public:
  120.     Figure *create(const std::string &str) override
  121.     {
  122.         return Square::make(str);
  123.     }
  124. };
  125.  
  126.  
  127. class AbstractFactory
  128. {
  129. public:
  130.  
  131.     static AbstractFactory &factory_instance()//Создаю синглтон - объект, к которому можно обращаться через
  132.     {                                         //AbstractFactory::factory_instance()
  133.         static AbstractFactory s;
  134.         return s;
  135.     }
  136.  
  137.     Figure *create(const std::string &type, const std::string &params)//Просто функция, создающая объекты поклассов
  138.     {                                                                 // Figure
  139.         return disc[type]->create(params);
  140.     }
  141.  
  142. private:
  143.     std::map<std::string, FigCreator *> disc = {//FigCreator - это некий абстрактый класс, который выступает в роли абстрактного класса
  144.             {"C", new CircleCreator()},         //для классов-создателей круга, квадрата и прямоугольника(код их очень прост)
  145.             {"S", new SquareCreator()},
  146.             {"R", new RectangleCreator()},
  147.     };
  148.  
  149.     AbstractFactory() = default;
  150.  
  151.     ~AbstractFactory()
  152.     {
  153.         for (auto &c:disc) {// надо правильно удалить map, потому что его значения выделены в динамической памяти
  154.             delete c.second;
  155.         }
  156.     }
  157.  
  158.     AbstractFactory(AbstractFactory const &) = delete;
  159.  
  160.     AbstractFactory &operator=(AbstractFactory const &) = delete;
  161. };
  162.  
  163. int main()
  164. {
  165.     std::vector<Figure *> figures;
  166.     std::string tmp;
  167.     while (!std::getline(std::cin, tmp).eof()) {
  168.         std::stringstream s;
  169.         s << tmp;
  170.         std::string type, param1, param2;
  171.         s >> type >> param1 >> param2;
  172.         figures.push_back(AbstractFactory::factory_instance().create(type, param1 + " " + param2));
  173.     }
  174.  
  175.     std::stable_sort(figures.begin(), figures.end(),
  176.                      [](Figure *a, Figure *b) { return a->get_square() < b->get_square(); });
  177.  
  178.     for (auto &c:figures) {
  179.         std::cout << c->to_string() << std::endl;
  180.         delete c;
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement