new.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5. // Êëàññ òî÷êè.
  6. class Point {
  7. int x, y;
  8. public:
  9. // Êîíñòðóêòîð. Ñîçäàåò îáúåêò ñ íóëåâûìè êîîðäèíàòàìè.
  10. Point() { this->x = this->y = 0; }
  11. // Êîíñòðóêòîð. Ñîçäàåò îáúåêò ñ óêàçàííûìè êîîðäèíàòàìè.
  12. Point(int _x, int _y) { this->x = _x; this->y = _y; }
  13. // Äåñòðóêòîð. Çàíóëÿåò òî÷êè.
  14. ~Point() { this->x = this->y = 0; }
  15. // 2.5 Ñëîæåíèå òî÷åê. Åñëè ðàññìàòðèâàòü òî÷êó êàê âåêòîð, òî äàííóþ îïåðàöèþ ìîæíî ïðèìåíÿòü äëÿ ñëîæåíèÿ âåêòîðîâ.
  16. // Ïåðåãðóçêà ñòàíäàðòíîãî îïåðàòîðà ñëîæåíèÿ.
  17. Point operator+(const Point& other) {
  18. return Point(x + other.x, y + other.y);
  19. }
  20. void setCoordinates(int _x) { this->x = _x; }
  21. void setCoordinates(int _x, int _y) { this->x = _x; this->y = _y; }
  22. int getX() const { return this->x; }
  23. int getY() const { return this->y; }
  24. private:
  25. friend std::istream& operator>>(std::istream& stream, Point& p);
  26. };
  27. // 2.6 Îïåðàòîðû ââîäà âûâîäà äëÿ êëàññà òî÷êè.
  28. // Âûâîä.
  29. std::ostream& operator<<(std::ostream& stream, const Point& p) {
  30. stream << "Point (" << p.getX() << ";" << p.getY() << ")";
  31. return stream;
  32. }
  33. // Ââîä.
  34. std::istream& operator>>(std::istream& stream, Point& p) {
  35. stream >> p.x >> p.y;
  36. return stream;
  37. }
  38. // Áàçîâûé êëàññ äëÿ âñåõ ôèãóð.
  39. class Shape {
  40. public:
  41. // Ôóíêöèÿ ÷èñòî âèðòóàëüíàÿ. Íåîáõîäèìà äëÿ âû÷èñëåíèÿ ïåðìèòåòðà äëÿ ëþáûõ ôèãóð.
  42. // Äîëæíà áûòü ïåðåîïðåäåëåíà â íàñëåäíèêå.
  43. virtual int calculatePerimeter() const = 0;
  44. };
  45. // Êëàññ 4óãîëüíèêà. Íàñëåäóåì îò áàçîâîãî êëàññà ôèãóðû.
  46. class Polygon : public Shape {
  47. public:
  48. Polygon() { // Êîíñòðóêòîð ïî óìîë÷àíèþ. Ñîçäàåò òî÷êè ñ íóëåâûìè êîîðäèíàòàìè.
  49. pts = new Point[4]; // 2.4.1
  50. pts[0] = pts[1] = pts[2] = pts[3] = Point(0, 0);
  51. }
  52. // Êîíñòðóêòîð ñ ïàðàìåòðàìè.
  53. Polygon(Point p1, Point p2, Point p3, Point p4) {
  54. pts[0] = p1;
  55. pts[1] = p2;
  56. pts[2] = p3;
  57. pts[3] = p4;
  58. }
  59. // 2.4.3 Äåñòðóêòîð. Îñâîáîæäàåò ïàìÿòü, âûäåëåííóþ ïîä òî÷êè..
  60. ~Polygon() {
  61. delete[] pts;
  62. std::cout << "~Polygon(), memory cleared." << std::endl;
  63. }
  64. Point& getPoint(int index) const {
  65. return pts[index];
  66. }
  67. // Ôóíêöèÿ âû÷èñëåíèÿ ïåðèìåòðà, îáúÿâëåííàÿ â áàçîâîì êëàññå. Òóò ìû åå îïðåäåëèëè.
  68. int calculatePerimeter() const {
  69. int perimeter = 0;
  70. for (int i = 0; i < 4; ++i) {
  71. int currIndex = i, nextIndex = i + 1;
  72. if (currIndex == 3)
  73. nextIndex = 0;
  74. const int xDelta = std::abs(pts[currIndex].getX() - pts[nextIndex].getX());
  75. const int yDelta = std::abs(pts[currIndex].getY() - pts[nextIndex].getY());
  76. if (xDelta > 0)
  77. perimeter += xDelta;
  78. else
  79. perimeter += yDelta;
  80. }
  81. return perimeter;
  82. }
  83. private:
  84. Point* pts;
  85. };
  86. // Ôóíêöèÿ îïðåäåëÿåò òèï ïîëèãîíà - êâàäðàò èëè ïðÿ-íèê.
  87. // Âîçâðàùàåìûå çíà÷åíèÿ:
  88. // 1 - Êâàäðàò
  89. // 2 - Ïðÿ-íèê
  90. int getPolygonType(const Polygon& pol) {
  91. double width = 0.f, height = 0.f;
  92. for (int i = 1; i < 4; ++i) {
  93. if (pol.getPoint(0).getX() != pol.getPoint(i).getX()) {
  94. width = abs(pol.getPoint(0).getX() - pol.getPoint(i).getX());
  95. }
  96. else if (pol.getPoint(0).getY() != pol.getPoint(i).getY()) {
  97. height = abs(pol.getPoint(0).getY() - pol.getPoint(i).getY());
  98. }
  99. }
  100. double diag = sqrt(((pol.getPoint(2).getX() - pol.getPoint(0).getX()) * (pol.getPoint(2).getX() - pol.getPoint(0).getX())) + (pol.getPoint(2).getY() - pol.getPoint(0).getY()) * (pol.getPoint(2).getY() - pol.getPoint(0).getY()));
  101. if (((width * width) + (height * height) - (diag * diag)) / (2 * height * width) == 1) { //Íå êâàäðàò è íå ïðÿ-íèê
  102. return 3; // Íå êâàäðàò è íå ïðÿ-íèê
  103. }
  104. if (width == height) {
  105. return 1; // Êâàäðàò.
  106. }
  107. return 2; // Ïðÿ-íèê.
  108. }
  109. // "Îêâàäðà÷èâàåò" ïðÿ-íèê.
  110. void squarePolygon(Polygon& pol) {
  111. int width = 0, height = 0;
  112. for (int i = 1; i < 4; ++i) {
  113. if (pol.getPoint(0).getX() != pol.getPoint(i).getX()) {
  114. width = abs(pol.getPoint(0).getX() - pol.getPoint(i).getX());
  115. }
  116. else if (pol.getPoint(0).getY() != pol.getPoint(i).getY()) {
  117. height = abs(pol.getPoint(0).getY() - pol.getPoint(i).getY());
  118. }
  119. }
  120. int targetSide = std::min(width, height);
  121. for (int i = 1; i < 4; ++i) {
  122. if ((pol.getPoint(0).getX() - pol.getPoint(i).getX()) > targetSide) {
  123. pol.getPoint(0).setCoordinates(pol.getPoint(i).getX() + targetSide);
  124. }
  125. else if ((pol.getPoint(i).getX() - pol.getPoint(0).getX()) > targetSide) {
  126. pol.getPoint(0).setCoordinates(pol.getPoint(0).getX() + targetSide);
  127. }
  128. else if ((pol.getPoint(0).getY() - pol.getPoint(i).getY()) > targetSide) {
  129. pol.getPoint(0).setCoordinates(pol.getPoint(i).getY() + targetSide);
  130. }
  131. else if ((pol.getPoint(i).getY() - pol.getPoint(0).getY()) > targetSide) {
  132. pol.getPoint(0).setCoordinates(pol.getPoint(0).getY() + targetSide);
  133. }
  134. }
  135. }
  136. // Ñòàòè÷åñêèå ïåðåìåííàÿ è ôóíêöèÿ, êîòîðûå èíèöèèðóþò êîë-âî ôèãóð.
  137. static int polygonCount = 0;
  138. static void setPolygonCount() {
  139. polygonCount = 3;
  140. }
  141. int main() {
  142. setPolygonCount(); // Èíèöèèðóåì êîë-âî ôèãóð.
  143. std::cout << "polygonCount = " << polygonCount << ". Do you want to change it? (y/n): ";
  144. std::string answer; std::cin >> answer;
  145. if (answer == "y") { // Ìåíÿåì êîë-âî ôèãóð ïî çàïðîñó.
  146. polygonCount = 0;
  147. while (polygonCount < 1) {
  148. std::cout << "How many polygons you want to enter? " << std::endl;
  149. std::cin >> polygonCount;
  150. if (polygonCount < 1)
  151. std::cout << "Polygon count must be > 0." << std::endl;
  152. }
  153. }
  154. std::vector<Polygon> polygons(polygonCount);
  155. // Ñ÷èòûàåì êîîðäèíàòû òî÷åê êàæäîãî ïîëèãîíà.
  156. for (int i = 0; i < polygonCount; ++i) {
  157. int x, y;
  158. std::cout << "POLYGON #" << i + 1 << std::endl;
  159. for (int j = 0; j < 4; ++j) {
  160. std::cout << "Enter point " << j + 1 << " (x y): " << std::endl;
  161. std::cin >> polygons[i].getPoint(j);
  162. }
  163. }
  164. // Ñ÷èòàåì, ñêîëüêî ïðÿ-íèêîâ è ïîïóòíî îêâàäðà÷èâàåì èõ.
  165. int rectCount = 0;
  166. for (int i = 0; i < polygonCount; ++i) {
  167. if (getPolygonType(polygons[i]) == 2) {
  168. squarePolygon(polygons[i]);
  169. rectCount++;
  170. }
  171. }
  172. std::cout << "There is " << rectCount << " not squares." << std::endl;
  173. // Ñíîâà ñ÷èòàåì ñêîëüêî ïðÿ-íèêîâ (äîëæíî áûòü 0).
  174. rectCount = 0;
  175. for (int i = 0; i < polygonCount; ++i) {
  176. if (getPolygonType(polygons[i]) == 1) {
  177. rectCount++;
  178. }
  179. }
  180. // Âûâîäèì ðåçóëüòàò è âûõîäèì èç ïðîãðàììû.
  181. std::cout << "There is " << rectCount << " squares after squaring." << std::endl;
  182. return 0;
  183. }