Please submit your programs to our e-tutor
Your project is to design a program to implement a calculator. The calculator will take an infix expression convert it to a postfix expression and then evaluate it.
You should implement the calculator in two parts:
A Converter class that will convert the input string to postfix.
A Calculator class that will evaluate the postfix expression.
The textbook contains explanations of the algorithms you will need. See pages 84 â 86 for the algorithm you should use for the Calculator class and pages 86 - 89 for the Converter algorithm (Another explanation of the two algorithms can be found here). You can also use the ideas from the PostCalc class we looked at in lecture for your Calculator class. However, it will need to be modified to work for this assignment.
Both the Converter and Calculator classes, should use the StackLI class for the Stack.
Basically this class will be the same as the Postcalc class shown in lecture. The main differences will be:
The input will be an infix expression instead of a postfix expression. It will be entered by the user instead of hard coded by you.
The input may have operands which consist of multiple digits -- not just single digits like PostCalc
Parentheses are legal in the infix expression (Note: The parentheses are needed in the infix expression. After your Converter class' algorithm converts the expression to postfix, it will no longer have parentheses because they are not necessary in postfix expressions.)
the Calculator class will instantiate an object of the Converter class in order to have the infix expression converted to a postfix expression.
you may have to modify the other methods in the PostCalc class (or add additional ones).
The converter class will use a stack in order to implement the algorithm in the book.
When the Converter class is instantiated, a String is passed representing the infix expression entered by the user. This should be saved as an instance variable.
The method that does all the work in the Converter class should be called toPostFix(). It will convert the infix expression to a postfix expression. (It should return a Queue (use the queue from the book) of Strings representing the postfix expression to the Calculator class.
The details and helper methods used in this class are left up to you.
Your output should show the converted postfix string and the result of the calculation. For instance, here is a sample run of the program (user input in italics):
type your infix expression (4+8)*(6-5)/((3-2)*(2+2)) converted to postfix: 4 8 + 6 5 - * 3 2 - 2 2 + * / answer is 3.0