Infix to Postfix and Postfix Eval of BigIntegers
Due Thursday, May 24th by 11:59:59pm
Basic documentation is required, but no javadoc is necessary -- UNLESS YOU DID NOT SUCCESSFULLY UTILIZE JAVADOC ON YOUR LAST ASSIGNMENT For this assignment you will convert infix expressions to postfix expressions and then evaluate those postfix expressions. You will be given a table of symbols (A - Z) and their associated values to be used in evaluating expressions. The value for each symbol is to be considered a BigInteger. Negative values are also possible. Following this table you will be given a series of strings which will contain INFIX expressions which may be fully or partially parenthesized, but will be valid. The infix expressions will be given one per line and the series of expressions will be terminated by a line containing only a #. You may assume the following about the INFIX expressions:
- the expressions will contain only the characters: A through Z, +, -, *, /, ^, (, ). NOTE: ^ represents the exponent operator, which is not supported in Java. You will need to make a call to BigInteger.pow( ) when applying this operator to operands to evaluate the postfix expression you create. Note that the parameter to BigInteger.pow( ) is an int. If you encounter a BigInteger exponent that is larger than Integer.MAX_VALUE, throw an exception, handle it and write the error to output. Program execution should then continue on to the next expression
- the expressions will be correctly formed and all symbols used in the expressions will be defined.
- the expressions will have no spaces between symbols and operators.
- the values associated with symbols in the expressions will be positive or negative BigIntegers. Although the symbol values may be negative, the expressions will contain no unary negative operators.
The official name for the input file for this program is infix.txt (hard-code this into your program)-- all output is to the monitor. Here is a sample file you can use for testing -- it is by no means exhaustive -- make sure you add things to this file that properly test all categories of valid expressions -- the file is guaranteed to be well-formed and contain valid data and expressions throughout for the purposes of this assignment. It is permissible for you to collaborate with classmates on the postfix file you use for testing.
After printing out the INFIX expression, your program should convert the expression to POSTFIX form, using the algorithm discussed in class. Here are the lecture notes and code discussed in class After the conversion, the POSTFIX form of the expression should be printed out to the monitor. Finally, the POSTFIX expression should be evaluated (also using the algorithm discussed in class) and the final value of the expression should be printed out to the monitor. Here is sample output matching the infix.txt file shown above. The evaluation process will require a symbol table to be used to lookup the value associated with the expression symbols. This process should be repeated for each of the expressions. You may write your own Stack class or use the Java API! If you write your own, use the linked list version we did on the board in class.
Sample input file:
A -8
C 10
D 5
#
(A + C)*D
A+C * D
#
Sample output for the above input file:
Infix Expression: (A+C)*D
Postfix Equivalent: AC+D*
Expression value after postfix evaluation: 10
Infix Expression: A+C*D
Postfix Equivalent: ACD*+
Expression value after postfix evaluation: 42
To turn in:
Turn in a zip file named with your last name, followed by the first initial of your first name, followed by hw4 hw4 (e.g. armstrongehw4.zip ). Include all source and the robust input file you built and used to test your program. Name the file that contains the mane method and drives the program: InfixPostfixTester. Note that the InfixPostfixTester class should not do any conversions or calculations. You can use it to read in the data from the input file, but no work should be done on the data by the InfixPostfixTester class.