fin tp piles
This commit is contained in:
BIN
DEV3.2/TP04/02_Chaine/Main.class
Normal file
BIN
DEV3.2/TP04/02_Chaine/Main.class
Normal file
Binary file not shown.
37
DEV3.2/TP04/02_Chaine/Main.java
Normal file
37
DEV3.2/TP04/02_Chaine/Main.java
Normal file
@@ -0,0 +1,37 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
PileChainee<String> pile = new PileChainee<>();
|
||||
|
||||
for (int i = 0; i != args.length; i++) {
|
||||
try {
|
||||
Integer.parseInt(args[i]);
|
||||
pile.addFirst(args[i]);
|
||||
} catch (NumberFormatException e) {
|
||||
int n2 = Integer.parseInt(pile.removeFirst());
|
||||
int n1 = Integer.parseInt(pile.removeFirst());
|
||||
System.out.println(n2);
|
||||
System.out.println(n1);
|
||||
|
||||
switch (args[i]) {
|
||||
case "+":
|
||||
pile.addFirst(n1 + n2 + "");
|
||||
break;
|
||||
case "-":
|
||||
pile.addFirst(n1 - n2 + "");
|
||||
break;
|
||||
case "x":
|
||||
pile.addFirst(n1 * n2 + "");
|
||||
break;
|
||||
case "/":
|
||||
pile.addFirst(n1 / n2 + "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("= " + pile.removeFirst());
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
BIN
DEV3.2/TP04/02_Chaine/Pile.class
Normal file
BIN
DEV3.2/TP04/02_Chaine/Pile.class
Normal file
Binary file not shown.
6
DEV3.2/TP04/02_Chaine/Pile.java
Normal file
6
DEV3.2/TP04/02_Chaine/Pile.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public interface Pile<E> {
|
||||
|
||||
void addFirst(E element);
|
||||
E removeFirst();
|
||||
boolean isEmpty();
|
||||
}
|
||||
BIN
DEV3.2/TP04/02_Chaine/PileChainee.class
Normal file
BIN
DEV3.2/TP04/02_Chaine/PileChainee.class
Normal file
Binary file not shown.
52
DEV3.2/TP04/02_Chaine/PileChainee.java
Normal file
52
DEV3.2/TP04/02_Chaine/PileChainee.java
Normal file
@@ -0,0 +1,52 @@
|
||||
public class PileChainee<E> implements Pile<E> {
|
||||
|
||||
protected E valeur;
|
||||
protected PileChainee<E> suivant;
|
||||
|
||||
public PileChainee() {
|
||||
this.valeur = null;
|
||||
this.suivant = null;
|
||||
}
|
||||
|
||||
public PileChainee(E element) {
|
||||
this.valeur = element;
|
||||
this.suivant = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFirst(E element) {
|
||||
if (this.isEmpty()) {
|
||||
this.valeur = element;
|
||||
} else {
|
||||
PileChainee<E> nouveau = new PileChainee<>();
|
||||
nouveau.valeur = this.valeur;
|
||||
nouveau.suivant = this.suivant;
|
||||
this.valeur = element;
|
||||
this.suivant = nouveau;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E removeFirst() {
|
||||
if (this.isEmpty()) {
|
||||
throw new IllegalStateException("Pile vide");
|
||||
} else {
|
||||
if (this.suivant == null) {
|
||||
E temp = this.valeur;
|
||||
this.valeur = null;
|
||||
return temp;
|
||||
} else {
|
||||
E temp = this.valeur;
|
||||
this.valeur = this.suivant.valeur;
|
||||
this.suivant = this.suivant.suivant;
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return (this.valeur == null);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user