fin arbre
This commit is contained in:
BIN
DEV3.2/arbre/infixe/Infixe.class
Normal file
BIN
DEV3.2/arbre/infixe/Infixe.class
Normal file
Binary file not shown.
45
DEV3.2/arbre/infixe/Infixe.java
Normal file
45
DEV3.2/arbre/infixe/Infixe.java
Normal file
@@ -0,0 +1,45 @@
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
public class Infixe {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0) {
|
||||
System.out.println("Veuillez fournir une expression en notation polonaise inversée.");
|
||||
return;
|
||||
}
|
||||
|
||||
Deque<Node> stack = new ArrayDeque<>();
|
||||
|
||||
try {
|
||||
for (String token : args) {
|
||||
switch (token) {
|
||||
case "+":
|
||||
case "-":
|
||||
case "x":
|
||||
case "/":
|
||||
// Création d'un nœud opérateur
|
||||
Node right = stack.pop();
|
||||
Node left = stack.pop();
|
||||
stack.push(new OperatorNode(token.equals("x") ? "*" : token, left, right));
|
||||
break;
|
||||
default:
|
||||
// Création d'un nœud opérande
|
||||
stack.push(new OperandNode(Integer.parseInt(token)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Le résultat final doit être le seul élément restant sur la pile
|
||||
if (stack.size() == 1) {
|
||||
Node result = stack.pop();
|
||||
System.out.println("= " + result.toInfix());
|
||||
} else {
|
||||
System.out.println("Erreur : Expression incorrecte.");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Erreur lors de l'évaluation de l'expression : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
DEV3.2/arbre/infixe/Node.class
Normal file
BIN
DEV3.2/arbre/infixe/Node.class
Normal file
Binary file not shown.
4
DEV3.2/arbre/infixe/Node.java
Normal file
4
DEV3.2/arbre/infixe/Node.java
Normal file
@@ -0,0 +1,4 @@
|
||||
// Classe représentant un nœud de l'arbre de syntaxe
|
||||
public abstract class Node {
|
||||
public abstract String toInfix(); // Convertir en notation préfixe
|
||||
}
|
||||
BIN
DEV3.2/arbre/infixe/OperandNode.class
Normal file
BIN
DEV3.2/arbre/infixe/OperandNode.class
Normal file
Binary file not shown.
12
DEV3.2/arbre/infixe/OperandNode.java
Normal file
12
DEV3.2/arbre/infixe/OperandNode.java
Normal file
@@ -0,0 +1,12 @@
|
||||
public class OperandNode extends Node {
|
||||
int value;
|
||||
|
||||
OperandNode(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toInfix() {
|
||||
return Integer.toString(value);
|
||||
}
|
||||
}
|
||||
BIN
DEV3.2/arbre/infixe/OperatorNode.class
Normal file
BIN
DEV3.2/arbre/infixe/OperatorNode.class
Normal file
Binary file not shown.
20
DEV3.2/arbre/infixe/OperatorNode.java
Normal file
20
DEV3.2/arbre/infixe/OperatorNode.java
Normal file
@@ -0,0 +1,20 @@
|
||||
// Nœud opérateur
|
||||
public class OperatorNode extends Node {
|
||||
String operator;
|
||||
Node left, right;
|
||||
|
||||
OperatorNode(String operator, Node left, Node right) {
|
||||
this.operator = operator;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toInfix() {
|
||||
// On ajoute systématiquement des parenthèses autour de chaque opération
|
||||
if (operator == "*"){
|
||||
return "(" + left.toInfix() + " x " + right.toInfix() + ")";
|
||||
}
|
||||
return "(" + left.toInfix() + " " + operator + " " + right.toInfix() + ")";
|
||||
}
|
||||
}
|
||||
BIN
DEV3.2/arbre/prefixe/Node.class
Normal file
BIN
DEV3.2/arbre/prefixe/Node.class
Normal file
Binary file not shown.
4
DEV3.2/arbre/prefixe/Node.java
Normal file
4
DEV3.2/arbre/prefixe/Node.java
Normal file
@@ -0,0 +1,4 @@
|
||||
// Classe représentant un nœud de l'arbre de syntaxe
|
||||
public abstract class Node {
|
||||
public abstract String toPrefix(); // Convertir en notation préfixe
|
||||
}
|
||||
BIN
DEV3.2/arbre/prefixe/OperatorNode.class
Normal file
BIN
DEV3.2/arbre/prefixe/OperatorNode.class
Normal file
Binary file not shown.
16
DEV3.2/arbre/prefixe/OperatorNode.java
Normal file
16
DEV3.2/arbre/prefixe/OperatorNode.java
Normal file
@@ -0,0 +1,16 @@
|
||||
// Nœud opérateur
|
||||
public class OperatorNode extends Node {
|
||||
String operator;
|
||||
Node left, right;
|
||||
|
||||
OperatorNode(String operator, Node left, Node right) {
|
||||
this.operator = operator;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toPrefix() {
|
||||
return operator + " " + left.toPrefix() + " " + right.toPrefix();
|
||||
}
|
||||
}
|
||||
BIN
DEV3.2/arbre/prefixe/Prefixe.class
Normal file
BIN
DEV3.2/arbre/prefixe/Prefixe.class
Normal file
Binary file not shown.
45
DEV3.2/arbre/prefixe/Prefixe.java
Normal file
45
DEV3.2/arbre/prefixe/Prefixe.java
Normal file
@@ -0,0 +1,45 @@
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
public class Prefixe {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0) {
|
||||
System.out.println("Veuillez fournir une expression en notation polonaise inversée.");
|
||||
return;
|
||||
}
|
||||
|
||||
Deque<Node> stack = new ArrayDeque<>();
|
||||
|
||||
try {
|
||||
for (String token : args) {
|
||||
switch (token) {
|
||||
case "+":
|
||||
case "-":
|
||||
case "x":
|
||||
case "/":
|
||||
// Opérateur : créer un nœud opérateur
|
||||
Node right = stack.pop();
|
||||
Node left = stack.pop();
|
||||
stack.push(new OperatorNode(token, left, right));
|
||||
break;
|
||||
default:
|
||||
// Opérande : créer un nœud valeur
|
||||
stack.push(new ValueNode(Integer.parseInt(token)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// L'AST final doit être le seul élément restant sur la pile
|
||||
if (stack.size() == 1) {
|
||||
Node root = stack.pop();
|
||||
System.out.println("= " + root.toPrefix());
|
||||
} else {
|
||||
System.out.println("Erreur : Expression incorrecte.");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Erreur lors de l'évaluation de l'expression : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
DEV3.2/arbre/prefixe/ValueNode.class
Normal file
BIN
DEV3.2/arbre/prefixe/ValueNode.class
Normal file
Binary file not shown.
13
DEV3.2/arbre/prefixe/ValueNode.java
Normal file
13
DEV3.2/arbre/prefixe/ValueNode.java
Normal file
@@ -0,0 +1,13 @@
|
||||
// Nœud feuille (valeur)
|
||||
public class ValueNode extends Node {
|
||||
int value;
|
||||
|
||||
ValueNode(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toPrefix() {
|
||||
return Integer.toString(value);
|
||||
}
|
||||
}
|
||||
BIN
DEV3.2/arbre/repertoire/Noeud.class
Normal file
BIN
DEV3.2/arbre/repertoire/Noeud.class
Normal file
Binary file not shown.
42
DEV3.2/arbre/repertoire/Noeud.java
Normal file
42
DEV3.2/arbre/repertoire/Noeud.java
Normal file
@@ -0,0 +1,42 @@
|
||||
import java.io.File;
|
||||
|
||||
// Classe représentant un nœud de l'arbre
|
||||
public class Noeud {
|
||||
String nom;
|
||||
Noeud[] enfants;
|
||||
|
||||
public Noeud(String nom) {
|
||||
this.nom = nom;
|
||||
this.enfants = new Noeud[0]; // Initialement pas d'enfants
|
||||
}
|
||||
|
||||
public void setEnfants(Noeud[] enfants) {
|
||||
this.enfants = enfants;
|
||||
}
|
||||
|
||||
public void afficher(String prefixe) {
|
||||
System.out.println(prefixe + nom);
|
||||
for (Noeud enfant : enfants) {
|
||||
enfant.afficher(prefixe + " ");
|
||||
}
|
||||
}
|
||||
|
||||
// Méthode pour construire un arbre à partir d'un répertoire
|
||||
public static Noeud construireArbre(File fichier) {
|
||||
Noeud noeud = new Noeud(fichier.getName());
|
||||
if (fichier.isDirectory()) {
|
||||
File[] contenu = fichier.listFiles();
|
||||
if (contenu != null) {
|
||||
Noeud[] enfants = new Noeud[contenu.length];
|
||||
for (int i = 0; i < contenu.length; i++) {
|
||||
System.out.print(fichier.getName()+":");
|
||||
System.out.print(contenu[i]);
|
||||
System.out.println("");
|
||||
enfants[i] = construireArbre(contenu[i]);
|
||||
}
|
||||
noeud.setEnfants(enfants);
|
||||
}
|
||||
}
|
||||
return noeud;
|
||||
}
|
||||
}
|
||||
BIN
DEV3.2/arbre/repertoire/Repertoires.class
Normal file
BIN
DEV3.2/arbre/repertoire/Repertoires.class
Normal file
Binary file not shown.
27
DEV3.2/arbre/repertoire/Repertoires.java
Normal file
27
DEV3.2/arbre/repertoire/Repertoires.java
Normal file
@@ -0,0 +1,27 @@
|
||||
import java.io.File;
|
||||
|
||||
public class Repertoires {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length != 1) {
|
||||
System.out.println("Usage: java Repertoires <nom_du_répertoire>");
|
||||
return;
|
||||
}
|
||||
|
||||
File racine = new File(args[0]);
|
||||
System.out.println("Chemin fourni : " + racine.getAbsolutePath()); // Débogage
|
||||
|
||||
if (!racine.exists()) {
|
||||
System.out.println("Le répertoire spécifié n'existe pas : " + racine.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!racine.isDirectory()) {
|
||||
System.out.println("Le chemin spécifié n'est pas un répertoire : " + racine.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
|
||||
Noeud arbre = Noeud.construireArbre(racine);
|
||||
arbre.afficher("");
|
||||
}
|
||||
}
|
||||
0
DEV3.2/arbre/repertoire/toto/fichier4
Normal file
0
DEV3.2/arbre/repertoire/toto/fichier4
Normal file
0
DEV3.2/arbre/repertoire/toto/sous1/fichier1
Normal file
0
DEV3.2/arbre/repertoire/toto/sous1/fichier1
Normal file
0
DEV3.2/arbre/repertoire/toto/sous1/fichier2
Normal file
0
DEV3.2/arbre/repertoire/toto/sous1/fichier2
Normal file
0
DEV3.2/arbre/repertoire/toto/sous2/fichier3
Normal file
0
DEV3.2/arbre/repertoire/toto/sous2/fichier3
Normal file
Reference in New Issue
Block a user