Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You can combine the algorithms for converting between infix to postfix and for e

ID: 3592008 • Letter: Y

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 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<E> class.

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

Explanation / Answer

import java.util.Stack; /** * Class to evaluate infix and postfix expressions. * public class InfixPostfixEvaluator { /** * Operators in reverse order of precedence. */ private static final String operators = "-+/*"; private static final String operands = "0123456789"; public int evalInfix(String infix) { return evaluatePostfix(convert2Postfix(infix)); } public String convert2Postfix(String infixExpr) { char[] chars = infixExpr.toCharArray(); Stack&lt;Character&gt; stack = new Stack&lt;Character&gt;(); StringBuilder out = new StringBuilder(infixExpr.length()); for (char c : chars) { if (isOperator(c)) { while (!stack.isEmpty() && stack.peek() != '(') { if (operatorGreaterOrEqual(stack.peek(), c)) { out.append(stack.pop()); } else { break; } } stack.push(c); } else if (c == '(') { stack.push(c); } else if (c == ')') { while (!stack.isEmpty() && stack.peek() != '(') { out.append(stack.pop()); } if (!stack.isEmpty()) { stack.pop(); } } else if (isOperand(c)) { out.append(c); } } while (!stack.empty()) { out.append(stack.pop()); } return out.toString(); } public int evaluatePostfix(String postfixExpr) { char[] chars = postfixExpr.toCharArray(); Stack&lt;Integer&gt; stack = new Stack&lt;Integer&gt;(); for (char c : chars) { if (isOperand(c)) { stack.push(c - '0'); // convert char to int val } else if (isOperator(c)) { int op1 = stack.pop(); int op2 = stack.pop(); int result; switch (c) { case '*': result = op1 * op2; stack.push(result); break; case '/': result = op2 / op1; stack.push(result); break; case '+': result = op1 + op2; stack.push(result); break; case '-': result = op2 - op1; stack.push(result); break; } } } return stack.pop(); } private int getPrecedence(char operator) { int ret = 0; if (operator == '-' || operator == '+') { ret = 1; } else if (operator == '*' || operator == '/') { ret = 2; } return ret; } private boolean operatorGreaterOrEqual(char op1, char op2) { return getPrecedence(op1) &gt;= getPrecedence(op2); } private boolean isOperator(char val) { return operators.indexOf(val) &gt;= 0; } private boolean isOperand(char val) { return operands.indexOf(val) &gt;= 0; } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote