Advertisement
spacerose

oop411

May 19th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <string>
  6. #include <list>
  7. using namespace std;
  8. class Base
  9. {
  10. protected:
  11.     string name;
  12.     Base* pointer;
  13.  
  14. public:
  15.     list<Base*> pointers;
  16.     Base(Base* pointer)
  17.     {
  18.         if (pointer != NULL)
  19.         {
  20.             pointer->pointers.push_back(this);
  21.             this->pointer = pointer;
  22.         }
  23.     }
  24.     Base(Base* pointer, string name)
  25.     {
  26.         this->name = name;
  27.         if (pointer != NULL)
  28.         {
  29.             pointer->pointers.push_back(this);
  30.             this->pointer = pointer;
  31.         }
  32.     }
  33.     void setname(string name)
  34.     {
  35.         this->name = name;
  36.     }
  37.     string getname()
  38.     {
  39.         return this->name;
  40.     }
  41.  
  42.     static void tree_out(Base* obj)
  43.     {
  44.         cout << endl << obj->getname();
  45.         for (auto i : obj->pointers) cout << " " << i->getname();
  46.         for (auto i : obj->pointers)
  47.         {
  48.             if (i->pointers.size() != 0)
  49.                 tree_out(i);
  50.         }
  51.     }
  52. };
  53.  
  54. class First : public Base
  55. {
  56. public:
  57.     First(Base* pointer, string name) :Base(pointer, name) {}
  58. };
  59.  
  60. class Second : public Base
  61. {
  62. public:
  63.     Second(Base* pointer, string name) :Base(pointer, name) {}
  64. };
  65.  
  66. int main()
  67. {
  68.     int i = 0;
  69.     string rootname, head, child;
  70.     cin >> rootname;
  71.     Base root(NULL, rootname);
  72.     Base* crnt = &root;
  73.     cin >> head >> child;
  74.     while (head != child)
  75.     {
  76.         if (crnt->getname() != head)
  77.         {
  78.             for (auto* n : crnt->pointers)
  79.             {
  80.                 if (head == n->getname())
  81.                 {
  82.                     crnt = n;   //меняем crnt на имя корневого объекта текущего объекта
  83.                     break;
  84.                 }
  85.             }
  86.         }
  87.         if (i % 2 == 0 && crnt->getname() == head)
  88.             First* x = new First(crnt, child);
  89.         else if (crnt->getname() == head)
  90.             Second* x = new Second(crnt, child);
  91.         i++;
  92.         cin >> head >> child;
  93.     }
  94.     cout << root.getname();
  95.     Base::tree_out(&root);
  96.     return(0);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement