1+ /* Dijkstra’s Two-Stack Algorithm for Expression Evaluation
2+ * Adapted from Algorithms, 4th ed. implementation (http://algs4.cs.princeton.edu/13stacks/Evaluate.java.html)
3+ * Evaluates basic mathematic string expressions
4+ * Author: Kendrick Ledet 2016 * kennyvledet@gmail.com
5+ */
6+ import java .util .Stack ;
7+
8+ public class ExpressionEvaluation {
9+ public static double evaluate (String expression ) {
10+ // expression needs spaces between parentheses, operators and operands
11+ // Each operation, for every 2 operands, must be enclosed in parenthesis
12+ // Example: ( ( 10 * 10 ) * ( 10 * 10 ) ) instead of ( 10 * 10 * 10 * 10 )
13+
14+ Stack <String > operators = new Stack <String >();
15+ Stack <Double > operands = new Stack <Double >();
16+
17+ for (String token : expression .split (" " )) {
18+ switch (token ) {
19+ case "(" :
20+ continue ;
21+ case "+" :
22+ case "-" :
23+ case "*" :
24+ case "/" :
25+ case "sqrt" :
26+ operators .push (token ); break ;
27+ case ")" : // Pop operand, apply operator and push result
28+ String operator = operators .pop ();
29+ double value = operands .pop ();
30+
31+ switch (operator ) {
32+ case "+" : value = operands .pop () + value ; break ;
33+ case "-" : value = operands .pop () - value ; break ;
34+ case "*" : value = operands .pop () * value ; break ;
35+ case "/" : value = operands .pop () / value ; break ;
36+ case "sqrt" : value = Math .sqrt (value ); break ;
37+ }
38+ operands .push (value );
39+ break ;
40+ default :
41+ operands .push (Double .parseDouble (token ));
42+ }
43+ }
44+ return operands .pop ();
45+ }
46+
47+ public static void main (String [] args ) {
48+ // Test client
49+
50+ // Should pass
51+ for (double x = 0 ; x < 100 ; x ++) {
52+ for (double y = 0 ; y < 100 ; y ++) {
53+ String expression = "( ( " +x +" * " +x +" ) * ( " +y +" * " +y +" ) )" ;
54+ if ( ( evaluate (expression ) != ( x * x * ( y * y ) ) ) )
55+ throw new AssertionError ();
56+ }
57+ }
58+
59+ String testExpression = "( ( 10 * 10 ) * ( 10 * 10 ) )" ;
60+ System .out .println (evaluate (testExpression )); // == 10000.0
61+
62+ // Should pass
63+ if ( evaluate (testExpression ) != 10000.0 )
64+ throw new AssertionError ();
65+ }
66+ }
0 commit comments