Advertisement
PyTimur

Tecnick

May 15th, 2022
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. struct Point {
  6.     int x, y;
  7.     Point() {}
  8.     Point(Point& p) {
  9.         x = p.x;
  10.         y = p.y;
  11.     }
  12.     Point(int x, int y) {
  13.         this->x = x;
  14.         this->y = y;
  15.     }
  16. };
  17.  
  18. struct PointCollectionElement {
  19.     Point* info;
  20.     PointCollectionElement *next;
  21.     PointCollectionElement(int x, int y) {
  22.         info = new Point(x, y);
  23.         next = nullptr;
  24.     }
  25.     PointCollectionElement(PointCollectionElement& p) {
  26.         info = p.info;
  27.         next = nullptr;
  28.     }
  29.     ~PointCollectionElement() {
  30.         delete info; info = 0;
  31.     }
  32. };
  33.  
  34. class PointCollection {
  35.     private:
  36.         PointCollectionElement* first;
  37.         PointCollectionElement* last;
  38.         PointCollectionElement* cur;
  39.    
  40.     public:
  41.         PointCollection() {
  42.             last = first = cur = nullptr;
  43.         }
  44.         PointCollection(PointCollection& p) {
  45.             last = first = cur = nullptr;
  46.             PointCollectionElement* temp = p.first;
  47.             while(temp != nullptr) {
  48.                 int x = temp->info->x; int y = temp->info->y;
  49.                 addPoint(x, y);
  50.                 if(temp == p.cur) cur = last;
  51.                 temp = temp->next;
  52.             }
  53.         }
  54.         ~PointCollection() {
  55.             while(first != nullptr) {
  56.                 PointCollectionElement* temp = first;
  57.                 first = first->next;
  58.                 delete temp;
  59.             }
  60.         }
  61.         void addPoint(int x, int y) {
  62.             if(first == nullptr) {
  63.                 cur = first = last = new PointCollectionElement(x, y);
  64.             } else {
  65.                 last = (last->next = new PointCollectionElement(x, y));
  66.             }
  67.         }
  68.  
  69.         void reset() {
  70.             cur = first;
  71.         }
  72.  
  73.         Point* nextPoint() {
  74.             if(cur == nullptr) {
  75.                 return nullptr;
  76.             }
  77.             Point* info = cur->info;
  78.             cur = cur->next;
  79.             return info;
  80.         }
  81. };
  82.  
  83. void process0(PointCollection& points, int n = 3) {
  84.     while(n-- > 0) {
  85.         points.addPoint(rand() % 101, rand() % 101);
  86.     }
  87. }
  88.  
  89. //здесь нельзя менять сигнатуру метода (в том числе добавлять &, * и т.д.)
  90. void process1(PointCollection points) {
  91.     Point* cur;
  92.     while((cur = points.nextPoint()) != nullptr) {
  93.         cout << " | " << (cur->x * cur->x + cur->y * cur->y) << " | ";
  94.     }
  95.     cout << endl << endl;
  96. }
  97.  
  98. //здесь нельзя менять сигнатуру метода (в том числе добавлять &, * и т.д.)
  99. void process2(PointCollection points) {
  100.     Point* cur;
  101.     while((cur = points.nextPoint()) != nullptr) {
  102.         cout << " | " << cur->x << " " << cur->y << " | ";
  103.     }
  104.     cout << endl << endl;
  105. }
  106.  
  107. int main() {
  108.     srand(time(0));
  109.     PointCollection points1, points2;
  110.     process0(points1);
  111.     process1(points1);
  112.     process0(points2);
  113.     process1(points2);
  114.     process2(points1);
  115.     process2(points2);
  116.     return 0;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement