27 Octobre
This commit is contained in:
BIN
DEV3.2/TP4/Arithmetique/MainArithmetique.class
Normal file
BIN
DEV3.2/TP4/Arithmetique/MainArithmetique.class
Normal file
Binary file not shown.
31
DEV3.2/TP4/Arithmetique/MainArithmetique.java
Normal file
31
DEV3.2/TP4/Arithmetique/MainArithmetique.java
Normal file
@@ -0,0 +1,31 @@
|
||||
import java.util.*;
|
||||
|
||||
public class MainArithmetique {
|
||||
|
||||
public static String calculateur(String val, ArrayDeque<String> pile){
|
||||
switch(val){
|
||||
case "+":
|
||||
return String.valueOf(Integer.parseInt(calculateur(pile.pop(), pile))+Integer.parseInt(calculateur(pile.pop(), pile)));
|
||||
case "-":
|
||||
return String.valueOf(-(Integer.parseInt(calculateur(pile.pop(), pile)))+Integer.parseInt(calculateur(pile.pop(), pile)));
|
||||
case "x":
|
||||
return String.valueOf(Integer.parseInt(calculateur(pile.pop(), pile))*Integer.parseInt(calculateur(pile.pop(), pile)));
|
||||
case "/":
|
||||
return String.valueOf(Integer.parseInt(calculateur(pile.pop(), pile))/Integer.parseInt(calculateur(pile.pop(), pile)));
|
||||
default:
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ArrayDeque<String> pile = new ArrayDeque(args.length);
|
||||
String result;
|
||||
|
||||
for (int i=0; i<args.length; i++){
|
||||
pile.push(args[i]);
|
||||
}
|
||||
result = calculateur(pile.pop(), pile);
|
||||
System.out.println("\n = "+result);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user