Advertisement
Joporezka1

Untitled

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