Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- struct Point {
- int x, y;
- Point() {}
- Point(Point& p) {
- x = p.x;
- y = p.y;
- }
- Point(int x, int y) {
- this->x = x;
- this->y = y;
- }
- };
- struct PointCollectionElement {
- Point* info;
- PointCollectionElement *next;
- PointCollectionElement(int x, int y) {
- info = new Point(x, y);
- next = nullptr;
- }
- PointCollectionElement(PointCollectionElement& p) {
- info = p.info;
- next = nullptr;
- }
- ~PointCollectionElement() {
- delete info; info = 0;
- }
- };
- class PointCollection {
- private:
- PointCollectionElement* first;
- PointCollectionElement* last;
- PointCollectionElement* cur;
- public:
- PointCollection() {
- last = first = cur = nullptr;
- }
- PointCollection(PointCollection& p) {
- last = first = cur = nullptr;
- PointCollectionElement* temp = p.first;
- while(temp != nullptr) {
- int x = temp->info->x; int y = temp->info->y;
- addPoint(x, y);
- if(temp == p.cur) cur = last;
- temp = temp->next;
- }
- }
- ~PointCollection() {
- while(first != nullptr) {
- PointCollectionElement* temp = first;
- first = first->next;
- delete temp;
- }
- }
- void addPoint(int x, int y) {
- if(first == nullptr) {
- cur = first = last = new PointCollectionElement(x, y);
- } else {
- last = (last->next = new PointCollectionElement(x, y));
- }
- }
- void reset() {
- cur = first;
- }
- Point* nextPoint() {
- if(cur == nullptr) {
- return nullptr;
- }
- Point* info = cur->info;
- cur = cur->next;
- return info;
- }
- };
- void process0(PointCollection& points, int n = 3) {
- while(n-- > 0) {
- points.addPoint(rand() % 101, rand() % 101);
- }
- }
- //здесь нельзя менять сигнатуру метода (в том числе добавлять &, * и т.д.)
- void process1(PointCollection points) {
- Point* cur;
- while((cur = points.nextPoint()) != nullptr) {
- cout << " | " << (cur->x * cur->x + cur->y * cur->y) << " | ";
- }
- cout << endl << endl;
- }
- //здесь нельзя менять сигнатуру метода (в том числе добавлять &, * и т.д.)
- void process2(PointCollection points) {
- Point* cur;
- while((cur = points.nextPoint()) != nullptr) {
- cout << " | " << cur->x << " " << cur->y << " | ";
- }
- cout << endl << endl;
- }
- int main() {
- srand(time(0));
- PointCollection points1, points2;
- process0(points1);
- process1(points1);
- process0(points2);
- process1(points2);
- process2(points1);
- process2(points2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement