1 /******************************************************************************2 * Compilation:javac Evaluatedeluxe.java3 * Execution:java Evaluatedeluxe4 * Dependencies:Stack.java5 *6 * evaluates arithmetic expressions using Dijkstra ' s two-stack algorithm.7 * Handles The following binary operators: +,-, *,/and parentheses.8 *9 *% echo "3 + 5 * 6-7 * (8 + 5)" | java evaluatedeluxeTen * -58.0 One * A * - * Limitiations - * -------------- the *-can easily add additional operators and precedence orders, but they - * Must be left associative (exponentiation are right associative) - *-assumes whitespace between operators (including parentheses) - * + * Remarks - * -------------- + *-can eliminate second phase if we enclose input expression A * in parentheses (and, then, could also remove the test at * For whether the operator stack was empty in the inner while loop) - *- Seehttp://introcs.cs.princeton.edu/java/11precedence/ for - * operator Precedence in Java - * - ******************************************************************************/ - in ImportJava.util.TreeMap; - to Public classEvaluatedeluxe { + - //result of applying binary operator op to operands val1 and Val2 the Public Static DoubleEval (String op,DoubleVal1,Doubleval2) { * if(Op.equals ("+"))returnVal1 +Val2; $ if(Op.equals ("-"))returnVal1-Val2;Panax Notoginseng if(Op.equals ("/"))returnVAL1/Val2; - if(Op.equals ("*"))returnVAL1 *Val2; the Throw NewRuntimeException ("Invalid operator"); + } A the Public Static voidMain (string[] args) { + - //precedence order of operators $Treemap<string, integer> precedence =NewTreemap<string, integer>(); $Precedence.put ("(", 0);//For convenience with algorithm -Precedence.put (")", 0); -Precedence.put ("+", 1);//+ and-have Lower precedence than * and/ thePrecedence.put ("-", 1); -Precedence.put ("*", 2);WuyiPrecedence.put ("/", 2); the -stack<string> Ops =NewStack<string>(); WuStack<double> Vals =NewStack<double>(); - About while(!Stdin.isempty ()) { $ - //read in next token (operator or value) -String s =stdin.readstring (); - A //token is a value + if(!Precedence.containskey (s)) { the Vals.push (double.parsedouble (s)); - Continue; $ } the the //Token is an operator the while(true) { the - //The last condition ensures, the operator with higher precedence is evaluated first in if(Ops.isempty () | | s.equals ("(") | | (Precedence.get (s) >Precedence.get (Ops.peek ()))) { the Ops.push (s); the Break; About } the the //Evaluate Expression theString op =Ops.pop (); + - //But ignore left parentheses the if(Op.equals ("(")) {Bayi assertS.equals (")"); the Break; the } - - //evaluate operator and operands and push result onto value stack the Else { the DoubleVal2 =Vals.pop (); the DoubleVal1 =Vals.pop (); the Vals.push (eval (OP, Val1, Val2)); - } the } the } the 94 //finished parsing string-evaluate operator and operands remaining on both stacks the while(!Ops.isempty ()) { theString op =Ops.pop (); the DoubleVal2 =Vals.pop ();98 DoubleVal1 =Vals.pop (); About Vals.push (eval (OP, Val1, Val2)); - }101 102 //Last value in stack is value of expression103 stdout.println (Vals.pop ());104 assertvals.isempty (); the assertops.isempty ();106 }107}
Algorithm Sedgewick Fourth Edition-1th Chapter Foundation-201 evaluates the value of an expression by priority