44 lines
1.2 KiB
Java
44 lines
1.2 KiB
Java
public class Arithmetique {
|
|
public static void main(String[] args) {
|
|
ArrayPile<Integer> pile = new ArrayPile<>();
|
|
|
|
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());
|
|
}
|
|
} |