31 lines
1006 B
Java
31 lines
1006 B
Java
|
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);
|
||
|
|
||
|
}
|
||
|
}
|