Question: Java InfixToPostFix class I have a problem with convert method,when I reuse it over and over it keeps adding the expressions to the first postfix
Java InfixToPostFix class
I have a problem with convert method,when I reuse it over and over it keeps adding the expressions to the first postfix expression, for example:
convert("(A + B) * C");
output1=A B + C *
conver("(A + B) * (C + D));
output2=A B + C * A B + C D +
As you can see it keeps adding each new output to the previous one. I used the textbook code for this application, which I think this problem because the Pattern variable. do I need to write my own code in order to fix this ?
public class InfixToPostfixWithParentheses { private static final String operators="-+*/()"; private static final int[]precedence= {1,1,2,2,-1,-1}; private static final String pattern="\\d+\\.\\d*|\\d+|"+"\\p{L}[\\p{L}\\p{N}]*"+"|[" + operators + "]"; private static LinkedStack
public static String convert(String infix) throws SyntaxErrorException { InfixToPostfixWithParentheses h =new InfixToPostfixWithParentheses(); h.convertToPostfix(infix); return h.getPostfix(); }
public void convertToPostfix(String infix) throws SyntaxErrorException { String nextToken; try { Scanner scan=new Scanner(infix); while((nextToken=scan.findInLine(pattern))!=null) { char firstChar=nextToken.charAt(0); if(Character.isLetter(firstChar) || Character.isDigit(firstChar)) { postfix.add(nextToken); } else if(isOperator(firstChar)) { processOperator(firstChar); } else { throw new SyntaxErrorException("Unexpected Character encountered: "+firstChar); } } while (!operatorStack.isEmpty()) { char op= operatorStack.pop(); if(op=='(') throw new SyntaxErrorException("Unmatched opening parenhesis"); postfix.add(Character.toString(op)); } }catch (NoSuchElementException ex) { throw new SyntaxErrorException("Syntax Error: The stack is Empty"); } } private void processOperator(char op) { if(operatorStack.isEmpty() || op=='(') { operatorStack.push(op); } else { char topOp=operatorStack.peek(); if(precedence(op)>precedence(topOp)) { operatorStack.push(op); } else { while(!operatorStack.isEmpty() && precedence(op)<=precedence(topOp)) { operatorStack.pop(); if(topOp=='(') { break; } postfix.add(Character.toString(topOp)); if(!operatorStack.isEmpty()){ topOp=operatorStack.peek(); } } if (op!=')'){ operatorStack.push(op); } } } }
private static boolean isOperator(char op) { return operators.indexOf(op)!=-1; } private static int precedence(char op) { return precedence[operators.indexOf(op)]; } public static void main(String[] args) throws SyntaxErrorException { InfixToPostfixWithParentheses g= new InfixToPostfixWithParentheses(); g.convertToPostfix(("10 + 3 * 5 / (16 - 4)")); System.out.println(g.getPostfix()); InfixToPostfixWithParentheses p= new InfixToPostfixWithParentheses(); p.convertToPostfix("(A+B)*(C+D)"); System.out.println(p.getPostfix()); }
}
output 1=10 3 5 * 16 4 - / + output 2=10 3 5 * 16 4 - / + A B + C D + *
how can I get a separate output when I test my code?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
