Advertisement
selebry

4_1_1 EBAL V ROT

Apr 15th, 2022
1,834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. /////////////BASE
  7. /////////////BASE
  8. /////////////BASE
  9. /////////////BASE
  10. /////////////BASE
  11. class Base {
  12.     string obj_name="";
  13.     Base* head_obj;
  14.     enum class Status { error = -1, buid, ready };
  15.     Status state;
  16.     vector<Base*> sub_objs;
  17. protected:
  18.     Base(Base* head_obj, string obj_name = "Object_root");
  19.     void set_obj_name(string obj_name);
  20.     string get_obj_name();
  21.     int get_state();
  22.     void set_head_obj(Base* head_obj);
  23.     Base* get_head_obj();
  24.     Base* get_obj_by_name(string obj_name);
  25.   //  unsigned int sub_objs_count();
  26.     void print_tree();
  27.     ~Base();
  28. public:
  29.     void set_state(int state);
  30.  
  31. };
  32. Base::Base(Base* head_obj, string obj_name)
  33.     :head_obj(head_obj), obj_name(obj_name), state(Status::buid)
  34. {
  35.     if (head_obj) head_obj->sub_objs.push_back(this);
  36. }
  37. void Base::set_obj_name(string obj_name) {
  38.     this->obj_name =obj_name;
  39. }
  40. string Base::get_obj_name() {
  41.     return obj_name;
  42. }
  43. void Base::set_state(int state) {
  44.     switch (state) {
  45.     case -1:
  46.         this->state = Status::error;
  47.         break;
  48.     case 0:
  49.         this->state = Status::buid;
  50.         break;
  51.     case 1:
  52.         this->state = Status::ready;
  53.         break;
  54.     default:
  55.         break;
  56.     }
  57. }
  58. int Base::get_state() {
  59.     switch (state) {
  60.     case Status::error:
  61.         return -1;
  62.     case Status::buid:
  63.         return 0;
  64.     case Status::ready:
  65.         return 1;
  66.     default:
  67.         break;
  68.     }
  69. }
  70. void Base::set_head_obj(Base* head_obj) {
  71.     this->head_obj = head_obj;
  72. }
  73. Base* Base::get_head_obj() {
  74.     return head_obj;
  75. }
  76. Base* Base::get_obj_by_name(string obj_name) {
  77.     if (this->obj_name == obj_name) return this;
  78.     Base* val=nullptr;
  79.     if (!sub_objs.empty()) {
  80.         for (int i = 0; i < sub_objs.size(); i++) {
  81.             if (sub_objs[i]->obj_name == obj_name) val = sub_objs[i];
  82.         }
  83.         if (val == nullptr) {
  84.             for (int i = 0; i < sub_objs.size(); i++) {
  85.                 val = sub_objs[i]->get_obj_by_name(obj_name);
  86.             }
  87.         }
  88.     }
  89.     if (val == nullptr) set_state(-1);
  90.     return val;
  91. }
  92. /*unsigned int sub_objs_count();
  93. */
  94. void Base::print_tree() {
  95.     if (!head_obj) cout << obj_name;
  96.     if (sub_objs.size() > 0) {
  97.         cout << '\n' << obj_name;
  98.         for (int i = 0; i < sub_objs.size(); i++)
  99.             cout << ' ' << sub_objs[i]->get_obj_name();
  100.         for (int i = 0; i < sub_objs.size(); i++) sub_objs[i]->print_tree();
  101.     }
  102.  
  103. }
  104. Base::~Base() {
  105.     delete head_obj;
  106.     sub_objs.clear();
  107. }
  108.  
  109. /////////////SUB
  110. /////////////SUB
  111. /////////////SUB
  112. /////////////SUB
  113. /////////////SUB
  114.  
  115. class Sub :public Base {
  116. public:
  117.     Sub(Base* head_obj, string obj_name = "Object_root");
  118.     void set_state(int state);
  119. };
  120. Sub::Sub(Base* head_obj, string obj_name) :Base(head_obj, obj_name) {};
  121.  
  122. void Sub::set_state(int state) {
  123.     Base::set_state(state);
  124. }
  125.  
  126. /////////////APP
  127. /////////////APP
  128. /////////////APP
  129. /////////////APP
  130. /////////////APP
  131. class App:public Base {
  132. public:
  133.     App(Base* head=nullptr);
  134.     void build_tree_objs();
  135.     int exec_app();
  136. };
  137. App::App(Base* head) :Base(head) {}
  138. void App::build_tree_objs() {
  139.     string obj_name1,obj_name2;
  140.     cin >> obj_name1;
  141.     set_obj_name(obj_name1);
  142.     while (true) {
  143.         cin >> obj_name1 >> obj_name2;
  144.         if (obj_name1 == obj_name2) return;
  145.         Sub* sub = new Sub(get_obj_by_name(obj_name1), obj_name2);
  146.         get_obj_by_name(obj_name2)->set_state(1);
  147.     }
  148. }
  149. int App::exec_app() {
  150.     print_tree();
  151.     return 0;
  152. }
  153. int main()
  154. {
  155.     App ob_cl_application(nullptr);
  156.     ob_cl_application.build_tree_objs();
  157.     return ob_cl_application.exec_app();
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement