Kame3

Windows Explorer lab7.1

Dec 25th, 2020 (edited)
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.41 KB | None | 0 0
  1. Windows Explorer Problem 1 (0 / 0)
  2.  
  3. Потребно е да имплементирате Windows Explorer со помош на дрво. Јазлите треба да ви бидат фолдери/датотеки. Почетно ќе имате само еден фолдер c: и тој ви е тековен фолдер. Ќе ви биде дадена низа од команди што можат да бидат во еден од следните типови:
  4.  
  5. CREATE [име на фолдер/датотека] - треба да креирате нов фолдер/датотека во тековниот. Треба да внимавате при додавањето, новиот фолдер/датотека треба да се смести на онаа позиција така што сите фолдери/датотеки во тековниот фолдер ќе бидат подредени лексикографски
  6.  
  7. OPEN [име на фолдер/датотека] - го отварате фолдерот во тековниот фолдер и се менува тековниот фолдер
  8.  
  9. DELETE [име на фолдер/датотека] - го бришете фолдерот/датотеката
  10.  
  11. BACK - се враќате назад во претходниот фолдер
  12.  
  13. PATH - се печати патеката на тековниот фолдер на пример: c:\users\darko\mydocuments
  14.  
  15. PRINT - се печати целата структура на датотечниот систем така што секој фолдер/датотека се печати во еден ред со онолку празни места колку што е нивото на тој фолдер/датотека
  16.  
  17. Забелешка: Имињата на фолдерите/датотеките ќе бидат составени само од еден збор што содржи мали латинични букви. Сите операции ќе бидат валидни.
  18.  
  19. Име на класата: WindowsExplorer
  20.  
  21.  
  22. import java.io.BufferedReader;
  23. import java.io.InputStreamReader;
  24. import java.util.StringTokenizer;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.Iterator;
  28. import java.util.NoSuchElementException;
  29.  
  30. interface Tree<E> {
  31.     ////////////Accessors ////////////
  32.  
  33.     public Node<E> root();
  34.  
  35.     public Node<E> parent(Node<E> node);
  36.  
  37.     public int childCount(Node<E> node);
  38.  
  39.     ////////////Transformers ////////////
  40.     public void makeRoot(E elem);
  41.  
  42.     public Node<E> addChild(Node<E> node, E elem);
  43.  
  44.     public void remove(Node<E> node);
  45.  
  46.     ////////////Iterator ////////////
  47.     public Iterator<E> children(Node<E> node);
  48.    
  49. }
  50.  
  51. interface Node<E> {
  52.  
  53.         public E getElement();
  54.  
  55.         public void setElement(E elem);
  56. }
  57.  
  58.  
  59. class SLLTree<E> implements Tree<E> {
  60.    
  61.     public SLLNode<E> root;
  62.  
  63.     public SLLTree() {
  64.         root = null;
  65.     }
  66.  
  67.     public Node<E> root() {
  68.         return root;
  69.     }
  70.  
  71.     public Node<E> parent(Node<E> node) {
  72.         return ((SLLNode<E>) node).parent;
  73.     }
  74.  
  75.     public int childCount(Node<E> node) {
  76.         SLLNode<E> tmp = ((SLLNode<E>) node).firstChild;
  77.         int num = 0;
  78.         while (tmp != null) {
  79.             tmp = tmp.sibling;
  80.             num++;
  81.         }
  82.         return num;
  83.     }
  84.  
  85.     public void makeRoot(E elem) {
  86.         root = new SLLNode<E>(elem);
  87.     }
  88.  
  89.     public Node<E> addChild(Node<E> node, E elem) {
  90.         SLLNode<E> tmp = new SLLNode<E>(elem);
  91.         SLLNode<E> curr = (SLLNode<E>) node;
  92.         //tmp.sibling = curr.firstChild;
  93.         //curr.firstChild = tmp;
  94.         //tmp.parent = curr;
  95.         //return tmp;
  96.         if(curr.firstChild==null || tmp.compareTo(curr.firstChild)<0) {
  97.             tmp.sibling = curr.firstChild;
  98.             curr.firstChild = tmp;
  99.             tmp.parent = curr;
  100.             return tmp;
  101.         }
  102.         SLLNode<E> node1 = (SLLNode<E>) curr.firstChild;
  103.         SLLNode<E> node2 = (SLLNode<E>) node1.sibling;
  104.         while(node2!=null) {
  105.             if(tmp.compareTo(node2)<0) {
  106.                 node1.sibling=tmp;
  107.                 tmp.sibling=node2;
  108.                 tmp.parent=curr;
  109.                 return tmp;
  110.             }
  111.             node1=node1.sibling;
  112.             node2=node2.sibling;
  113.         }
  114.       node1.sibling=tmp;
  115.       tmp.parent=curr;
  116.       return tmp;
  117.        
  118.     }
  119.    
  120.     public Node<E> getNode(Node<E> node,E elem){
  121.         SLLNode<E> tmp=(SLLNode<E>) node;
  122.         tmp=tmp.firstChild;
  123.         while(tmp!=null) {
  124.             if(tmp.element.equals(elem)) {
  125.                 return tmp;
  126.             }
  127.             tmp=tmp.sibling;
  128.         }
  129.         throw new NoSuchElementException();
  130.     }
  131.    
  132.    
  133.  
  134.     public void remove(Node<E> node) {
  135.         SLLNode<E> curr = (SLLNode<E>) node;
  136.         if (curr.parent != null) {
  137.             if (curr.parent.firstChild == curr) {
  138.                 // The node is the first child of its parent
  139.                 // Reconnect the parent to the next sibling
  140.                 curr.parent.firstChild = curr.sibling;
  141.             } else {
  142.                 // The node is not the first child of its parent
  143.                 // Start from the first and search the node in the sibling list and remove it
  144.                 SLLNode<E> tmp = curr.parent.firstChild;
  145.                 while (tmp.sibling != curr) {
  146.                     tmp = tmp.sibling;
  147.                 }
  148.                 tmp.sibling = curr.sibling;
  149.             }
  150.         } else {
  151.             root = null;
  152.         }
  153.     }
  154.  
  155.     class SLLTreeIterator<T> implements Iterator<T> {
  156.  
  157.         SLLNode<T> start, current;
  158.  
  159.         public SLLTreeIterator(SLLNode<T> node) {
  160.             start = node;
  161.             current = node;
  162.         }
  163.  
  164.         public boolean hasNext() {
  165.             return (current != null);
  166.         }
  167.  
  168.         public T next() throws NoSuchElementException {
  169.             if (current != null) {
  170.                 SLLNode<T> tmp = current;
  171.                 current = current.sibling;
  172.                 return tmp.getElement();
  173.             } else {
  174.                 throw new NoSuchElementException();
  175.             }
  176.         }
  177.  
  178.         public void remove() {
  179.             if (current != null) {
  180.                 current = current.sibling;
  181.             }
  182.         }
  183.     }
  184.  
  185.     public Iterator<E> children(Node<E> node) {
  186.         return new SLLTreeIterator<E>(((SLLNode<E>) node).firstChild);
  187.     }
  188.    
  189.     void printTreeRecursive(Node<E> node, int level) {
  190.         if (node == null)
  191.             return;
  192.         int i;
  193.         SLLNode<E> tmp;
  194.  
  195.         for (i=0;i<level;i++)
  196.             System.out.print(" ");
  197.         System.out.println(node.getElement().toString());
  198.         tmp = ((SLLNode<E>)node).firstChild;
  199.         while (tmp != null) {
  200.             printTreeRecursive(tmp, level+1);
  201.             tmp = tmp.sibling;
  202.         }
  203.     }
  204.    
  205.     public void printTree() {
  206.         printTreeRecursive(root, 0);
  207.     }
  208.    
  209. }
  210.  
  211. class SLLNode<P> implements Node<P>, Comparable<SLLNode<P>> {
  212.  
  213.     // Holds the links to the needed nodes
  214.     public SLLNode<P> parent, sibling, firstChild;
  215.     // Hold the data
  216.     public P element;
  217.  
  218.     public SLLNode(P o) {
  219.         element = o;
  220.         parent = sibling = firstChild = null;
  221.     }
  222.  
  223.     public P getElement() {
  224.         return element;
  225.     }
  226.  
  227.     public void setElement(P o) {
  228.         element = o;
  229.     }
  230.    
  231.     @Override
  232.     public int compareTo(SLLNode<P> arg0) {
  233.         String s1=(String)element;
  234.         String s2=(String)arg0.element;
  235.         return s1.compareTo(s2);
  236.     }
  237. }
  238.  
  239. public class WindowsExplorer {
  240.    
  241.     public static void path(SLLNode<String> node) {
  242.         if(node==null) {
  243.             return;
  244.         }
  245.         path(node.parent);
  246.         System.out.print(node.element+"\\");
  247.     }
  248.    
  249.     public static void main(String[] args) throws Exception {
  250.         int i,j,k;
  251.        
  252.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  253.        
  254.         int N = Integer.parseInt(br.readLine());
  255.         String commands[] = new String[N];
  256.        
  257.         for (i=0;i<N;i++)
  258.             commands[i] = br.readLine();
  259.            
  260.         br.close();
  261.        
  262.         SLLTree<String> tree = new SLLTree<String>();
  263.         tree.makeRoot("c:");
  264.         SLLNode<String> node=tree.root;
  265.        
  266.         // vasiot kod stoi ovde
  267.         //int flag=0;
  268.        
  269.         for(String command:commands) {
  270.             String parts[]=command.split("\\s");
  271.             if(parts[0].equals("CREATE")) {
  272.                 tree.addChild(node, parts[1]);
  273.             }else if(parts[0].equals("OPEN")) {
  274.                 node=(SLLNode<String>)tree.getNode(node, parts[1]);
  275.             }else if(parts[0].equals("DELETE")) {
  276.                 SLLNode<String> toDelete=node.firstChild;
  277.                 while(toDelete!=null) {
  278.                     if(toDelete.element.equals(parts[1])){
  279.                         tree.remove(toDelete);
  280.                     }
  281.                     toDelete=toDelete.sibling;
  282.                 }
  283.             } else if(parts[0].equals("BACK")) {
  284.                 node=node.parent;
  285.             } else if(parts[0].equals("PRINT")) {
  286.                 tree.printTree();
  287.             } else if(parts[0].equals("PATH")) {
  288.                 SLLNode<String> tmp=node;
  289.                 path(tmp);
  290.                 System.out.print("\n");
  291.             }
  292.         }
  293.        
  294.        
  295.        
  296.     }
  297.    
  298. }
  299.  
  300.  
  301.  
  302. Sample input
  303.  
  304. 15
  305. CREATE a
  306. OPEN a
  307. CREATE b
  308. CREATE d
  309. CREATE c
  310. PATH
  311. OPEN c
  312. PATH
  313. CREATE u
  314. CREATE g
  315. CREATE h
  316. PATH
  317. BACK
  318. PATH
  319. PRINT
  320.  
  321. Sample output
  322.  
  323. c:\a\
  324. c:\a\c\
  325. c:\a\c\
  326. c:\a\
  327. c:
  328.  a
  329.   b
  330.   c
  331.    g
  332.    h
  333.    u
  334.   d
  335.  
  336.  
Add Comment
Please, Sign In to add comment