Advertisement
spacerose

KL31

Apr 15th, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. class Base
  6. {
  7. public:
  8.     string name, headname;
  9.     int flag;
  10.     vector <Base*> pointers;
  11.     Base(string name)
  12.     {
  13.         this->headname = "";
  14.         this->name = name;
  15.         this->flag = 1;
  16.     }
  17.     Base(string headname, string name, int num)
  18.     {
  19.         this->headname = headname;
  20.         this->name = name;
  21.         this->flag = num;
  22.     }
  23.     Base* src(string name)
  24.     {
  25.         static Base* sobj;
  26.         for (auto obj : pointers)
  27.         {
  28.             if (obj->name == name)
  29.             {
  30.                 sobj = obj;
  31.                 break;
  32.             }
  33.             obj->src(name);
  34.         }
  35.         return sobj;
  36.     }
  37.  
  38.  
  39.     void create(string headname, string name, int headnum, int num);
  40.     void dataout()
  41.     {
  42.         for (auto obj : pointers)
  43.         {
  44.             cout << "\nThe object " << obj->name;
  45.             if (obj->flag >= 1) cout << " is ready";
  46.             else cout << " is not ready";
  47.             obj->dataout();
  48.         }
  49.     }
  50. };
  51. class second : public Base
  52. {
  53. public:
  54.     second(string headname, string name, int num, Base* obj) : Base(headname, name, num)
  55.     {
  56.         obj->pointers.push_back(this);
  57.     }
  58. };
  59. class third : public Base
  60. {
  61. public:
  62.     third(string headname, string name, int num, Base* obj) : Base(headname, name, num)
  63.     {
  64.         obj->pointers.push_back(this);
  65.     }
  66. };
  67. class fourth : public Base
  68. {
  69. public:
  70.     fourth(string headname, string name, int num, Base* obj) : Base(headname, name, num)
  71.     {
  72.         obj->pointers.push_back(this);
  73.     }
  74. };
  75. void Base::create(string headname, string name, int headnum, int num)
  76. {
  77.     Base* obj;
  78.     second* obj2;
  79.     third* obj3;
  80.     fourth* obj4;
  81.     if (headname == this->name)
  82.         obj = this;
  83.     else
  84.         obj = src(headname);
  85.     if (headnum == 2)
  86.         obj2 = new second(headname, name, num, obj);
  87.     else if (headnum == 3)
  88.         obj3 = new third(headname, name, num, obj);
  89.     else if (headnum == 4)
  90.         obj4 = new fourth(headname, name, num, obj);
  91. }
  92. int main()
  93. {
  94.     int headnum, num;
  95.     string headname, name;
  96.     cin >> name;
  97.     Base obj(name);
  98.     while (true)
  99.     {
  100.         cin >> headname;
  101.         if (headname == "endtree")
  102.             break;
  103.         cin >> name >> headnum >> num;
  104.         obj.create(headname, name, headnum, num);
  105.     }
  106.     cout << "The object " << obj.name << " is ready";
  107.     obj.dataout();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement