Algorithm Sedgewick Fourth Edition-1th Chapter Foundation-201 evaluates the value of an expression by priority

Source: Internet
Author: User
Tags assert

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.