Objectives:
To gain experience with stacks and lists.
Documentation:
1. Explain the purpose of the program as detail as possible
2. Develop a solution for the problem and mention algorithms to be used
3. List data structures to be used in solution.
4. Give a description of how to use the program and expected input/output
5. Explain the purpose of each class you develop in the program.
Programming:
1. For each method, give the pre and post conditions and invariant, if any
2. Program execution according to the requirements given
3. Naming of program as required 5%
4. Print out of source code 5%
Description of Program
You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens:
(1) Integer constants (a series of decimal digits).
(2) One alphabetic character - "x" (representing a value to be supplied later).
(3) Binary operators (+, -, *, / and % (modulo)).
(4) Unary operators + & -
(5) Parentheses
You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract a postfix expression from the tree. Display this postfixexpression(String).You will supply this postfix expression (string) to the calculator engine (a function that you will write) to produce a result.
Spaces between tokens are allowed but not required. The program will repeatedly prompt the user for the value of x, displaying the value of the expression each time. When the user enters the letter q instead of a number, the program terminates.
The following example illustrates the behavior of the program (user input is in bold):
Enter infix expression: (x + 125) * (x - 2) / 4
Converted expression: x 125 + x 2 - * 4 /
Enter value of x: 5
Answer to expression: 97
Enter value of x: 7
Answer to expression: 165
Enter value of x: q
If the infix expression contains an error of any kind, the program must display the message Error in expression (with an optional explanation) and then terminate. The following examples illustrate various types of errors:
Enter infix expression: 1 2 +
Error in expression!! No operator between operands. Also last token must be an operand.
Enter infix expression: 10.4
Error in expression!! Cannot accept floating point numbers.
Enter infix expression: 1 ( + 2)
Error in expression!! No operator between operand and left parentheses.
Enter infix expression: 5 - (x - 2))
Error in expression!! No matching left parentheses for a right parentheses.
Enter infix expression: 1 ** 2
Error in expression!! The * operator cannot be preceded by a * operator.