123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #include <iostream>
- #include <cmath>
- #include <string>
- struct Point {
- int x;
- int y;
- std::string color; // Поле для хранения цвета
- Point* next; // Указатель на следующую точку
- };
- double distance(const Point& p1, const Point& p2) {
- return std::sqrt(std::pow(p1.x - p2.x, 2) + std::pow(p1.y - p2.y, 2));
- }
- Point* findMinPoint(Point* head) {
- if (!head) return nullptr;
- Point* minPoint = head;
- Point* current = head->next;
- while (current != head) {
- if (current->x < minPoint->x || (current->x == minPoint->x && current->y < minPoint->y)) {
- minPoint = current;
- }
- current = current->next;
- }
- return minPoint;
- }
- double calculatePerimeter(Point* head) {
- if (!head || !head->next) return 0.0;
- double perimeter = 0.0;
- Point* current = head;
- do {
- perimeter += distance(*current, *current->next);
- current = current->next;
- } while (current != head);
- return perimeter;
- }
- void printPoint(const Point& p) {
- std::cout << "Point (" << p.x << ", " << p.y << ") - Color: " << p.color << std::endl;
- }
- int main() {
- Point p1 = {1, 2, "Red", nullptr};
- Point p2 = {3, 4, "Green", nullptr};
- Point p3 = {5, 6, "Blue", nullptr};
- p1.next = &p2;
- p2.next = &p3;
- p3.next = &p1; // Делаем список циклическим
- std::cout << "Accessing members through point:" << std::endl;
- printPoint(p1);
- std::cout << "Accessing members through pointer:" << std::endl;
- printPoint(*p1.next);
- Point p4 = p1;
- std::cout << "After assignment:" << std::endl;
- printPoint(p4);
- std::cout << "Distance between p1 and p2: " << distance(p1, p2) << std::endl;
- Point* ptr = &p1;
- std::cout << "Incrementing pointer:" << std::endl;
- ptr = ptr->next;
- printPoint(*ptr);
- Point* minPoint = findMinPoint(&p1);
- std::cout << "Point with minimum coordinates:" << std::endl;
- printPoint(*minPoint);
- std::cout << "Perimeter of the polygon: " << calculatePerimeter(&p1) << std::endl;
- return 0;
- }
|