Search code examples
javainfix-notationstackpostfix-notation

Infix to Postfix using stacks and Precedence of Operators


I know there are similar questions to this already on SO but I can't find one which solves the problem I'm having. I am trying to make a method which converts infix notation expressions to postfix notation, while implementing precedence of operators in order to get correct output. I have made my own stack class with the usual methods (push, pop, peek etc.) and it works absolutely fine. My problem is that for more complicated expressions such as A-(B+C^D^C)/D*B , I am getting the wrong output. The result of the conversion should be ABCDC^^+D/B*- whereas i keep on getting ABCDC^^+D/-B

here is my method:

    public static String infixToPostfix(char[] expressionArray, CharStack opStack){
    String output = "";
    int length = expressionArray.length;
    for(int i = 0; i < length; i++){    
        if(isOperatorOrBracket(expressionArray[i])){ 
            if(priorityAtInput(expressionArray[i]) >= priorityAtStack(opStack.peek())){
                opStack.push(expressionArray[i]);
            }else if(priorityAtInput(expressionArray[i]) == priorityAtStack(opStack.peek())){
                output = output + expressionArray[i];
            }else{
                while(opStack.peek() != '('){
                    output = output + opStack.pop();
                }
                opStack.pop();
            }
        }else{
            output = output + expressionArray[i];
        }
    }
    while(!opStack.empty()){
        if(opStack.peek() != '('){
            output = output + opStack.pop();
        }else if(opStack.peek() == '('){
            opStack.pop();
        }
    }
    return output;
}

Please let me know if you need to any of the component methods. Any help greatly appreciated!


Solution

  • After an hour of staring at the screen I found the problem. Thank goodness for the debugger in eclipse!

    public static String infixToPostfix(char[] expressionArray, CharStack opStack){
        String output = "";
        int length = expressionArray.length;
        for(int i = 0; i < length; i++){    
            if(isOperatorOrBracket(expressionArray[i])){ 
                if(priorityAtInput(expressionArray[i]) >= priorityAtStack(opStack.peek())){
                    opStack.push(expressionArray[i]);
                }else if(priorityAtInput(expressionArray[i]) < priorityAtStack(opStack.peek())){
                    while(priorityAtInput(expressionArray[i]) < priorityAtStack(opStack.peek())){
                        output = output + opStack.pop();
                        if(opStack.peek() == '('){
                            opStack.pop();
                            break;
                        }else if(priorityAtInput(expressionArray[i]) >= priorityAtStack(opStack.peek())){
                            opStack.push(expressionArray[i]);
                            break;
                        }
                    }
                }else{
                    while(opStack.peek() != '('){
                        output = output + opStack.pop();
                    }
                    opStack.pop();
                }
            }else{
                output = output + expressionArray[i];
            }
        }
        while(!opStack.empty()){
            if(opStack.peek() != '('){
                output = output + opStack.pop();
            }else if(opStack.peek() == '('){
                opStack.pop();
            }
        }
        return output;
    }