HeatPulse

Lab7-1 Windows explorer

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