Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <list>
- using namespace std;
- class Base
- {
- protected:
- string name;
- Base* pointer;
- public:
- list<Base*> pointers;
- Base(Base* pointer)
- {
- if (pointer != NULL)
- {
- pointer->pointers.push_back(this);
- this->pointer = pointer;
- }
- }
- Base(Base* pointer, string name)
- {
- this->name = name;
- if (pointer != NULL)
- {
- pointer->pointers.push_back(this);
- this->pointer = pointer;
- }
- }
- void setname(string name)
- {
- this->name = name;
- }
- string getname()
- {
- return this->name;
- }
- static void tree_out(Base* obj)
- {
- cout << endl << obj->getname();
- for (auto i : obj->pointers) cout << " " << i->getname();
- for (auto i : obj->pointers)
- {
- if (i->pointers.size() != 0)
- tree_out(i);
- }
- }
- };
- class First : public Base
- {
- public:
- First(Base* pointer, string name) :Base(pointer, name) {}
- };
- class Second : public Base
- {
- public:
- Second(Base* pointer, string name) :Base(pointer, name) {}
- };
- int main()
- {
- int i = 0;
- string rootname, head, child;
- cin >> rootname;
- Base root(NULL, rootname);
- Base* crnt = &root;
- cin >> head >> child;
- while (head != child)
- {
- if (crnt->getname() != head)
- {
- for (auto* n : crnt->pointers)
- {
- if (head == n->getname())
- {
- crnt = n; //меняем crnt на имя корневого объекта текущего объекта
- break;
- }
- }
- }
- if (i % 2 == 0 && crnt->getname() == head)
- First* x = new First(crnt, child);
- else if (crnt->getname() == head)
- Second* x = new Second(crnt, child);
- i++;
- cin >> head >> child;
- }
- cout << root.getname();
- Base::tree_out(&root);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement