import java.util.Stack; public class Arithmetik { public static void main(String[] args) { // Exemple : 18 6 1 2 + 9 + / + 8 7 - * // String[] args = {"18", "6", "1", "2", "+", "9", "+", "/", "+", "8", "7", "-", "*"}; Pile pile = new Pile<>(); for (String element : args) { if (estNombre(element)) { // Si c’est un nombre, on le met dans la pile pile.push(Integer.parseInt(element)); } else { // Sinon c’est un opérateur on dépile 2 valeurs int b = pile.pop(); int a = pile.pop(); int resultat = calculer(a, b, element); // On empile le résultat pile.push(resultat); } } // À la fin, il reste le résultat final dans la pile System.out.println("Résultat = " + pile.pop()); } // Vérifie si une chaîne est un nombre public static boolean estNombre(String s) { try { Integer.parseInt(s); return true; } catch (NumberFormatException e) { return false; } } // Calcule a (op) b public static int calculer(int a, int b, String op) { switch (op) { case "+": return a + b; case "-": return a - b; case "*": return a * b; case "/": return a / b; default: throw new IllegalArgumentException("Opérateur inconnu : " + op); } } }