Advertisement
malixds_

Untitled

Apr 1st, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. //application.cpp  
  2. #include "Application.h"
  3.     #include "Secondary.h"
  4.     #include "Obect.h"
  5.     using namespace std;
  6.     void Application::build_Tree()
  7.     {
  8.         Object* curr_parent, * curr_child;
  9.         string parent_name, child_name;
  10.         cin >> parent_name;
  11.         set_name(parent_name);
  12.         curr_parent = this;
  13.         cin >> parent_name >> child_name;
  14.         while (true)
  15.         {
  16.             if (parent_name == child_name) return;
  17.             if (parent_name != curr_parent->get_name())
  18.             {
  19.                 if (parent_name != curr_parent->get_name())
  20.                 {
  21.                     curr_parent == curr_child;
  22.                 }
  23.                 else
  24.                     continue;
  25.             }
  26.             curr_child = new Secondary(curr_parent, child_name);
  27.             cin >> parent_name >> child_name;
  28.         }
  29.     }
  30.     Application::Application(Object* parent) : Object(parent) {}
  31.     int Application::exec()
  32.     {
  33.         print();
  34.         return 0;
  35.     }
  36. //application.h
  37. #ifndef APPLICATION_H
  38. #define APPLICATION_H
  39. #include "Obect.h"
  40. class Application : public Object
  41. {
  42. public:
  43.     void build_Tree();
  44.     Application(Object* parent = 0);
  45.     int exec();
  46. };
  47. #endif
  48. //main.cpp
  49. #include "Application.h"
  50. int main()
  51. {
  52.     setlocale(LC_ALL, "RUS");
  53.     Application obj;
  54.     obj.build_Tree();
  55.     return obj.exec();
  56. }
  57. //object.h
  58. #ifndef OBECT_H
  59. #define OBECT_H
  60. #include <iostream>
  61. #include <vector>
  62. #include <string>
  63. using namespace std;
  64. class Object
  65. {
  66.     string name;
  67.     Object* parent;
  68.     vector<Object*> children;
  69. public:
  70.     Object(Object*, string = "root");
  71.     void set_parent(Object*);
  72.     Object* get_parent();
  73.     void set_name(string);
  74.     string get_name();
  75.     void print();
  76.     ~Object();
  77. };
  78. #endif
  79. //object.cpp
  80. #include "Obect.h"
  81. using namespace std;
  82. Object::Object(Object* parent, string name)
  83. {
  84.     this->parent = NULL;
  85.     this->name = "root";
  86.     set_parent(parent);
  87.     set_name(name);
  88. }
  89. void Object::set_parent(Object* p_parent)
  90. {
  91.     if (parent)
  92.     {
  93.         for (int i = 0; i < parent->children.size(); i++)
  94.         {
  95.             if (parent->children[i] == this)
  96.                 parent->children.erase(children.begin() + i);
  97.         }
  98.     }
  99.     parent = p_parent;
  100.     if (parent)
  101.         parent->children.push_back(this);
  102. }
  103. Object* Object::get_parent()
  104. {
  105.     return this->parent;
  106. }
  107. void Object::set_name(string name)
  108. {
  109.     this->name = name;
  110. }
  111. string Object::get_name()
  112. {
  113.     return name;
  114. }
  115. void Object::print()
  116. {
  117.     if (!parent) cout << get_name();
  118.     vector<Object*>::iterator children_iterator;
  119.     if (children.empty()) return;
  120.     cout << endl << name;
  121.     children_iterator = children.begin();
  122.     while (children_iterator != children.end())
  123.     {
  124.         cout << " " << (*children_iterator)->get_name();
  125.         children_iterator++;
  126.     }
  127.     children_iterator--;
  128.     (*children_iterator)->print();
  129. }
  130. Object::~Object()
  131. {
  132.     for (int i = 0; i < children.size(); i++)
  133.     {
  134.         delete children[i];
  135.     }
  136. }
  137. //secondary.cpp
  138. #include <iostream>
  139. #include <string>
  140. #include "Secondary.h"
  141. using namespace std;
  142. Secondary::Secondary(Object* parent, string name) : Object(parent, name) {}
  143. //secondary.h
  144. #ifndef SECONDARY_H
  145. #define SECONDARY_H
  146. #include "Obect.h"
  147. class Secondary : public Object
  148. {
  149. public:
  150.     Secondary(Object*, string);
  151. };
  152. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement