Untitled

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