|
www.brianziman.com
Linux – Java – Web – Questions Answered, Problems Solved
| |
|
Brian Ziman about contact terms of use privacy policy activity site map |
Infix Calculator/Expression Evaluator JavaScript ExampleWelcome to the Infix Calculator and Expression Evaluator JavaScript example. While the algorithms for an infix expression evaluator are not difficult, it seemed, in a fit of boredom, an interesting challenge to implement these algorithms in JavaScript and the user interface in HTML. Please note that this was written in 2002, and while it has been updated to conform to XHTML standards, and tested in Firefox 2, a modern implementation would use an object oriented that JavaScript now supports. This code is still good for illustrative purposes. For instructions and more details, please see below.Instructions and Implementation NotesThis page contains an implementation of a simple interactive stack based infix expression evaluator that is aware of parenthesis and basic order of operations. It understands unary +/-. The calculator allows you to enter a complete expression to be evaluated and caches all submitted expressions. In its current form, the calculator does not perform syntax checking, and conforms to the GIGO rule, that is, Garbage In, Garbage Out. The debugging check box allows you to see the state of the stack and other information in an alert box as the expression is evaluated. You may load a cached expression into the expression window by selecting it in the menu on the right with the mouse. A better implementation would use a recursive algorithm for clarity of code, and better syntax checking. It would also be shorter and easier to expand. I leave that implementation as an exercise to the reader. CommentsNo comments. |
|
All material copyright © 1995-2012 by
Brian Ziman, unless otherwise noted.
| |