Advertisement
wowonline

Untitled

Apr 13th, 2022
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <algorithm>
  2. #include <sstream>
  3. #include <iostream>
  4. #include <map>
  5. #include <functional>
  6. #include <string>
  7. #include <vector>
  8. #include <memory>
  9.  
  10. // #include <math.h>
  11.  
  12. // class Figure {
  13. // private:
  14. //     double square;
  15. // public:
  16. //     Figure() {
  17. //         square = 0;
  18. //     }
  19. //     Figure(double sq) {
  20. //         square = sq;
  21. //     }
  22. //     virtual ~Figure() {}
  23.  
  24.  
  25. //     virtual double get_square() const {
  26. //         return square;
  27. //     };
  28.  
  29. // };
  30.  
  31. // class Rectangle : public Figure {
  32. // public:
  33. //     double edge1, edge2, square;
  34.  
  35. //     virtual double get_square() const {
  36. //         return square;
  37. //     }
  38.  
  39. //     Rectangle(double e1, double e2) {
  40. //         edge1 = e1;
  41. //         edge2 = e2;
  42. //         square = edge1 * edge2;
  43. //     }
  44.  
  45. //     static Rectangle* make(std::string str) {
  46. //         double e1, e2;
  47. //         sscanf(str.c_str(), "%lf%lf", &e1, &e2);
  48. //         return new Rectangle(e1, e2);
  49. //     }
  50. // };
  51.  
  52. // class Square : public Figure {
  53. // public:
  54. //     double edge, square;
  55.  
  56. //     virtual double get_square() const {
  57. //         return square;
  58. //     }
  59.  
  60. //     Square(double e) {
  61. //         edge = e;
  62. //         square = edge * edge;
  63. //     }
  64.  
  65. //     static Square* make(std::string str) {
  66. //         double e;
  67. //         sscanf(str.c_str(), "%lf", &e);
  68. //         return new Square(e);
  69. //     }
  70. // };
  71.  
  72. // class Circle : public Figure {
  73. // public:
  74. //     double rad, square;
  75.  
  76. //     virtual double get_square() const {
  77. //         return square;
  78. //     }
  79.  
  80. //     Circle(double r) {
  81. //         rad = r;
  82. //         square = M_PI * rad * rad;
  83. //     }
  84.    
  85. //     static Circle* make(std::string str) {
  86. //         double r;
  87. //         sscanf(str.c_str(), "%lf", &r);
  88. //         return new Circle(r);
  89. //     }
  90. // };
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. class Factory {
  101. public:
  102.  
  103.     static Factory &factory_instance()
  104.     {
  105.         static Factory inst;
  106.         return inst;
  107.     }
  108.  
  109.     std::map<std::string, std::function<Figure* (std::string str)>> m = {
  110.         {"R", Rectangle::make},
  111.         {"S", Square::make},
  112.         {"C", Circle::make},
  113.     };
  114. };
  115.  
  116. int main()
  117. {
  118.     std::string str, type, params;
  119.     std::vector<std::unique_ptr<Figure>> vec;
  120.     Factory &fac = Factory::factory_instance();
  121.     while (std::getline(std::cin, str)) {
  122.         std::istringstream stream(str);
  123.         stream >> type >> params;
  124.         Figure *fig = fac.m[type](params);
  125.         vec.push_back(std::unique_ptr<Figure>(fig));
  126.     }
  127.  
  128.     std::stable_sort(vec.begin(), vec.end(), [](const std::unique_ptr<Figure> &a, const std::unique_ptr<Figure> &b) {
  129.         return a->get_square() < b->get_square();
  130.     });
  131.  
  132.     for (auto c : vec) {
  133.         std::cout << c->to_string() << std::endl;
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement