Advertisement
Joporezka1

Untitled

Apr 27th, 2022
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 KB | None | 0 0
  1. #include "cl_base.h"
  2. #include "child2.h"
  3. #include "child3.h"
  4. #include "child4.h"
  5. #include "child5.h"
  6. #include "child6.h"
  7. #include "cl_application.h"
  8. #include <iostream>
  9. #include <string>
  10. using namespace std;
  11.  
  12.  
  13. void cl_application::bild_tree_objects() {
  14.     string parent_path;
  15.     string child;
  16.     cl_base* parent=nullptr;
  17.    
  18.     int cl_num,readiness1;
  19.     cin >> parent_path;
  20.     //создание головного объекта
  21.     this->set_name(parent_path);
  22.    
  23.     while (true)
  24.     {
  25.         parent = nullptr;
  26.         cin >> parent_path;
  27.         if(parent_path=="endtree") break;
  28.        
  29.         string temp_obj = "";
  30.         if(parent_path == "/"){
  31.             parent = this;
  32.         }else if(parent_path[0]=='/' and parent_path[1]=='/'){
  33.             for(int i=2;i<parent_path.length();i++)
  34.                 temp_obj+=parent_path[i];
  35.             parent = this->get_object_by_name(temp_obj);
  36.             if(parent == nullptr){
  37.                 cout<<"The head object "<<parent_path<<" is not found\n";
  38.                 return;
  39.             }
  40.         }else{ // /01/02/..../0n
  41.             int cnt_objects=0;
  42.             for(int i=1;i<parent_path.length();i++){
  43.                 if(parent_path[i]=='/'){
  44.                     if(cnt_objects==0){
  45.                         parent = this->get_object_by_name(temp_obj,1);
  46.                     }else{
  47.                         parent = parent->get_object_by_name(temp_obj,1);
  48.                     }
  49.                     cnt_objects++;
  50.                     if(parent==nullptr){
  51.                         cout<<"The head object "<<temp_obj<<" is not found\n";
  52.                         return;
  53.                     }
  54.                     if(i!= parent_path.length()-1) temp_obj="";
  55.                 }else{
  56.                 //get substr
  57.                     temp_obj+=parent_path[i];
  58.                 }
  59.             }
  60.             if(cnt_objects==0)
  61.                 parent = this->get_object_by_name(temp_obj,1);
  62.             else
  63.                 parent = parent->get_object_by_name(temp_obj,1);
  64.             if(parent==nullptr){
  65.                 cout<<"The head object "<<temp_obj<<" is not found\n";
  66.                 return;
  67.             }
  68.        
  69.         }
  70.         cin >> child;
  71.         cin >> cl_num;
  72.         if(cl_num==2) child2* ch = new child2(parent,child);
  73.         else if(cl_num==3) child3* ch = new child3(parent,child);
  74.         else if(cl_num==4) child4* ch = new child4(parent,child);
  75.         else if(cl_num==5) child5* ch = new child5(parent,child);
  76.         else if(cl_num==6) child6* ch = new child6(parent,child);
  77.     }
  78.    
  79. }
  80.  
  81. int cl_application::exec_app() {
  82.     cout<<"Object tree\n";
  83.     this->print_tree();
  84.     cout<<endl;
  85.    
  86.     while(true){
  87.         string command, arg;
  88.         cin>>command;
  89.         if(command=="END") return 0;
  90.         cin>>arg;
  91.         //ввели arg и command -> обработка
  92.        
  93.         //process comma
  94.         cl_base* obj_search = nullptr;
  95.         string temp_name="";
  96.        
  97.         if(arg.length()==1 && arg[0] == '/'){ // /-root
  98.             obj_search=this;
  99.         }else if(arg.length()==1 && arg[0]=='.'){ // .-cur
  100.             obj_search = this->current;
  101.         }else if(arg[0]=='/' && arg[1]=='/'){ // // find from root
  102.            
  103.             for(int i=2;i<arg.length();i++)
  104.                 temp_name+=arg[i];
  105.             obj_search = this->get_object_by_name(temp_name);
  106.            
  107.         }else if(arg.length()>1 && arg[0]=='/'){ // /j1/j2/j3..../jn - abs path from root
  108.            
  109.             int cnt_objects=0;
  110.             for(int i=1;i<arg.length();i++){
  111.                 if(arg[i] == '/' or i==arg.length()-1){
  112.                     if(i==arg.length()-1) temp_name+=arg[i];
  113.                     if(cnt_objects==0){
  114.                         obj_search = this->get_object_by_name(temp_name,1);
  115.                     }else{
  116.                         obj_search = obj_search->get_object_by_name(temp_name,1);
  117.                     }
  118.                     cnt_objects++;
  119.                     temp_name = "";
  120.                     if(obj_search == nullptr){
  121.                         break;
  122.                     }
  123.                    
  124.                 }else{
  125.                     temp_name+=arg[i];
  126.                 }
  127.             }
  128.         }else{ // j1/j2/j3..... - относительный путь от cur
  129.  
  130.             int cnt_objects = 0;
  131.             for(int i=0;i<arg.length();i++){
  132.                 if(arg[i]=='/' or i==arg.length()-1){
  133.                     if(i==arg.length()-1) temp_name+=arg[i];
  134.                     if(cnt_objects==0){
  135.                         obj_search = current->get_object_by_name(temp_name,1);
  136.                     }
  137.                     else{
  138.                         obj_search = obj_search->get_object_by_name(temp_name,1);
  139.                     }
  140.                     cnt_objects++;
  141.                     temp_name = "";
  142.                     if(obj_search==nullptr){
  143.                         //cout<<"broke"<<endl;
  144.                         break;
  145.                     }
  146.                 }else
  147.                     temp_name+=arg[i];
  148.             }
  149.            
  150.         }
  151.        
  152.         //process output
  153.         if(command == "SET"){
  154.             if(obj_search!=nullptr){
  155.                 cout<<"Object is set: "<<obj_search->get_name()<<endl;
  156.                 this->current = obj_search;
  157.             }else{
  158.                 cout<<"Object is not found: "<<obj_search->get_name()<<" "<<arg<<endl;
  159.             }
  160.         }else{//FIND
  161.             if(obj_search!=nullptr){
  162.                 cout<<arg<<"     Object name: "<<obj_search->get_name()<<endl;
  163.             }else{
  164.                 cout<<arg<<"     Object is not found"<<endl;
  165.             }
  166.         }
  167.        
  168.     }
  169.     return 0;
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement