Advertisement
Guest User

qwewrt

a guest
Apr 10th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. struct SPoint {
  6.     int x;
  7.     int y;
  8.  
  9. };
  10.  
  11. class IGeoFig {
  12. public:
  13.     virtual double square() = 0;
  14.  
  15.     virtual double perimeter() = 0;
  16. };
  17.  
  18. class CVector2D {
  19. public:
  20.     double x, y;
  21.  
  22. };
  23.  
  24. class IPhysObject {
  25. public:
  26.     virtual double mass() const = 0;
  27.  
  28.     virtual CVector2D position() = 0;
  29.  
  30.     virtual bool operator==(const IPhysObject &ob) const = 0;
  31.  
  32.     virtual bool operator<(const IPhysObject &ob) const = 0;
  33.  
  34. };
  35.  
  36. class IPrintable {
  37. public:
  38.     virtual void draw() = 0;
  39. };
  40.  
  41. class IDialogInitiable {
  42.     virtual void initFromDialog() = 0;
  43. };
  44.  
  45. class BaseCObject {
  46. public:
  47.     virtual const char *classname() = 0;
  48.  
  49.     virtual unsigned int size() = 0;
  50. };
  51.  
  52. class CFigure
  53.         : public IGeoFig,
  54.           public CVector2D,
  55.           public IPhysObject,
  56.           public IPrintable,
  57.           public IDialogInitiable,
  58.           public BaseCObject {
  59. };
  60.  
  61.  
  62. class CCircle : public CFigure {
  63. private:
  64.     SPoint centre;
  65.     double radius = 0;
  66.     double circleMass = 0;
  67. public:
  68.     void initFromDialog() override {
  69.         printf("Input centre, radius and circleMass\n");
  70.         std::cin >> centre.x >> centre.y >> radius >> circleMass;
  71.     }
  72.  
  73.     CCircle(CCircle &other) {
  74.         *this = other;
  75.     }
  76.  
  77.     void operator=(const CCircle &other) {
  78.         this->centre = other.centre;
  79.         this->radius = other.radius;
  80.         this->circleMass = other.circleMass;
  81.     }
  82.  
  83.     CCircle() {
  84.         this->centre.x = 0;
  85.         this->centre.y = 0;
  86.         this->radius = 0;
  87.         this->circleMass = 0;
  88.     }
  89.  
  90.     CCircle(SPoint otherCentre, double otherRadius, double otherMass) {
  91.         this->centre = otherCentre;
  92.         this->radius = otherRadius;
  93.         this->circleMass = otherMass;
  94.     }
  95.  
  96.     double mass() const override {
  97.         return circleMass;
  98.     }
  99.  
  100.     CVector2D position() override {
  101.         CVector2D position;
  102.         position.x = this->centre.x;
  103.         position.y = this->centre.y;
  104.         return position;
  105.     }
  106.  
  107.     bool operator==(const IPhysObject &ob) const override {
  108.         return this->mass() == ob.mass();
  109.     }
  110.  
  111.     bool operator<(const IPhysObject &ob) const override {
  112.         return this->mass() < ob.mass();
  113.     }
  114.  
  115.     const char *classname() override {
  116.         return "Circle\n";
  117.     }
  118.  
  119.     unsigned size() override {
  120.         return sizeof(CCircle);
  121.     }
  122.  
  123.     double perimeter() override {
  124.         return (2 * M_PI * this->radius);
  125.     }
  126.  
  127.     double square() override {
  128.         return (M_PI * pow(this->radius, 2));
  129.     }
  130.  
  131.     void draw() override {
  132.         std::cout << "\n---Circle---\n";
  133.         printf("Centre:\n(%d, %d);\nRadius: %d;\nMass: %d\n", this->centre.x, this->centre.y, this->radius,
  134.                this->circleMass);
  135.     }
  136. };
  137.  
  138. class CRectangle : public CFigure {
  139. private:
  140.     SPoint point1;
  141.     SPoint point2;
  142.     SPoint point3;
  143.     SPoint point4;
  144.     double mass1 = 0;
  145.     double mass2 = 0;
  146.     double mass3 = 0;
  147.     double mass4 = 0;
  148.  
  149.     double getDistance(SPoint firstPoint, SPoint secondPoint) const {
  150.         return std::sqrt(std::pow(secondPoint.x - firstPoint.x, 2) + std::pow(secondPoint.y - firstPoint.y, 2));
  151.     }
  152.  
  153. public:
  154.     CRectangle() {
  155.         point1 = {0, 0};
  156.         mass1 = 0;
  157.         point2 = {0, 0};
  158.         mass2 = 0;
  159.         point3 = {0, 0};
  160.         mass3 = 0;
  161.         point4 = {0, 0};
  162.         mass4 = 0;
  163.     }
  164.  
  165.     CRectangle(CRectangle &other) {
  166.         *this = other;
  167.     }
  168.  
  169.     void operator=(const CRectangle &other) {
  170.         this->point1 = other.point1;
  171.         this->point2 = other.point2;
  172.         this->point3 = other.point3;
  173.         this->point4 = other.point4;
  174.         this->mass1 = other.mass1;
  175.         this->mass2 = other.mass2;
  176.         this->mass3 = other.mass3;
  177.         this->mass4 = other.mass4;
  178.     }
  179.  
  180.     double mass() const override {
  181.         return (this->mass1 + this->mass2 + this->mass3 + this->mass4);
  182.     }
  183.  
  184.     CVector2D position() override {
  185.         CVector2D position;
  186.         position.x = (this->point1.x + this->point2.x + this->point3.x + this->point4.x) / 4;
  187.         position.y = (this->point1.y + this->point2.y + this->point3.y + this->point4.y) / 4;
  188.         return position;
  189.     }
  190.  
  191.     bool operator==(const IPhysObject &ob) const override {
  192.         return mass() == ob.mass();
  193.     }
  194.  
  195.     bool operator<(const IPhysObject &ob) const override {
  196.         return this->mass() < ob.mass();
  197.     }
  198.  
  199.     const char *classname() override {
  200.         return "Rectangle!\n";
  201.     }
  202.  
  203.     unsigned size() override {
  204.         return sizeof(CRectangle);
  205.     }
  206.  
  207.     void initFromDialog() override {
  208.         printf("Input 4 points and their mass:\n");
  209.         std::cin >> this->point1.x >> this->point1.y;
  210.         std::cin >> this->mass1;
  211.         std::cin >> this->point2.x >> this->point2.y;
  212.         std::cin >> this->mass2;
  213.         std::cin >> this->point3.x >> this->point3.y;
  214.         std::cin >> this->mass3;
  215.         std::cin >> this->point4.x >> this->point4.y;
  216.         std::cin >> this->mass4;
  217.     }
  218.  
  219.     double perimeter() override {
  220.         double perimeter = 0;
  221.         perimeter = getDistance(this->point1, this->point2) + getDistance(this->point2, this->point3) +
  222.                     getDistance(this->point3, this->point4) + getDistance(this->point4, this->point1);
  223.         return perimeter;
  224.     }
  225.  
  226.     double square() override {
  227.         double a = getDistance(point1, point2);
  228.         double b = getDistance(point1, point3);
  229.         return (a * b);
  230.     }
  231.  
  232.     void draw() override {
  233.         printf("\n---Rectangle---\n");
  234.         printf("Points:\n(%d, %d) %d;\n (%d, %d) %d;\n (%d, %d) %d;\n (%d, %d) %d;\n\n", this->point1.x, this->point1.y,
  235.                this->mass1,
  236.                this->point2.x, this->point2.y, this->mass2, this->point3.x, this->point3.y, this->mass3,
  237.                this->point4.x, this->point4.y, this->mass4);
  238.         printf("Mass:%lf\n", mass());
  239.     }
  240. };
  241.  
  242. void printCommands() {
  243.     printf("1: Add figure\n2: Print all figures\n3: Sum squares\n4: Sum perimeters\n"
  244.            "5: Print centre of mass\n6: Memory\n7: Sort\n0: Exit\n");
  245. }
  246.  
  247. int main() {
  248.     std::vector<CFigure *> Figures;
  249.     int task = 0;
  250.     while (true) {
  251.         printCommands();
  252.         std::cin >> task;
  253.         switch (task) {
  254.             case 1:
  255.                 printf("1: Circle\n2: Rectangle\n");
  256.                 int x;
  257.                 std::cin >> x;
  258.                 if (x == 1) {
  259.                     CCircle *tmp = new CCircle();
  260.                     tmp->initFromDialog();
  261.                     Figures.push_back(tmp);
  262.                 } else {
  263.                     CRectangle *tmp = new CRectangle;
  264.                     tmp->initFromDialog();
  265.                     Figures.push_back(tmp);
  266.                 }
  267.                 break;
  268.             case 2: {
  269.                 std::cout << std::endl;
  270.                 for (int i = 0; i < Figures.size(); i++) {
  271.                     printf("%d: ", i + 1);
  272.                     Figures[i]->draw();
  273.                 }
  274.                 std::cout << std::endl;
  275.                 break;
  276.             }
  277.             case 3: {
  278.                 double sum = 0;
  279.                 for (int i = 0; i < Figures.size(); i++) {
  280.                     sum += Figures[i]->square();
  281.                 }
  282.                 std::cout << sum << std::endl;
  283.                 break;
  284.             }
  285.             case 4: {
  286.                 double sum = 0;
  287.                 for (int i = 0; i < Figures.size(); i++) {
  288.                     sum += Figures[i]->perimeter();
  289.                 }
  290.                 std::cout << sum << std::endl;
  291.                 break;
  292.             }
  293.             case 5: {
  294.                 double x = 0;
  295.                 double y = 0;
  296.                 double tmpMass = 0;
  297.                 for (int i = 0; i < Figures.size(); i++) {
  298.                     x += Figures[i]->position().x * Figures[i]->mass();
  299.                     y += Figures[i]->position().y * Figures[i]->mass();
  300.                     tmpMass += Figures[i]->mass();
  301.                 }
  302.                 std::cout << x / tmpMass << " " << y / tmpMass << std::endl;
  303.                 break;
  304.             }
  305.             case 6: {
  306.                 double sum = 0;
  307.                 for (int i = 0; i < Figures.size(); i++) {
  308.                     sum += Figures[i]->size();
  309.                 }
  310.                 std::cout << sum << std::endl;
  311.                 break;
  312.             }
  313.             case 7: {
  314.                 for (int i = 0; i < Figures.size(); i++) {
  315.                     for (int j = i + 1; j < Figures.size(); j++) {
  316.                         if ((Figures[i]->classname() == Figures[j]->classname()) && (Figures[j] < Figures[i])) {
  317.                             CFigure *tmp = Figures[i];
  318.                             Figures[i] = Figures[j];
  319.                             Figures[j] = tmp;
  320.                         }
  321.                     }
  322.                 }
  323.             }
  324.             default:
  325.                 exit(0);
  326.         }
  327.     }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement