Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <sstream>
- #include <iostream>
- #include <map>
- #include <functional>
- #include <string>
- #include <vector>
- #include <memory>
- // #include <math.h>
- // class Figure {
- // private:
- // double square;
- // public:
- // Figure() {
- // square = 0;
- // }
- // Figure(double sq) {
- // square = sq;
- // }
- // virtual ~Figure() {}
- // virtual double get_square() const {
- // return square;
- // };
- // };
- // class Rectangle : public Figure {
- // public:
- // double edge1, edge2, square;
- // virtual double get_square() const {
- // return square;
- // }
- // Rectangle(double e1, double e2) {
- // edge1 = e1;
- // edge2 = e2;
- // square = edge1 * edge2;
- // }
- // static Rectangle* make(std::string str) {
- // double e1, e2;
- // sscanf(str.c_str(), "%lf%lf", &e1, &e2);
- // return new Rectangle(e1, e2);
- // }
- // };
- // class Square : public Figure {
- // public:
- // double edge, square;
- // virtual double get_square() const {
- // return square;
- // }
- // Square(double e) {
- // edge = e;
- // square = edge * edge;
- // }
- // static Square* make(std::string str) {
- // double e;
- // sscanf(str.c_str(), "%lf", &e);
- // return new Square(e);
- // }
- // };
- // class Circle : public Figure {
- // public:
- // double rad, square;
- // virtual double get_square() const {
- // return square;
- // }
- // Circle(double r) {
- // rad = r;
- // square = M_PI * rad * rad;
- // }
- // static Circle* make(std::string str) {
- // double r;
- // sscanf(str.c_str(), "%lf", &r);
- // return new Circle(r);
- // }
- // };
- class Factory {
- public:
- static Factory &factory_instance()
- {
- static Factory inst;
- return inst;
- }
- std::map<std::string, std::function<Figure* (std::string str)>> m = {
- {"R", Rectangle::make},
- {"S", Square::make},
- {"C", Circle::make},
- };
- };
- int main()
- {
- std::string str, type, params;
- std::vector<std::unique_ptr<Figure>> vec;
- Factory &fac = Factory::factory_instance();
- while (std::getline(std::cin, str)) {
- std::istringstream stream(str);
- stream >> type >> params;
- Figure *fig = fac.m[type](params);
- vec.push_back(std::unique_ptr<Figure>(fig));
- }
- std::stable_sort(vec.begin(), vec.end(), [](const std::unique_ptr<Figure> &a, const std::unique_ptr<Figure> &b) {
- return a->get_square() < b->get_square();
- });
- for (auto c : vec) {
- std::cout << c->to_string() << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement