## Infix Expression EvaluationBeta 4.5, May 23, 2008
The ExprEvaluator class is intended to be easy to use class. InfixEvalDemo just demonstrates some of its capabilities. It could be used in other Java programs whenever it is desirable to allow the user to input a prefix, infix or postfix expression that will be evaluated by the program. Three modes of operation are demonstrated this demo. When " When " When " The demo requires a Java 1.5 or higher add-on to be installed in your browser. If needed, you can down load a free copy from http://www.java.com/en/ |

In the *"Use variables t, x, y without '='"* mode, the demo uses 4 instances of ExprEvaluator - one
or each of t, x, y, and infix. This allows several interesting uses. Here are some examples:

t | x | y | Infix | Comment |
---|---|---|---|---|

sqrt(3^2 + 4^2) | Evaluates the hypotenuse of a right triangle with sides 3 and 4. | |||

3 | 4 | sqrt(x^2 + y^2) | Evaluates the hypotenuse of a right triangle with sides x and y. | |

3 | (4/3)*x | sqrt(x^2 + y^2) | Evaluates the hypotenuse of a right triangle with sides x and (4/3)x. | |

0 | (x-4)/(x-5) | Evaluates (x-4)/(x-5) when x = 0. | ||

x+.1 | (x-4)/(x-5) | Evaluates (x-4)/(x-5) with x increasing by .1 each time the expressions are evaluated. | ||

30 | cos(toRadians(t)) | sin(toRadians(t)) | Calculates the x and y coordinates of the point on the unit circle at the angle of 30 degrees. | |

t + 10 | cos(toRadians(t)) | sin(toRadians(t)) | sin(2*toRadians(t))^2 | Calculates the x, y, and z coordinates of the point above the unit circle with angle t increasing by 10 degrees
each time the expressions are evaluated. The z value is [sin(2t)]^{2} |

In the *"Use variables a, b, ... z with '='"* mode, the demo uses only 1 instances of ExprEvaluator .
Here are a few examples:

1st | 2nd | 3rd | Infix | Comment |
---|---|---|---|---|

a = 3 | b = 4 | sqrt(a^2 + b^2) | Evaluates the hypotenuse of a right triangle with sides a and b. | |

a = 3 | b = 4 | c = 2 | Evaluate a polynomial with 3 constant parameters. This is a two part example. First assign values to a, b, and c. Then follow up with assigning values to x and y. Can be repeated with new values of x and y. | |

x = 3 | y = 4 | a*x^2 + b*x*y + c*y^2 |

In the case of illegal calculations such as 1/0, log(-1), or sqrt(-1), the result will typically be infinity, -infinity, or NaN (Not a Number).

ExprEvaluator originally was developed for a Data Structures class that demonstrated stacks and binary trees. It only operated with numbers and the binary operators +, -, *, /. It could build binary expression trees from both infix and postfix expressions. This version expands it bty allowing the ^ operator, functions, unary operators + and -, variables a, b, c, ..., z, as well as constants PI and E.

ExprEvaluator can be used in nearly any program where it is desirable to allow the user to input an prefix, infix or postfix expression that needs to be evaluated. The class was designed to be very easy to use as illustrated by TrivialExample.java. Suppose that the infix expression is contained in the String expr and the values of t, x, and y are stored in tVal, xVal, and yVal. All that is needed to evaluate the expression into a variable exprVal are these lines of code:

ExprEvaluator evaluator = new ExprEvaluator(); ... try { evaluator.buildFromInfix(infix); exprVal = evaluator.evaluate(tVal, xVal, yVal); }catch (Exception e) { System.out.println ("Error: namely: " + e); }

When it is desired to evaluate several different functions the following code style could be used:

ExprEvaluator evaluator = new ExprEvaluator(); ExprTree [ ] tree = new ExprTree[3]; String [ ] expr = new String[3]; double x; double [ ] val = new double[3]; expr[0] = ... expr[1] = ... expr[2] = ... ... try { for (int i = 0; i < 3; i++) tree[i] = evaluator.buildFromInfix(expr[i]); for (x = ...; x < ...; ...) setX(x); for (i = 0; i < 3; i++) val[i] = evaluator.evaluate(tree[i]); ... } }catch (Exception e) { System.out.println ("Error: namely: " + e); }

If the expression is prefix or postfix instead of infix, the only change would be to be to replace "buildFromInfix" by "buildFromPrefix" or "buildFromPostfix".

For additional information, please look at the Javadoc files for ExprEvaluator.

Down load the beta copy as a .zip file. Included files:

- ExprEvaluator.java - Contains methods for building binary trees from infix and postfix expressions as well as evaluating the resulting trees.
- InfixEvalDemo.java - The demo applet on this page.
- ExprEvaluatorTest.java - A much simpler and primitive application that demos building trees from either infix or postfix. (Note: Examples of the postfix for functions: The postfix for "f3(t, x, y)" is "t x y f3" and for abs(x + y), it is "x y + abs".)
- TrivialExample.java - A simple application showing how easy it is to use ExprEvaluator.
- TrivialExample2.java - A simple application showing how easy it is to use ExprEvaluator with all 26 variables.
- TrivialExample3.java - A simple application showing how easy it is to use ExprEvaluator with a couple of variable other than t, x, or y.
- TrivialExample4.java - A simple application showing how to define the user function.
- TableExample1.java - Prints a table of values for a single function of x
- TableExample2.java - Prints a table of values for two functions of x
- TableExample3.java - Prints a table of values for a parametric functions of t
- TableExample4.java - Prints a table of values for polar function of theta and r along with the corresponding rectangular coordinates x and y.
- EasyFormat.java - Provides static formatting and rounding methods.
- DoubleScanner.java - The scanner used by ExprEvaluator. (Requires Java 5.0 or higher.)
- BinaryNode.java - The binary nodes used by ExprEvaluator. Revised for Beta 3.0
- ArrayPureStack.java - The stack used by ExprEvaluator.
- PureStack.java - Abstract class implemented by ArrayPureStack.
- ExprTree.java - A specialized BinaryNode used as a root of an expression tree..
- ExprEvaluator.class
- InfixEvalDemo.class
- ExprEvaluatorTest.class
- TrivialExample.class
- TrivialExample2.class
- TrivialExample3.class
- TrivialExample4.class
- TableExample1.class
- TableExample2.class
- TableExample3.class
- TableExample4.class
- EasyFormat.class
- DoubleScanner.class
- BinaryNode.class
- ArrayPureStack.class
- PureStack.class
- ExprTree.class
- ExprEvaluator.jar: An archive jar file that contains all of the above class files. Very useful for
speeding loading of applications and reducing the clutter directories using the ExprEvaluator package. For example, if
Example.java and ExprEvaluator.jar are the only files in the default folder, you can use
javac -classpath .;ExprEvaluator.jar Example.java java -classpath .;ExprEvaluator.jar Example

to compile and execute the application Example using the ExprEvaluator package even though Example.class is not in the ExprEvaluator.jar file. The jar file is speeds loading of applets in web pages. For example, this page contains the tags:<applet code = "InfixEvalDemo.class" archive = "ExprEvaluator.jar" width= "530" height= "630"> </applet>

- ExprEvaluator.bat - It compiles ExprEvaluator.java and then creates the ExprEvaluator.jar file
which contains
**all**the class files in the folder. - InfixEvalDemo.HTML - This file. It can be used to run InfixEvalDemo in a browser or the AppletViewer.
- ExprEvaluatorAlgo.doc - The algorithms used by the BuildFromInfix and comparePrecedence methods.
- ExprEvaluatorTest.proj - A file used by the author while developing this package.
- ExprEvaluatorTest.init - Another file used by the author while developing this package.
- documentation - A folder containing JavaDoc documentation for this package.

The class requires Java 1.5 or higher

Please send any comments, corrections, questions, or suggestions to James Brink brinkje@plu.edu.

Copyright James Brink, 2008. Permission to use the package for non-commercial use is hereby granted.

*Revised 6/21/2008*