fin tp piles

This commit is contained in:
Simoes Lukas
2025-11-03 11:08:37 +01:00
parent 63a15fc6c7
commit 9c4ca5bcda
14 changed files with 211 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,37 @@
import java.awt.*;
public class Main {
public static void main(String[] args) {
PileTableau<String> pile = new PileTableau<>();
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());
}
}

Binary file not shown.

View File

@@ -0,0 +1,6 @@
public interface Pile<E> {
void addFirst(E element);
E removeFirst();
boolean isEmpty();
}

Binary file not shown.

View File

@@ -0,0 +1,35 @@
public class PileTableau<E> implements Pile<E> {
protected E[] elements;
public PileTableau() {
this.elements = (E[]) new Object[16];
}
@Override
public void addFirst(E element) {
E[] nouveau = (E[]) new Object[16];
nouveau[0] = element;
for (int i = 0; i != this.elements.length-1; i++) {
nouveau[i+1] = this.elements[i];
}
this.elements = nouveau;
}
@Override
public E removeFirst() {
E[] nouveau = (E[]) new Object[16];
E temp = this.elements[0];
for (int i = 1; i != this.elements.length; i++) {
nouveau[i-1] = this.elements[i];
}
this.elements = nouveau;
return temp;
}
@Override
public boolean isEmpty() {
return (this.elements[0] == null);
}
}