Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.55 KB | None | 0 0
  1. // Identifiers.h ///////////////////
  2.  
  3. #pragma once
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Identifiers // Генератор идентификаторов фигур
  9. {
  10. private:
  11.     static int Number; // Порядковый номер идентификатора
  12.  
  13. public:
  14.     static string GetId(); // Возвращает строковое представление идентификатора (статический метод)
  15.     Identifiers();
  16.     ~Identifiers();
  17. };
  18.  
  19. // Identifiers.cpp ///////////////////
  20.  
  21. #include "stdafx.h"
  22. #include "Identifiers.h"
  23. #include <string>
  24.  
  25. using namespace std;
  26.  
  27. int Identifiers::Number;
  28.  
  29. string Identifiers::GetId()
  30. {
  31.     Identifiers::Number++;
  32.  
  33.     return "S" + std::to_string(Identifiers::Number);
  34. }
  35.  
  36. Identifiers::Identifiers()
  37. {
  38. }
  39.  
  40.  
  41. Identifiers::~Identifiers()
  42. {
  43. }
  44.  
  45. // Shape.h ///////////////////
  46.  
  47. #pragma once
  48. #include <list>
  49. #include <vector>
  50. #include "Coordinate.h"
  51.  
  52. using namespace std;
  53.  
  54. class Shape
  55. {
  56.     private:
  57.         static std::list<Shape> Items; // Список всех фигур
  58.         string Id; // Идентификатор
  59.  
  60.     protected:
  61.         int X = 0, Y = 0, Width, Height; // Коодринаты начала (центра) (в зависимости от фигуры), ширина и высота
  62.         std::vector<Coordinate> Coordinates; // Список координат (не для всех фигур)
  63.    
  64.     public:
  65.         string GetId(); // Возвращает идентификатор
  66.         void MoveByX(int Offset); // Перемещает начало (центр) фигуры по оси X
  67.         void MoveByY(int Offset); // Перемещает начало (центр) фигуры по оси Y
  68.         virtual void Scale(int Multiplier); // Изменяет размер фигуры (виртуальный метод)
  69.         virtual double GetArea(); // Возвращает площадь фигуры (виртуальный метод)
  70.         virtual int Compare(Shape Left, Shape Right); // Сравнение двух фигур (виртуальный метод)
  71.         virtual string to_string(); // Возвращает строковое представление класса (виртуальный метод)
  72.         static long GetSumOfAreas(Shape Shapes[], int Length); // Возвращает сумму площадей всех фигур (статический метод)
  73.         Shape();
  74.         ~Shape();
  75. };
  76.  
  77. // Shape.cpp ///////////////////
  78.  
  79. #include "stdafx.h"
  80. #include "Shape.h"
  81. #include "Identifiers.h"
  82. #include <string>
  83. #include <list>
  84. #include <vector>
  85. #include <iostream>
  86.  
  87. string Shape::GetId()
  88. {
  89.     return this->Id;
  90. }
  91.  
  92. void Shape::MoveByX(int Offset)
  93. {
  94.     std::vector<Coordinate>::iterator Iterator;
  95.  
  96.     for (Iterator = this->Coordinates.begin(); Iterator != this->Coordinates.end(); ++Iterator)
  97.     {
  98.         Iterator->X += Offset;
  99.     }
  100.  
  101.     this->X += Offset;
  102. }
  103.  
  104. void Shape::MoveByY(int Offset)
  105. {
  106.     std::vector<Coordinate>::iterator Iterator;
  107.  
  108.     for (Iterator = this->Coordinates.begin(); Iterator != this->Coordinates.end(); ++Iterator)
  109.     {
  110.         Iterator->Y += Offset;
  111.     }
  112.  
  113.     this->Y += Offset;
  114. }
  115.  
  116. void Shape::Scale(int Multiplier)
  117. {
  118.     std::vector<Coordinate>::iterator Iterator;
  119.  
  120.     for (Iterator = this->Coordinates.begin(); Iterator != this->Coordinates.end(); ++Iterator)
  121.     {
  122.         Iterator->X *= Multiplier;
  123.         Iterator->Y *= Multiplier;
  124.     }
  125. }
  126.  
  127. double Shape::GetArea()
  128. {
  129.     cout << "This method must be overriden in a child class\n";
  130.     return 0;
  131. }
  132.  
  133. string Shape::to_string()
  134. {
  135.     string Output = "OOP.Shape(X = " + std::to_string(this->X) + "; Y = " + std::to_string(this->Y) + "; Coordinates: ";
  136.     int I = 1;
  137.  
  138.     std::vector<Coordinate>::iterator Iterator;
  139.  
  140.     for (Iterator = this->Coordinates.begin(); Iterator != this->Coordinates.end(); ++Iterator)
  141.     {
  142.         Output += std::to_string(I) + "(" + std::to_string(Iterator->X) + "; " + std::to_string(Iterator->Y) + ") ";
  143.     }
  144.  
  145.     return Output;
  146. }
  147.  
  148. long Shape::GetSumOfAreas(Shape Shapes[], int Length)
  149. {
  150.     long Sum = 0;
  151.  
  152.     for (int I = 0; I < Length; ++I)
  153.     {
  154.         Sum += Shapes[I].GetArea();
  155.     }
  156.  
  157.     return Sum;
  158. }
  159.  
  160. int Shape::Compare(Shape Left, Shape Right)
  161. {
  162.     return 0;
  163. }
  164.  
  165. Shape::Shape()
  166. {
  167.     this->Id = Identifiers::GetId();
  168. }
  169.  
  170.  
  171. Shape::~Shape()
  172. { }
  173.  
  174. // Circle.h ///////////////////
  175.  
  176. #pragma once
  177. #include "Shape.h"
  178. class Circle : public Shape
  179. {
  180. private:
  181.     int Radius; // Диаметр
  182.     int CenterX; // Координата X центра
  183.     int CenterY; // Координата Y центра
  184.  
  185. public:
  186.     int GetRadius(); // Возвращает радиус
  187.     int GetCenterX(); // Возвращает координату X центра
  188.     int GetCenterY(); // Возвращает координату Y центра
  189.     int GetDiameter(); // Возвращает диаметр
  190.     double GetArea(); // Возвращает площадь
  191.     double GetCircumference(); // Возвращает длину окружности
  192.     double GetSectorArea(int Degree); // Возвращает площадь сектора заданного центрального угла (Degree)
  193.  
  194.     void SetRadius(int Radius); // Устанавливает радиус
  195.     void SetCenterX(int X); // Устанавливает координату X центра
  196.     void SetCenterY(int Y); // Устанавливает координату Y центра
  197.     void Scale(double Multiplier); // Изменяет размер окружности на величину (Multiplier)
  198.     string to_string(); // Возвращает строковое представление класса
  199.     int Compare(Circle Left, Circle Right); // Возвращает -1, если Left имеет большую площадь, 0 - если площадь Left и Right равны, 1 - если Right имеет большую площадь
  200.  
  201.     bool operator < (Circle Circle);
  202.     bool operator > (Circle Circle);
  203.  
  204.     Circle();
  205.     ~Circle();
  206. };
  207.  
  208. // Circle.cpp ///////////////////
  209.  
  210. #include "stdafx.h"
  211. #include "Circle.h"
  212. #include <string>
  213.  
  214. using namespace std;
  215.  
  216. int Circle::GetRadius()
  217. {
  218.     return this->Radius;
  219. }
  220.  
  221. int Circle::GetCenterX()
  222. {
  223.     return this->X;
  224. }
  225.  
  226. int Circle::GetCenterY()
  227. {
  228.     return this->Y;
  229. }
  230.  
  231. int Circle::GetDiameter()
  232. {
  233.     return this->Radius * 2;
  234. }
  235.  
  236. double Circle::GetArea()
  237. {
  238.     return 3.14 * pow((this->GetDiameter() / 2), 2);
  239. }
  240.  
  241. double Circle::GetCircumference()
  242. {
  243.     return 3.14 * this->GetDiameter();
  244. }
  245.  
  246. double Circle::GetSectorArea(int Degree)
  247. {
  248.     return (this->GetArea() / 360) * Degree;
  249. }
  250.  
  251. void Circle::SetRadius(int Radius)
  252. {
  253.     if (Radius > 0)
  254.     {
  255.         this->Radius = Radius;
  256.     }
  257. }
  258.  
  259. void Circle::SetCenterX(int X)
  260. {
  261.     this->X = X;
  262. }
  263.  
  264. void Circle::SetCenterY(int Y)
  265. {
  266.     this->Y = Y;
  267. }
  268.  
  269. void Circle::Scale(double Multiplier)
  270. {
  271.     if (Multiplier > 0)
  272.     {
  273.         this->Radius *= Multiplier;
  274.     }
  275. }
  276.  
  277. string Circle::to_string()
  278. {
  279.     return "OOP.Circle(X = " + std::to_string(this->X) + "; Y = " + std::to_string(this->Y) + "; Radius: " + std::to_string(this->Radius) + ")";
  280. }
  281.  
  282. int Circle::Compare(Circle Left, Circle Right)
  283. {
  284.     if (Left.GetArea() > Right.GetArea())
  285.     {
  286.         return -1;
  287.     }
  288.  
  289.     if (Left.GetArea() < Right.GetArea())
  290.     {
  291.         return 1;
  292.     }
  293.  
  294.     return 0;
  295. }
  296.  
  297. bool Circle::operator < (Circle Circle)
  298. {
  299.     return this->GetDiameter() < Circle.GetDiameter();
  300. };
  301.  
  302. bool Circle::operator > (Circle Circle)
  303. {
  304.     return this->GetDiameter() > Circle.GetDiameter();
  305. };
  306.  
  307. Circle::Circle() : Shape()
  308. {
  309. }
  310.  
  311.  
  312. Circle::~Circle()
  313. {
  314. }
  315.  
  316. // Pentagon.h ///////////////////
  317.  
  318. #pragma once
  319. #include "Shape.h"
  320. #include "Circle.h"
  321. class Pentagon : public Shape
  322. {
  323. private:
  324.     Circle OuterCircle; // Вписанная окружности
  325. public:
  326.     int GetCircleRadius(); // Возвращает радиус вписанной окружности
  327.     int GetCenterX(); // Возвращает координату X центра вписанной окружности
  328.     int GetCenterY(); // Возвращает координату Y центра вписанной окружности
  329.     int GetCircleDiameter(); // Возвращает диаметр вписанной окружности
  330.     double GetSideLength(); // Возвращает длину стороны
  331.     double GetPerimeter(); // Возвращает периметр
  332.     double GetArea(); // Возвращает площадь
  333.     double GetCircleCircumference(); // Возвращает длину вписанной окружности
  334.     double GetSectorArea(int Degree); // Возвращает площадь сектора заданного центрального угла (Degree)
  335.     double GetInnerCircleRadius(); // Возвращает радиус вписанной окружности
  336.  
  337.     void SetCircleRadius(int Radius); // Устанавливает радиус
  338.     void SetCenterX(int X); // Устанавливает координату X центра вписанной окружности
  339.     void SetCenterY(int Y); // Устанавливает координату Y центра вписанной окружности
  340.     void Scale(double Multiplier); // Изменяет размер окружности на величину (Multiplier)
  341.     string to_string(); // Возвращает строковое представление класса
  342.     int Compare(Pentagon Left, Pentagon Right); // Возвращает -1, если Left имеет большую площадь, 0 - если площадь Left и Right равны, 1 - если Right имеет большую площадь
  343.  
  344.     bool operator < (Pentagon Pentagon);
  345.     bool operator > (Pentagon Pentagon);
  346.  
  347.     Pentagon();
  348.     ~Pentagon();
  349. };
  350.  
  351. // Pentagon.cpp ///////////////////
  352.  
  353. #include "stdafx.h"
  354. #include "Pentagon.h"
  355. #include "Circle.h"
  356. #include <string>
  357. #include <math.h>
  358.  
  359. using namespace std;
  360.  
  361. int Pentagon::GetCircleRadius()
  362. {
  363.     return this->OuterCircle.GetRadius();
  364. }
  365.  
  366. int Pentagon::GetCenterX()
  367. {
  368.     return this->X;
  369. }
  370.  
  371. int Pentagon::GetCenterY()
  372. {
  373.     return this->Y;
  374. }
  375.  
  376. int Pentagon::GetCircleDiameter()
  377. {
  378.     return this->OuterCircle.GetRadius() * 2;
  379. }
  380.  
  381. double Pentagon::GetSideLength()
  382. {
  383.     return (2 * this->OuterCircle.GetRadius()) * sin(180 / 5);
  384. }
  385.  
  386. double Pentagon::GetArea()
  387. {
  388.     return 5 * pow(this->OuterCircle.GetRadius(), 2) * tan(180 / 5);
  389. }
  390.  
  391. double Pentagon::GetPerimeter()
  392. {
  393.     return this->GetSideLength() * 5;
  394. }
  395.  
  396. double Pentagon::GetCircleCircumference()
  397. {
  398.     return 3.14 * this->OuterCircle.GetDiameter();
  399. }
  400.  
  401. double Pentagon::GetInnerCircleRadius()
  402. {
  403.     return this->OuterCircle.GetRadius() * cos(180 / 5);
  404. }
  405.  
  406. double Pentagon::GetSectorArea(int Degree)
  407. {
  408.     return (this->GetArea() / 360) * Degree;
  409. }
  410.  
  411. void Pentagon::SetCircleRadius(int Radius)
  412. {
  413.     this->OuterCircle.SetRadius(Radius);
  414. }
  415.  
  416. void Pentagon::SetCenterX(int X)
  417. {
  418.     this->OuterCircle.SetCenterX(X);
  419. }
  420.  
  421. void Pentagon::SetCenterY(int Y)
  422. {
  423.     this->OuterCircle.SetCenterY(Y);
  424. }
  425.  
  426. void Pentagon::Scale(double Multiplier)
  427. {
  428.     this->OuterCircle.Scale(Multiplier);
  429. }
  430.  
  431. string Pentagon::to_string()
  432. {
  433.     return "OOP.Pentagon(X = " + std::to_string(this->OuterCircle.GetCenterX()) + "; Y = " + std::to_string(this->OuterCircle.GetCenterY()) + "; Outer circle's radius: " + std::to_string(this->OuterCircle.GetRadius()) + ")";
  434. }
  435.  
  436. int Pentagon::Compare(Pentagon Left, Pentagon Right)
  437. {
  438.     if (Left.GetArea() > Right.GetArea())
  439.     {
  440.         return -1;
  441.     }
  442.  
  443.     if (Left.GetArea() < Right.GetArea())
  444.     {
  445.         return 1;
  446.     }
  447.  
  448.     return 0;
  449. }
  450.  
  451. bool Pentagon::operator < (Pentagon Pentagon)
  452. {
  453.     return this->OuterCircle.GetDiameter() < Pentagon.GetCircleDiameter();
  454. };
  455.  
  456. bool Pentagon::operator > (Pentagon Pentagon)
  457. {
  458.     return this->OuterCircle.GetDiameter() > Pentagon.GetCircleDiameter();
  459. };
  460.  
  461. Pentagon::Pentagon()
  462. {
  463.     this->OuterCircle = Circle();
  464. }
  465.  
  466.  
  467. Pentagon::~Pentagon()
  468. {
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement