Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <cmath>
- #include <string>
- #include <sstream>
- #include <iostream>
- #include <map>
- #include <vector>
- #include <algorithm>
- class FigCreator
- {
- public:
- virtual Figure *create(const std::string &str) = 0;
- virtual ~FigCreator() = default;
- };
- class CircleCreator : public FigCreator
- {
- public:
- Figure *create(const std::string &str) override
- {
- return Circle::make(str);
- }
- };
- class RectangleCreator : public FigCreator
- {
- public:
- Figure *create(const std::string &str) override
- {
- return Rectangle::make(str);
- }
- };
- class SquareCreator : public FigCreator
- {
- public:
- Figure *create(const std::string &str) override
- {
- return Square::make(str);
- }
- };
- class AbstractFactory
- {
- public:
- static AbstractFactory &factory_instance()
- {
- static AbstractFactory s;
- return s;
- }
- Figure *create(const std::string &type, const std::string ¶ms)
- {
- return disc[type]->create(params);
- }
- private:
- std::map<std::string, FigCreator *> disc = {
- {"C", new CircleCreator()},
- {"S", new SquareCreator()},
- {"R", new RectangleCreator()},
- };
- AbstractFactory() = default;
- ~AbstractFactory()
- {
- for (auto &c:disc) {
- delete c.second;
- }
- }
- AbstractFactory(AbstractFactory const &) = delete;
- AbstractFactory &operator=(AbstractFactory const &) = delete;
- };
- int main()
- {
- std::vector<Figure *> figures;
- std::string tmp;
- while (!std::getline(std::cin, tmp).eof()) {
- std::stringstream s;
- s << tmp;
- std::string type, param1, param2;
- s >> type >> param1 >> param2;
- figures.push_back(AbstractFactory::factory_instance().create(type, param1 + " " + param2));
- }
- std::stable_sort(figures.begin(), figures.end(),
- [](Figure *a, Figure *b) { return a->get_square() < b->get_square(); });
- for (auto &c:figures) {
- std::cout << c->to_string() << std::endl;
- delete c;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment