import java.util.*; import java.util.StringTokenizer; public class debut { public static void main (String args []) { String ok=new String("oui"); do { TextWindow.printLine("Donner l'expression a evaluer : ") ; String expression=TextWindow.readString(); StringTokenizer e=new StringTokenizer(expression); evaluation(e); TextWindow.printLine("voulez vous recommencer?"); ok=TextWindow.readString(); } while(ok.equals("oui")); } public static void evaluation (StringTokenizer e) { Pile p = new Pile(); String op = null; String w; String z; int b; int c; int resa; String resb; String ope; while (e.hasMoreTokens()) { String i = e.nextToken(); if (estunnombre(i)) { p.empiler(i); p.affichePile(); } else if (estunoperateur(i)) { if (!(estprioritaire(op,i))) { while (p!=null) { z = p.sommet(); b=Integer.parseInt(z); p.depiler(); ope = p.sommet(); p.depiler(); w = p.sommet(); c=Integer.parseInt(w); p.depiler(); resa=calculer(b,ope,c); resb=Integer.toString(resa); p.empiler(resb); p.affichePile(); } p.empiler(i); op =i ; p.affichePile(); } else if(estprioritaire(op,i)) { p.empiler(i); p.empiler(e.nextToken()); w = p.sommet(); b=Integer.parseInt(w); p.depiler(); ope = p.sommet(); p.depiler(); z = p.sommet(); c=Integer.parseInt(z); p.depiler(); resa=calculer(b,ope,c); resb=Integer.toString(resa); p.empiler(resb); p.affichePile(); while (p!=null) { z = p.sommet(); b=Integer.parseInt(z); p.depiler(); ope = p.sommet(); p.depiler(); w = p.sommet(); c=Integer.parseInt(w); p.depiler(); resa=calculer(b,ope,c); resb=Integer.toString(resa); p.empiler(resb); p.affichePile(); } } } } } public static boolean estsuperieur(String premier , String deuxieme) { if ( (deuxieme.equals("x"))||(deuxieme.equals("/"))) { return true; } else if ((deuxieme.equals("+"))||(deuxieme.equals("-"))) { if ((premier.equals("*"))||(premier.equals("/"))) { return false; } else { return true; } } else { return false ; } } public static boolean estunnombre(String valeur) { for (int i = 0; i < valeur.length(); i++) { char c = valeur.charAt(i); if(c<'0' || c>'9') return false; } return true; } public static boolean estunoperateur(String x) { if (x.equals("+") || x.equals("-") || x.equals("*")|| x.equals("/")) { return true; } else { return false; } } public static int calculer(int x, String a,int y) { if ( a.equals("+")) { return x+y ; } else if ( a.equals("*")) { return x*y ; } else if ( a.equals("-")) { return x-y ; } else if ( a.equals("/")) { return x/y ; } else return -1 ; } public static boolean estprioritaire(String premier , String deuxieme) { if ( (deuxieme.equals("x"))||(deuxieme.equals("/"))) { return true; } else if ((deuxieme.equals("+"))||(deuxieme.equals("-"))) { if ((premier.equals("*"))||(premier.equals("/"))) { return false; } else { return true; } } else { return false ; } } }