This commit is contained in:
HORVILLE 2022-11-24 15:17:33 +01:00
parent b7f44474fc
commit b18f49ddd5
13 changed files with 412 additions and 2 deletions

View File

@ -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;
}

View File

@ -0,0 +1,33 @@
import java.util.ArrayDeque;
/**
* Arithmetique
*/
public class Infixe {
public static void main(String[] args) {
ArrayDeque<Node<String>> pile = new ArrayDeque<>();
for (String arg : args) {
try {
Integer.parseInt(arg);
pile.addFirst(new Node<String>(arg));
} catch (NumberFormatException e) {
if (pile.size() < 2) {
System.err.println("Invalid stack size.");
return;
}
Node<String> n2 = pile.pollFirst();
Node<String> n1 = pile.pollFirst();
Node<String> n3 = new Node<String>(arg);
n3.addSubNode(n1);
n3.addSubNode(n2);
pile.addFirst(n3);
}
}
System.out.println(pile.pollFirst().showNode());
}
}

View File

@ -0,0 +1,40 @@
import java.util.ArrayList;
import java.util.List;
public class Node<T> {
private T value;
private List<Node<T>> 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<Node<T>> getSubNodes() {
return this.subNodes;
}
public boolean addSubNode(Node<T> node) {
return subNodes.add(node);
}
public boolean remSubNode(Node<T> 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() + ")";
}
}
}

View File

@ -0,0 +1,19 @@
public class Tree<T> {
private Node<T> rootNode;
public Tree() {
rootNode = new Node<T>(null);
}
public void setRootNode(Node<T> rootNode) {
this.rootNode = rootNode;
}
public Node<T> getRootNode() {
return rootNode;
}
public void showTree() {
rootNode.showNode();
}
}

View File

@ -0,0 +1,40 @@
import java.util.ArrayList;
import java.util.List;
public class Node<T> {
private T value;
private List<Node<T>> 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<Node<T>> getSubNodes() {
return this.subNodes;
}
public boolean addSubNode(Node<T> node) {
return subNodes.add(node);
}
public boolean remSubNode(Node<T> node) {
return subNodes.remove(node);
}
public void showNode() {
System.out.print(value.toString() + " ");
for (Node<T> subNode : subNodes) {
subNode.showNode();
}
}
}

View File

@ -0,0 +1,34 @@
import java.util.ArrayDeque;
/**
* Arithmetique
*/
public class Prefixe {
public static void main(String[] args) {
ArrayDeque<Node<String>> pile = new ArrayDeque<>();
for (String arg : args) {
try {
Integer.parseInt(arg);
pile.addFirst(new Node<String>(arg));
} catch (NumberFormatException e) {
if (pile.size() < 2) {
System.err.println("Invalid stack size.");
return;
}
Node<String> n2 = pile.pollFirst();
Node<String> n1 = pile.pollFirst();
Node<String> n3 = new Node<String>(arg);
n3.addSubNode(n1);
n3.addSubNode(n2);
pile.addFirst(n3);
}
}
pile.pollFirst().showNode();
System.out.println("");
}
}

View File

@ -0,0 +1,19 @@
public class Tree<T> {
private Node<T> rootNode;
public Tree() {
rootNode = new Node<T>(null);
}
public void setRootNode(Node<T> rootNode) {
this.rootNode = rootNode;
}
public Node<T> getRootNode() {
return rootNode;
}
public void showTree() {
rootNode.showNode();
}
}

View File

@ -0,0 +1,41 @@
import java.util.Collection;
import java.util.HashSet;
public class Node<T> {
private T value;
private Collection<Node<T>> 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<Node<T>> getSubNodes() {
return this.subNodes;
}
public boolean addSubNode(Node<T> node) {
return subNodes.add(node);
}
public boolean remSubNode(Node<T> 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<T> subNode : subNodes) {
subNode.showNode(level + 1);
}
}
}

View File

@ -0,0 +1,27 @@
import java.io.File;
/**
* Repertoires
*/
public class Repertoires {
public static void main(String[] args) {
Tree<String> 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<String> node, File folder) {
for (File f : folder.listFiles()) {
Node<String> subNode = new Node<String>(f.getName());
if (f.isDirectory()) {
fillNode(subNode, f);
}
node.addSubNode(subNode);
}
}
}

View File

@ -0,0 +1,19 @@
public class Tree<T> {
private Node<T> rootNode;
public Tree() {
rootNode = new Node<T>(null);
}
public void setRootNode(Node<T> rootNode) {
this.rootNode = rootNode;
}
public Node<T> getRootNode() {
return rootNode;
}
public void showTree() {
rootNode.showNode(0);
}
}

View File

@ -0,0 +1,65 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Node {
private String value;
private List<Node> 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<Node> 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<String, Integer> 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() + ")";
}
}
}

View File

@ -0,0 +1,19 @@
public class Tree<T> {
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();
}
}

View File

@ -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<Node> pile = new ArrayDeque<>();
HashMap<String, Integer> 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<String, Integer> 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));
}
}