Question: Application #5 Write an application that converts infix expression to postfix and evaluates it. Class Postfix includes convertToPostfix and evaluatePostfix methods that should implement algorithms
Application #5
Write an application that converts infix expression to postfix and evaluates it.
Class Postfix includes convertToPostfix and evaluatePostfix methods that should implement algorithms given in Segments 5.16 and 5.18 respectively. Assume that the given algebraic expressions are syntactically correct. The String method replaceAll, StringBuilder class, and Character.isLetter method should be utilized.
Your Task:
1. Analyze provided skeleton, UML diagram and the sample run.
2. When working with the Stack use only the stacks "vanilla" methods: push, pop, and peek.
public class Postfix { /** * Creates a postfix expression that represents a given infix expression. * Segment 5.16 * * @param infix a string that is a valid infix expression * @return a string that is the postfix expression equivalent to infix */ public void convertToPostfix(String infix) { // TODO PROJECT #5 System.out.println("Infix: " + infix); Stack operatorStack = new Stack<>(); StringBuilder postfix = new StringBuilder(); int characterCount = infix.length(); char topOperator; // IMPLEMENT ALGORITHM 5.16 System.out.println("Postfix: " + postfix.toString()); System.out.println(" "); } // end convertToPostfix /** * Indicates the precedence of a given operator. * * @param operator a character that is (, ), +, -, *, /, or ^ * @return an integer that indicates the precedence of operator: * 0 if ( or ), 1 if + or -, 2 if * or /, 3 if ^, -1 if * anything else */ private int getPrecedence(char operator) { switch (operator) { case '(': case ')': return 0; case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; } // end switch return -1; } // end getPrecedence /** * Evaluates a postfix expression. * Segment 5.18 * * @param postfix a string that is a valid postfix expression * @return the value of the postfix expression */ public double evaluatePostfix(String postfix) { // TODO PROJECT #5 Stack valueStack = new Stack<>(); int characterCount = postfix.length(); // IMPLEMENT ALGORITHM 5.18 return 0; // THIS IS A STUB } // end evaluatePostfix private double valueOf(char variable) { switch (variable) { case 'a': return 2.0; case 'b': return 3.0; case 'c': return 4.0; case 'd': return 5.0; case 'e': return 6.0; } // end switch return 0; } // end valueOf private double compute(double operandOne, double operandTwo, char operator) { double result = 0; switch (operator) { case '+': result = operandOne + operandTwo; break; case '-': result = operandOne - operandTwo; break; case '*': result = operandOne * operandTwo; break; case '/': if (operandTwo != 0) result = operandOne / operandTwo; break; case '^': result = Math.pow(operandOne, operandTwo); break; } // end switch return result; } // end compute public static void main(String[] args) { Postfix tester = new Postfix(); System.out.println("Converting infix expressions to postfix expressions: "); tester.convertToPostfix("a+b"); tester.convertToPostfix("(a + b) * c"); tester.convertToPostfix("a * b / (c - d)"); tester.convertToPostfix("a / b + (c - d)"); tester.convertToPostfix("a / b + c - d"); tester.convertToPostfix("a^b^c"); tester.convertToPostfix("(a^b)^c"); tester.convertToPostfix("a*(b/c+d)"); tester.convertToPostfix("(a+b)/(c-d)"); tester.convertToPostfix("a/(b-c)*d"); tester.convertToPostfix("a-(b/(c-d)*e+f)^g"); tester.convertToPostfix("(a-b*c)/(d*e^f*g+h)"); System.out.println("Evaluating postfix expressions with " + "a = 2, b = 3, c = 4, d = 5, e = 6"); System.out.println("Assuming correct input!!! "); System.out.println("ae+bd-/ : " + tester.evaluatePostfix("ae+bd-/") + " "); System.out.println("abc*d*- : " + tester.evaluatePostfix("abc*d*-") + " "); System.out.println("abc-/d* : " + tester.evaluatePostfix("abc-/d*") + " "); System.out.println("ebca^*+d- : " + tester.evaluatePostfix("ebca^*+d-") + " "); System.out.println("Done."); } // end main } // end Postfix SAMPLE RUN
Converting infix expressions to postfix expressions:
Infix: a+b
Postfix: ab+
Infix: (a + b) * c
Postfix: ab+c*
Infix: a * b / (c - d)
Postfix: ab*cd-/
Infix: a / b + (c - d)
Postfix: ab/cd-+
Infix: a / b + c - d
Postfix: ab/c+d-
Infix: a^b^c
Postfix: abc^^
Infix: (a^b)^c
Postfix: ab^c^
Infix: a*(b/c+d)
Postfix: abc/d+*
Infix: (a+b)/(c-d)
Postfix: ab+cd-/
Infix: a/(b-c)*d
Postfix: abc-/d*
Infix: a-(b/(c-d)*e+f)^g
Postfix: abcd-/e*f+g^-
Infix: (a-b*c)/(d*e^f*g+h)
Postfix: abc*-def^*g*h+/
Evaluating postfix expressions with
a = 2, b = 3, c = 4, d = 5, e = 6
Assuming correct input!!!
ae+bd-/ : -4.0
abc*d*- : -58.0
abc-/d* : -10.0
ebca^*+d- : 49.0
Done.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
