2.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. struct Point {
  5. int x;
  6. int y;
  7. std::string color; // Поле для хранения цвета
  8. Point* next; // Указатель на следующую точку
  9. };
  10. double distance(const Point& p1, const Point& p2) {
  11. return std::sqrt(std::pow(p1.x - p2.x, 2) + std::pow(p1.y - p2.y, 2));
  12. }
  13. Point* findMinPoint(Point* head) {
  14. if (!head) return nullptr;
  15. Point* minPoint = head;
  16. Point* current = head->next;
  17. while (current != head) {
  18. if (current->x < minPoint->x || (current->x == minPoint->x && current->y < minPoint->y)) {
  19. minPoint = current;
  20. }
  21. current = current->next;
  22. }
  23. return minPoint;
  24. }
  25. double calculatePerimeter(Point* head) {
  26. if (!head || !head->next) return 0.0;
  27. double perimeter = 0.0;
  28. Point* current = head;
  29. do {
  30. perimeter += distance(*current, *current->next);
  31. current = current->next;
  32. } while (current != head);
  33. return perimeter;
  34. }
  35. void printPoint(const Point& p) {
  36. std::cout << "Point (" << p.x << ", " << p.y << ") - Color: " << p.color << std::endl;
  37. }
  38. int main() {
  39. Point p1 = {1, 2, "Red", nullptr};
  40. Point p2 = {3, 4, "Green", nullptr};
  41. Point p3 = {5, 6, "Blue", nullptr};
  42. p1.next = &p2;
  43. p2.next = &p3;
  44. p3.next = &p1; // Делаем список циклическим
  45. std::cout << "Accessing members through point:" << std::endl;
  46. printPoint(p1);
  47. std::cout << "Accessing members through pointer:" << std::endl;
  48. printPoint(*p1.next);
  49. Point p4 = p1;
  50. std::cout << "After assignment:" << std::endl;
  51. printPoint(p4);
  52. std::cout << "Distance between p1 and p2: " << distance(p1, p2) << std::endl;
  53. Point* ptr = &p1;
  54. std::cout << "Incrementing pointer:" << std::endl;
  55. ptr = ptr->next;
  56. printPoint(*ptr);
  57. Point* minPoint = findMinPoint(&p1);
  58. std::cout << "Point with minimum coordinates:" << std::endl;
  59. printPoint(*minPoint);
  60. std::cout << "Perimeter of the polygon: " << calculatePerimeter(&p1) << std::endl;
  61. return 0;
  62. }