From b18f49ddd51f84389a10fd15606d58cffe15238e Mon Sep 17 00:00:00 2001 From: horville Date: Thu, 24 Nov 2022 15:17:33 +0100 Subject: [PATCH] TP 07 --- DEV 3.2/TP06/Compteur/Compteur.java | 3 +- DEV 3.2/TP07/Infixe/Infixe.java | 33 ++++++++++++ DEV 3.2/TP07/Infixe/Node.java | 40 ++++++++++++++ DEV 3.2/TP07/Infixe/Tree.java | 19 +++++++ DEV 3.2/TP07/Prefixe/Node.java | 40 ++++++++++++++ DEV 3.2/TP07/Prefixe/Prefixe.java | 34 ++++++++++++ DEV 3.2/TP07/Prefixe/Tree.java | 19 +++++++ DEV 3.2/TP07/Repertoires/Node.java | 41 ++++++++++++++ DEV 3.2/TP07/Repertoires/Repertoires.java | 27 ++++++++++ DEV 3.2/TP07/Repertoires/Tree.java | 19 +++++++ DEV 3.2/TP07/Variables/Node.java | 65 +++++++++++++++++++++++ DEV 3.2/TP07/Variables/Tree.java | 19 +++++++ DEV 3.2/TP07/Variables/Variables.java | 55 +++++++++++++++++++ 13 files changed, 412 insertions(+), 2 deletions(-) create mode 100644 DEV 3.2/TP07/Infixe/Infixe.java create mode 100644 DEV 3.2/TP07/Infixe/Node.java create mode 100644 DEV 3.2/TP07/Infixe/Tree.java create mode 100644 DEV 3.2/TP07/Prefixe/Node.java create mode 100644 DEV 3.2/TP07/Prefixe/Prefixe.java create mode 100644 DEV 3.2/TP07/Prefixe/Tree.java create mode 100644 DEV 3.2/TP07/Repertoires/Node.java create mode 100644 DEV 3.2/TP07/Repertoires/Repertoires.java create mode 100644 DEV 3.2/TP07/Repertoires/Tree.java create mode 100644 DEV 3.2/TP07/Variables/Node.java create mode 100644 DEV 3.2/TP07/Variables/Tree.java create mode 100644 DEV 3.2/TP07/Variables/Variables.java diff --git a/DEV 3.2/TP06/Compteur/Compteur.java b/DEV 3.2/TP06/Compteur/Compteur.java index 78a0b40..2725569 100644 --- a/DEV 3.2/TP06/Compteur/Compteur.java +++ b/DEV 3.2/TP06/Compteur/Compteur.java @@ -13,8 +13,7 @@ public class Compteur { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { String saisie = br.readLine(); - System.out.print(saisie); - if (saisie == null) { + if (saisie.compareTo("") == 0) { break; } diff --git a/DEV 3.2/TP07/Infixe/Infixe.java b/DEV 3.2/TP07/Infixe/Infixe.java new file mode 100644 index 0000000..fcbc4ec --- /dev/null +++ b/DEV 3.2/TP07/Infixe/Infixe.java @@ -0,0 +1,33 @@ +import java.util.ArrayDeque; + +/** + * Arithmetique + */ +public class Infixe { + public static void main(String[] args) { + ArrayDeque> pile = new ArrayDeque<>(); + + for (String arg : args) { + try { + Integer.parseInt(arg); + pile.addFirst(new Node(arg)); + } catch (NumberFormatException e) { + if (pile.size() < 2) { + System.err.println("Invalid stack size."); + return; + } + + Node n2 = pile.pollFirst(); + Node n1 = pile.pollFirst(); + + Node n3 = new Node(arg); + n3.addSubNode(n1); + n3.addSubNode(n2); + + pile.addFirst(n3); + } + } + + System.out.println(pile.pollFirst().showNode()); + } +} \ No newline at end of file diff --git a/DEV 3.2/TP07/Infixe/Node.java b/DEV 3.2/TP07/Infixe/Node.java new file mode 100644 index 0000000..c3b317d --- /dev/null +++ b/DEV 3.2/TP07/Infixe/Node.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; +import java.util.List; + +public class Node { + private T value; + private List> subNodes; + + public Node(T value) { + this.value = value; + this.subNodes = new ArrayList<>(); + } + + public T getValue() { + return value; + } + + public void setValue(T value) { + this.value = value; + } + + public List> getSubNodes() { + return this.subNodes; + } + + public boolean addSubNode(Node node) { + return subNodes.add(node); + } + + public boolean remSubNode(Node node) { + return subNodes.remove(node); + } + + public String showNode() { + if (subNodes.size() < 2) { + return value.toString(); + } else { + return "(" + subNodes.get(0).showNode() + value.toString() + subNodes.get(1).showNode() + ")"; + } + } +} diff --git a/DEV 3.2/TP07/Infixe/Tree.java b/DEV 3.2/TP07/Infixe/Tree.java new file mode 100644 index 0000000..971382c --- /dev/null +++ b/DEV 3.2/TP07/Infixe/Tree.java @@ -0,0 +1,19 @@ +public class Tree { + private Node rootNode; + + public Tree() { + rootNode = new Node(null); + } + + public void setRootNode(Node rootNode) { + this.rootNode = rootNode; + } + + public Node getRootNode() { + return rootNode; + } + + public void showTree() { + rootNode.showNode(); + } +} diff --git a/DEV 3.2/TP07/Prefixe/Node.java b/DEV 3.2/TP07/Prefixe/Node.java new file mode 100644 index 0000000..176f589 --- /dev/null +++ b/DEV 3.2/TP07/Prefixe/Node.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; +import java.util.List; + +public class Node { + private T value; + private List> subNodes; + + public Node(T value) { + this.value = value; + this.subNodes = new ArrayList<>(); + } + + public T getValue() { + return value; + } + + public void setValue(T value) { + this.value = value; + } + + public List> getSubNodes() { + return this.subNodes; + } + + public boolean addSubNode(Node node) { + return subNodes.add(node); + } + + public boolean remSubNode(Node node) { + return subNodes.remove(node); + } + + public void showNode() { + System.out.print(value.toString() + " "); + + for (Node subNode : subNodes) { + subNode.showNode(); + } + } +} diff --git a/DEV 3.2/TP07/Prefixe/Prefixe.java b/DEV 3.2/TP07/Prefixe/Prefixe.java new file mode 100644 index 0000000..662be7f --- /dev/null +++ b/DEV 3.2/TP07/Prefixe/Prefixe.java @@ -0,0 +1,34 @@ +import java.util.ArrayDeque; + +/** + * Arithmetique + */ +public class Prefixe { + public static void main(String[] args) { + ArrayDeque> pile = new ArrayDeque<>(); + + for (String arg : args) { + try { + Integer.parseInt(arg); + pile.addFirst(new Node(arg)); + } catch (NumberFormatException e) { + if (pile.size() < 2) { + System.err.println("Invalid stack size."); + return; + } + + Node n2 = pile.pollFirst(); + Node n1 = pile.pollFirst(); + + Node n3 = new Node(arg); + n3.addSubNode(n1); + n3.addSubNode(n2); + + pile.addFirst(n3); + } + } + + pile.pollFirst().showNode(); + System.out.println(""); + } +} \ No newline at end of file diff --git a/DEV 3.2/TP07/Prefixe/Tree.java b/DEV 3.2/TP07/Prefixe/Tree.java new file mode 100644 index 0000000..971382c --- /dev/null +++ b/DEV 3.2/TP07/Prefixe/Tree.java @@ -0,0 +1,19 @@ +public class Tree { + private Node rootNode; + + public Tree() { + rootNode = new Node(null); + } + + public void setRootNode(Node rootNode) { + this.rootNode = rootNode; + } + + public Node getRootNode() { + return rootNode; + } + + public void showTree() { + rootNode.showNode(); + } +} diff --git a/DEV 3.2/TP07/Repertoires/Node.java b/DEV 3.2/TP07/Repertoires/Node.java new file mode 100644 index 0000000..8332fc5 --- /dev/null +++ b/DEV 3.2/TP07/Repertoires/Node.java @@ -0,0 +1,41 @@ +import java.util.Collection; +import java.util.HashSet; + +public class Node { + private T value; + private Collection> subNodes; + + public Node(T value) { + this.value = value; + this.subNodes = new HashSet<>(); + } + + public T getValue() { + return value; + } + + public void setValue(T value) { + this.value = value; + } + + public Collection> getSubNodes() { + return this.subNodes; + } + + public boolean addSubNode(Node node) { + return subNodes.add(node); + } + + public boolean remSubNode(Node node) { + return subNodes.remove(node); + } + + public void showNode(int level) { + for (int i = 0; i < level; i++) System.out.print("\t"); + System.out.println(value.toString()); + + for (Node subNode : subNodes) { + subNode.showNode(level + 1); + } + } +} diff --git a/DEV 3.2/TP07/Repertoires/Repertoires.java b/DEV 3.2/TP07/Repertoires/Repertoires.java new file mode 100644 index 0000000..cc057b3 --- /dev/null +++ b/DEV 3.2/TP07/Repertoires/Repertoires.java @@ -0,0 +1,27 @@ +import java.io.File; + +/** + * Repertoires + */ +public class Repertoires { + + public static void main(String[] args) { + Tree tree = new Tree<>(); + File f = new File(args.length > 0 ? args[0] : "."); + tree.getRootNode().setValue(f.getName()); + fillNode(tree.getRootNode(), f); + tree.showTree(); + } + + public static void fillNode(Node node, File folder) { + for (File f : folder.listFiles()) { + Node subNode = new Node(f.getName()); + + if (f.isDirectory()) { + fillNode(subNode, f); + } + + node.addSubNode(subNode); + } + } +} \ No newline at end of file diff --git a/DEV 3.2/TP07/Repertoires/Tree.java b/DEV 3.2/TP07/Repertoires/Tree.java new file mode 100644 index 0000000..4b0277c --- /dev/null +++ b/DEV 3.2/TP07/Repertoires/Tree.java @@ -0,0 +1,19 @@ +public class Tree { + private Node rootNode; + + public Tree() { + rootNode = new Node(null); + } + + public void setRootNode(Node rootNode) { + this.rootNode = rootNode; + } + + public Node getRootNode() { + return rootNode; + } + + public void showTree() { + rootNode.showNode(0); + } +} diff --git a/DEV 3.2/TP07/Variables/Node.java b/DEV 3.2/TP07/Variables/Node.java new file mode 100644 index 0000000..d0575f4 --- /dev/null +++ b/DEV 3.2/TP07/Variables/Node.java @@ -0,0 +1,65 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class Node { + private String value; + private List subNodes; + + public Node(String value) { + this.value = value; + this.subNodes = new ArrayList<>(); + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public List getSubNodes() { + return this.subNodes; + } + + public boolean addSubNode(Node node) { + return subNodes.add(node); + } + + public boolean remSubNode(Node node) { + return subNodes.remove(node); + } + + public int getResult(Map ids) { + if (subNodes.size() < 2) { + try { + int n = Integer.parseInt(value); + return n; + } catch (NumberFormatException e) { + return ids.get(value); + } + } else { + switch (value) { + case "+": + return subNodes.get(0).getResult(ids) + subNodes.get(1).getResult(ids); + case "-": + return subNodes.get(0).getResult(ids) - subNodes.get(1).getResult(ids); + case "/": + return subNodes.get(0).getResult(ids) / subNodes.get(1).getResult(ids); + case "x": + return subNodes.get(0).getResult(ids) * subNodes.get(1).getResult(ids); + default: + return 0; + } + } + } + + public String showNode() { + if (subNodes.size() < 2) { + return value.toString(); + } else { + return "(" + subNodes.get(0).showNode() + value.toString() + subNodes.get(1).showNode() + ")"; + } + } +} diff --git a/DEV 3.2/TP07/Variables/Tree.java b/DEV 3.2/TP07/Variables/Tree.java new file mode 100644 index 0000000..48af515 --- /dev/null +++ b/DEV 3.2/TP07/Variables/Tree.java @@ -0,0 +1,19 @@ +public class Tree { + private Node rootNode; + + public Tree() { + rootNode = new Node(null); + } + + public void setRootNode(Node rootNode) { + this.rootNode = rootNode; + } + + public Node getRootNode() { + return rootNode; + } + + public void showTree() { + rootNode.showNode(); + } +} diff --git a/DEV 3.2/TP07/Variables/Variables.java b/DEV 3.2/TP07/Variables/Variables.java new file mode 100644 index 0000000..07e2435 --- /dev/null +++ b/DEV 3.2/TP07/Variables/Variables.java @@ -0,0 +1,55 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.HashMap; +import java.util.Map; + +/** + * Arithmetique + */ +public class Variables { + + public static void main(String[] args) { + ArrayDeque pile = new ArrayDeque<>(); + HashMap ids = new HashMap<>(); + + for (String arg : args) { + try { + Integer.parseInt(arg); + pile.addFirst(new Node(arg)); + } catch (NumberFormatException e) { + if (Character.isUpperCase(arg.charAt(0))) { + pile.addFirst(new Node(arg)); + ids.put(arg, 0); + } else { + if (pile.size() < 2) { + System.err.println("Invalid stack size."); + return; + } + Node n2 = pile.pollFirst(); + Node n1 = pile.pollFirst(); + + Node n3 = new Node(arg); + n3.addSubNode(n1); + n3.addSubNode(n2); + + pile.addFirst(n3); + } + } + } + + Node firstNode = pile.pollFirst(); + System.out.println(firstNode.showNode()); + + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { + for (Map.Entry entry : ids.entrySet()) { + System.out.print(entry.getKey() + " ? "); + ids.put(entry.getKey(), Integer.parseInt(br.readLine())); + } + } catch (Exception e) { + System.out.println(e); + } + + System.out.println("= " + firstNode.getResult(ids)); + } +} \ No newline at end of file