TP04
This commit is contained in:
44
DEV 3.2/TP04/Chaine/Arithmetique.java
Normal file
44
DEV 3.2/TP04/Chaine/Arithmetique.java
Normal file
@@ -0,0 +1,44 @@
|
||||
public class Arithmetique {
|
||||
public static void main(String[] args) {
|
||||
ChainePile<Integer> pile = new ChainePile<>();
|
||||
|
||||
for (String arg : args) {
|
||||
try {
|
||||
int n = Integer.parseInt(arg);
|
||||
pile.addFirst(n);
|
||||
} catch (NumberFormatException e) {
|
||||
if (pile.size() < 2) {
|
||||
System.err.println("Invalid stack size.");
|
||||
return;
|
||||
}
|
||||
|
||||
int n2 = pile.pollFirst();
|
||||
int n1 = pile.pollFirst();
|
||||
|
||||
switch (arg) {
|
||||
case "+":
|
||||
pile.addFirst(n1 + n2);
|
||||
break;
|
||||
|
||||
case "-":
|
||||
pile.addFirst(n1 - n2);
|
||||
break;
|
||||
|
||||
case "/":
|
||||
pile.addFirst(n1 / n2);
|
||||
break;
|
||||
|
||||
case "x":
|
||||
pile.addFirst(n1 * n2);
|
||||
break;
|
||||
|
||||
default:
|
||||
System.err.println("Invalid operator");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(pile.pollFirst());
|
||||
}
|
||||
}
|
||||
25
DEV 3.2/TP04/Chaine/Chaine.java
Normal file
25
DEV 3.2/TP04/Chaine/Chaine.java
Normal file
@@ -0,0 +1,25 @@
|
||||
public class Chaine<E> {
|
||||
|
||||
public Chaine() {
|
||||
|
||||
}
|
||||
|
||||
private E value;
|
||||
private Chaine<E> next;
|
||||
|
||||
public E getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(E value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Chaine<E> getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(Chaine<E> next) {
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
47
DEV 3.2/TP04/Chaine/ChainePile.java
Normal file
47
DEV 3.2/TP04/Chaine/ChainePile.java
Normal file
@@ -0,0 +1,47 @@
|
||||
public class ChainePile<E> implements Pile<E> {
|
||||
|
||||
private Chaine<E> lastChain;
|
||||
|
||||
public ChainePile() {}
|
||||
|
||||
@Override
|
||||
public E getFirst() {
|
||||
if (lastChain != null) {
|
||||
return lastChain.getValue();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pollFirst() {
|
||||
if (lastChain != null) {
|
||||
E value = lastChain.getValue();
|
||||
lastChain = lastChain.getNext();
|
||||
return value;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFirst(E e) {
|
||||
Chaine<E> link = new Chaine<E>();
|
||||
link.setValue(e);
|
||||
link.setNext(lastChain);
|
||||
lastChain = link;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
int count = 0;
|
||||
Chaine<E> curr = lastChain;
|
||||
while (curr != null) {
|
||||
curr = curr.getNext();
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
6
DEV 3.2/TP04/Chaine/Pile.java
Normal file
6
DEV 3.2/TP04/Chaine/Pile.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public interface Pile<E> {
|
||||
public E getFirst();
|
||||
public E pollFirst();
|
||||
public void addFirst(E e);
|
||||
public int size();
|
||||
}
|
||||
Reference in New Issue
Block a user