//Take the provided Infix to Postfix Java source file. Correct it so that it han
ID: 3835683 • Letter: #
Question
//Take the provided Infix to Postfix Java source file. Correct it so that it handles working with unary operations correctly. import java.util.*; public class InToPost { private static int iPow(int x, int y) { int prod=x; for(int i=1;i<y;i++) prod *= x; return prod; } public static int postfixEvaluate(String exp) { Stack<Integer> s = new Stack<Integer> (); Scanner tokens = new Scanner(exp); while (tokens.hasNext()) { if (tokens.hasNextInt()) { s.push(tokens.nextInt()); } else { int num2 = s.pop(); int num1 = s.pop(); String op = tokens.next(); if (op.equals("+")) { s.push(num1 + num2); } else if (op.equals("-")) { s.push(num1 - num2); } else if (op.equals("*")) { s.push(num1 * num2); } else if (op.equals("%")) { s.push(num1 % num2); } else if (op.equals("^")) { s.push(iPow(num1,num2)); } else { s.push(num1 / num2); } // "+", "-", "*", "/" } } return s.pop(); } // Method to convert infix to postfix: // Constructor: (default) private static int operatorPrecedence(String operator) { if (operator.equals("u-") || operator.equals("u+")) { return 3; } else if (operator.equals("^")) { return 2; } else if (operator.equals("*") || operator.equals("/") || operator.equals("%")) { return 1; } else if (operator.equals("-") || operator.equals("+")) { return 0; } else { return 0; } } // Method to convert infix to postfix: public static String convertToPostfix(String infix) { String postfix = ""; boolean unary = true; // is the current operator unary? Stack<String> stack = new Stack<String>(); StringTokenizer st = new StringTokenizer(infix, "()^+-/%* ", true); while (st.hasMoreTokens()) { String token = st.nextToken().trim(); //System.out.println(token); if (token.equals("")) { // skip any empty token } else if (token.equals("(")) { stack.push(token); } else if (token.equals(")")) { String op; while (!(op = stack.pop()).equals("(")) { postfix += " " + op; } } else if (token.equals("*") || // an operator token.equals("^") || token.equals("+") || token.equals("-") || token.equals("%") || token.equals("/")) { if (unary) { token = "u" + token; // a unary op always goes on // the stack without popping any other op stack.push(token); } else { int p = operatorPrecedence(token); while (!stack.isEmpty() && !stack.peek().equals("(") && operatorPrecedence(stack.peek()) >= p) { String op = stack.pop(); postfix += " " + op; } stack.push(token); } unary = true; // if an operator is after this one, it // has to be unary } else { // an operand Integer.parseInt(token); // just to check that // it is a number // If not a number, an exception is // thrown postfix += " " + token; unary = false; // any operator after an operand is binary } } while (!stack.isEmpty()) { String op = stack.pop(); postfix += " " + op; } return postfix; }//end convertToPostfix public static void main(String[] args) { // Test method for the class. System.out.print(" Enter an infix expression to convert: "); String input = new java.util.Scanner(System.in).nextLine(); String result = InToPost.convertToPostfix(input); System.out.println("postfix: " + result +", evaluated to "+postfixEvaluate(result)); System.out.println(); } // end main }//end class InfixToPostfix
Explanation / Answer
import java.util.*; /** * class InToPost * * Class to convert infix to postfix and evaluate it */ class InToPost { /** * Converts an infix expression to postfix expression * * @param String infix expression * @return String postfix expression */ static String convert(String infix) { String postfix = ""; char c, temp; try { CharStack s = new CharStack(50); for (int i = 0; i = priority(c)) { postfix += s.pop(); if (s.isEmpty()) { break; } } s.push(c); } catch (Exception e) { System.out.println(e); } } } else if (c == ')') { try { while ((temp = s.pop()) != '(') { postfix += temp; } } catch (StackEmptyException e) { System.out.println(e); } } } while (!s.isEmpty()) { try { postfix += s.pop(); } catch (StackEmptyException e) { System.out.println(e); } } } catch (Exception e) { System.out.println(e); } return postfix; } /** * Evaluates a valid postfix expression * * @param String postfix expression * @return int value of the expression */ public static int evalPost(String postfix) { CharStack s = new CharStack(); char temp, res; int number1, number2, result = 0; for (int i = 0; i = 0 && asciiRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.