Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.26 KB | None | 0 0
  1. #include <iostream>
  2. #define endl '\n';
  3. using namespace std;
  4.  
  5.  
  6. struct bst{
  7.     string nome;
  8.     bst* left;
  9.     bst* right;
  10.     bst* up;
  11.     bst* down;
  12.     bool isDirectory;
  13.  
  14.     void setRoot(){
  15.         this->nome = "/";
  16.         this->up = nullptr;
  17.         this->down = nullptr;
  18.         this->left = nullptr;
  19.         this->right = nullptr;
  20.         isDirectory = true;
  21.     }
  22. };
  23.  
  24. bst *touch(bst *root, bst* up, string nome){
  25.     if(root == nullptr){
  26.         bst *n = new bst;
  27.         n->nome = nome;
  28.         n->left = nullptr;
  29.         n->right = nullptr;
  30.         n->down = nullptr;
  31.         n->up = up;
  32.         n->isDirectory = false;
  33.         return n;
  34.     }else if(nome < root->nome){
  35.         root->left = touch(root->left, up,nome);
  36.         return root;
  37.     }else{
  38.         root->right = touch(root->right,up,nome);
  39.         return root;
  40.     }
  41. }
  42.  
  43. bst *mkdir(bst *root, bst* up, string nome){
  44.     if(root == nullptr){
  45.         bst *n = new bst;
  46.         n->nome = nome;
  47.         n->left = nullptr;
  48.         n->right = nullptr;
  49.         n->down = nullptr;
  50.         n->up = up;
  51.         n->isDirectory = true;
  52.         return n;
  53.     }else if(nome < root->nome){
  54.         root->left = mkdir(root->left, up,nome);
  55.         return root;
  56.     }else{
  57.         root->right = mkdir(root->right,up,nome);
  58.         return root;
  59.     }
  60. }
  61.  
  62. string pwd(bst *root){
  63.     return root->nome;
  64. }
  65.  
  66. string pre_order(bst* root, string indentacao){
  67.     if(root == nullptr || root->up == nullptr){
  68.         return "";
  69.     }
  70.     cout << indentacao << root->nome << endl;
  71.     if(root->isDirectory ){
  72.         pre_order(root->down, indentacao+"    ");
  73.     }
  74.     pre_order(root->left,indentacao);
  75.     pre_order(root->right,indentacao);
  76. }
  77.  
  78. string in_order(bst* root, string indentacao){
  79.     if(root == nullptr || root->up == nullptr){
  80.         return "";
  81.     }
  82.     in_order(root->left, indentacao);
  83.     cout << indentacao << root->nome << endl;
  84.     if(root->isDirectory ){
  85.         indentacao += "    ";
  86.         in_order(root->down, indentacao);
  87.         indentacao = "";
  88.     }
  89.     in_order(root->right,indentacao);
  90. }
  91.  
  92. string pos_order(bst* root, string indentacao){
  93.     if(root == nullptr || root->up == nullptr){
  94.         return "";
  95.     }
  96.     pos_order(root->left,indentacao);
  97.     pos_order(root->right,indentacao);
  98.     cout << indentacao << root->nome << endl;
  99.     if(root->isDirectory ){
  100.         pos_order(root->down, indentacao+"    ");
  101.     }
  102. }
  103.  
  104. string ls(bst* root){
  105.     if(root == nullptr || root->up == nullptr){
  106.         return "";
  107.     }
  108.     ls(root->left);
  109.     cout << root->nome << endl;
  110.     ls(root->right);
  111. }
  112.  
  113. int busca_previa(bst *root, string nome){
  114.     if(root == nullptr){
  115.         return 0;
  116.     }else if(nome == root->nome){
  117.         if(root->isDirectory){
  118.             return 1;
  119.         }
  120.         return 2;
  121.     }else if(nome < root->nome){
  122.         return busca_previa(root->left, nome);
  123.     }else{
  124.         return busca_previa(root->right,nome);
  125.     }
  126. }
  127.  
  128. bst* cd(bst* root, string nome){
  129.  
  130.     if(root == nullptr){
  131.         return nullptr ;
  132.     }else if(nome == root->nome){
  133.         return root;
  134.     }else if(nome < root->nome){
  135.         return cd(root->left, nome);
  136.     }else{
  137.         return cd(root->right,nome);
  138.     }
  139. }
  140. struct bst_min{
  141.     bst* root;
  142.     string nome;
  143. };
  144.  
  145. bst_min* bst_delete_min(bst* root){
  146.     if(root->left == nullptr){
  147.         bst_min* opa = new bst_min;
  148.         opa->nome = root->nome;
  149.         opa->root = root->right;
  150.         delete root;
  151.         return opa;
  152.     }else{
  153.         bst_min* rm = bst_delete_min(root -> left);
  154.         root->left = rm->root;
  155.         root->nome = rm->nome;
  156.         return rm;
  157.     }
  158.  
  159. }
  160.  
  161. bst* rm(bst* root, string nome){
  162.     if(root == nullptr){
  163.         cout << "No such file or directory" << endl;
  164.         return nullptr;
  165.     }else if(nome < root->nome){
  166.         root->left = rm(root->left,nome);
  167.         return root;
  168.     }else if(nome > root->nome){
  169.         root->right = rm(root->right,nome);
  170.         return root;
  171.     }else{
  172.         if(root->left == nullptr){
  173.             bst* r = root->right;
  174.             delete root;
  175.             return r;
  176.         }else if(root->right == nullptr){
  177.             bst* l = root->left;
  178.             delete root;
  179.             return l;
  180.         }else{
  181.             bst_min* rm = bst_delete_min(root -> right);
  182.             root->right = rm->root;
  183.             root->nome = rm->nome;
  184.             return root;
  185.         }
  186.     }
  187.  
  188. }
  189.  
  190.  
  191.  
  192.  
  193. int main(){
  194.     bst *pimba = new bst;
  195.     pimba->setRoot();
  196.     bst* current = pimba;
  197. //    string indentacao = "";
  198. //
  199. //    cout << pwd(current) << endl;
  200. //    current->down = mkdir(current->down,current,"Q");
  201. //    current->down = mkdir(current->down,current,"A");
  202. //    current->down = mkdir(current->down,current,"Z");
  203. //    current = cd(current->down, "A");
  204. //    current->down = touch(current->down,current, "W");
  205. //    current->down = mkdir(current->down,current, "S");
  206. //    current->down = touch(current->down,current, "X");
  207. //    current = cd(current->down, "S");
  208. //    current->down = touch(current->down,current, "1");
  209. //    current->down = touch(current->down,current, "0");
  210. //    current->down = touch(current->down,current, "2");
  211. //    current->down = touch(current->down,current, "9");
  212. //    current = current->up;
  213. //    if(busca_previa(current->down,"P") == 0){
  214. //        cout << "No such file or directory" << endl;
  215. //    }else if(busca_previa(current->down, "P") == 2){
  216. //        cout << "Not a directory" << endl;
  217. //    }else if(busca_previa(current->down,"P") == 1){
  218. //        current = cd(current->down,"P");
  219. //    }
  220. //    ls(current->down);
  221. //    if(busca_previa(current->down,"X") != 0){
  222. //        cout << "File exists" << endl;
  223. //            }else{
  224. //        current->down = mkdir(current->down,current,"X");
  225. //    }
  226. //    current = current->up;
  227. //    current = cd(current->down, "Q");
  228. //    current->down = touch(current->down,current, "E");
  229. //    current->down = touch(current->down,current, "D");
  230. //    current = current->up;
  231. //    current = cd(current->down, "Z");
  232. //    current->down = touch(current->down, current, "X");
  233. //    current = current->up;
  234. //    in_order(current->down,indentacao);
  235.  
  236.     string comando;
  237.     string d;
  238.  
  239.     while(cin >> comando){
  240.         string indentacao;
  241.         if(comando == "cd"){
  242.             cin >> d;
  243.             if(d == ".."){
  244.                 if(current->up != nullptr){
  245.                     current = current->up;
  246.                 }
  247.             }else{
  248.                 if(busca_previa(current->down,d) == 0){
  249.                     cout << "No such file or directory" << endl;
  250.                 }else if(busca_previa(current->down, d) == 2){
  251.                     cout << "Not a directory" << endl;
  252.                 }else if(busca_previa(current->down,d) == 1){
  253.                     current = cd(current->down,d);
  254.                 }
  255.             }
  256.  
  257.         }else if(comando == "ls"){
  258.             ls(current->down);
  259.  
  260.         }else if(comando == "touch"){
  261.             string f;
  262.             cin >> f;
  263.             if(busca_previa(current->down,f) != 0){
  264.                 cout << "File exists" << endl;
  265.             }else{
  266.                 current->down = touch(current->down,current,f);
  267.             }
  268.  
  269.         }else if(comando == "mkdir"){
  270.             string nD;
  271.             cin >> nD;
  272.             if(busca_previa(current->down,nD) != 0){
  273.                 cout << "File exists" << endl;
  274.             }else{
  275.                 current->down = mkdir(current->down,current,nD);
  276.             }
  277.  
  278.         }else if(comando == "pwd"){
  279.             cout << pwd(current) << endl;
  280.  
  281.         }else if(comando == "tree"){
  282.             string arg;
  283.             cin >> arg;
  284.             if(arg == "--pre-order"){
  285.                 pre_order(current->down,indentacao);
  286.  
  287.             }else if(arg == "--in-order"){
  288.                 in_order(current->down,indentacao);
  289.  
  290.             }else if(arg == "--post-order"){
  291.                 pos_order(current->down,indentacao);
  292.  
  293.             }else{
  294.                 cout << "Illegal option" << endl;
  295.             }
  296.  
  297.         }else if(comando == "rm"){
  298.             string f;
  299.             cin >> f;
  300.             current->down = rm(current,f);
  301.  
  302.         }else{
  303.             cout << "Command not found" << endl;
  304.         }
  305.  
  306.     }
  307.  
  308.     return 0;
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement