Question
You can combine the algorithms for converting between infix to postfix and for evaluating postfix to evaluate an infix expression directly. To do so you
You can combine the algorithms for converting between infix to postfix and for evaluating postfix to evaluate an infix expression directly. To do so you need two stacks: one to contain operators and the other to contain operands. When an operand is encountered, it is pushed onto the operand stack. When an operator is encountered, it is processed as described in the infix to postfix algorithm. When an operator is popped off the operator stack, it is processed as described in the postfix evaluation algorithm: The top two operands are popped off the operand stack, the operation is performed, and the result is pushed back onto the operand stack. Write a program to evaluate infix expressions directly using this combined algorithm.
The calculator must be able to compute any expression of floating point numbers using the operands +, -, *, or / (you need not implement parentheses, nor do you need to handle negative input numbers such as "-5").
The essence of your calculator is a class Calculator with at least the method double calculate(String expression). The program should be able to accept arbitrary, valid infix expressions that use these four operators.
Write test code to accept input from the user and print the result. You are welcome to create either a graphical user interface or a text-based user interface. For this project, preferably use the java.util.Stack
Sample interaction:
If using a text-based user interface, your test program could behave as follows. The parts your program prints are in this font, the user input is in this font.
calc 2 + 2 4 calc 2.1 + 2.4 4.5 calc 3 * 3 - 2 - 1 6 calc 1 + 2 + 1.5 * 8 / 3 7
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started