Advertisement
spacerose

KL32

May 17th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6. class Base
  7. {
  8. public:
  9.     string name, headname;
  10.     int flag;
  11.     vector <Base*> pointers;
  12.     Base(string name)
  13.     {
  14.         this->headname = "";
  15.         this->name = name;
  16.         this->flag = 1;
  17.     }
  18.     Base(string headname, string name, int num)
  19.     {
  20.         this->headname = headname;
  21.         this->name = name;
  22.         this->flag = num;
  23.     }
  24.     Base* src(string name)  //поиск корневого объекта у корневого объекта
  25.     {
  26.         static Base* sobj;
  27.         for (auto obj : pointers)
  28.         {
  29.             if (obj->name == name)
  30.             {
  31.                 sobj = obj;
  32.                 break;
  33.             }
  34.             obj->src(name);
  35.         }
  36.         return sobj;
  37.     }
  38.  
  39.  
  40.     void create(string headname, string name, int headnum, int num);
  41.     void dataout()
  42.     {
  43.         for (auto obj : pointers) {
  44.             cout <<endl<< setw(5)<<right << obj->name;
  45.             for (auto obj2 : obj->pointers) {
  46.                 cout << endl << setw(9) << right << obj2->name;
  47.                 for (auto obj3 : obj2->pointers) {
  48.                     cout << endl << setw(13) << right << obj3->name;
  49.                     for (auto obj4 : obj3->pointers) {
  50.                         cout << endl << setw(17) << right << obj4->name;
  51.                         for (auto obj5 : obj4->pointers) {
  52.                             cout << endl << setw(21) << right << obj5->name;
  53.                             for (auto obj6 : obj5->pointers) {
  54.                                 cout << endl << setw(25) << right << obj6->name;
  55.                             }
  56.                         }
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.     }
  62. };
  63. class second : public Base
  64. {
  65. public:
  66.     second(string headname, string name, int num, Base* obj) : Base(headname, name, num)
  67.     {
  68.         obj->pointers.push_back(this);
  69.     }
  70. };
  71. class third : public Base
  72. {
  73. public:
  74.     third(string headname, string name, int num, Base* obj) : Base(headname, name, num)
  75.     {
  76.         obj->pointers.push_back(this);
  77.     }
  78. };
  79. class fourth : public Base
  80. {
  81. public:
  82.     fourth(string headname, string name, int num, Base* obj) : Base(headname, name, num)
  83.     {
  84.         obj->pointers.push_back(this);
  85.     }
  86. };
  87. class fifth : public Base
  88. {
  89. public:
  90.     fifth(string headname, string name, int num, Base* obj) : Base(headname, name, num)
  91.     {
  92.         obj->pointers.push_back(this);
  93.     }
  94. };
  95. class sixth : public Base
  96. {
  97. public:
  98.     sixth(string headname, string name, int num, Base* obj) : Base(headname, name, num)
  99.     {
  100.         obj->pointers.push_back(this);
  101.     }
  102. };
  103. void Base::create(string headname, string name, int headnum, int num)
  104. {
  105.     Base* obj;
  106.     second* obj2;
  107.     third* obj3;
  108.     fourth* obj4;
  109.     fifth* obj5;
  110.     sixth* obj6;
  111.     if (headname == this->name)
  112.         obj = this;
  113.     else
  114.         obj = src(headname);
  115.     if (headnum == 2)
  116.         obj2 = new second(headname, name, num, obj);
  117.     else if (headnum == 3)
  118.         obj3 = new third(headname, name, num, obj);
  119.     else if (headnum == 4)
  120.         obj4 = new fourth(headname, name, num, obj);
  121.     else if (headnum == 5)
  122.         obj5 = new fifth(headname, name, num, obj);
  123.     else if (headnum == 6)
  124.         obj6 = new sixth(headname, name, num, obj);
  125. }
  126. int main()
  127. {
  128.     int headnum, num;
  129.     string headname, name;
  130.     cin >> name;
  131.     Base obj(name);
  132.     while (true)
  133.     {
  134.         cin >> headname;
  135.         if (headname == "endtree")
  136.             break;
  137.         cin >> name >> headnum >> num;
  138.         obj.create(headname, name, headnum, num);
  139.     }
  140.     cout << "Object tree"<<endl;
  141.     cout << obj.name;
  142.     obj.dataout();
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement