Handling parenthesis while converting infix expressions to postfix expressions

柔情痞子 提交于 2019-12-16 19:57:05

问题


I'm working on a project in Java that requires me to convert an infix expression to a postfix expression. I am currently able to convert infix expressions to postfix with this method as long as they don't contain parenthesis, but I can't figure out how to handle parenthesis.

Basically, I have two stacks that hold objects that are called 'Token'. A Token is a wrapper class that holds a string that is either a number, variable (which gets evaluated as a number, pending on user input), operator (the operator has a priority level associated with it so that my method can determine how to handle order of operations between '+', '-', '*' and '/'), or a parenthesis (the parenthesis has a way to determine if it is a open parenthesis or a closed parenthesis).

How should I handle parenthesis? What about multiple layers of parenthesis?

public String toPostFix() {
    StringBuilder postfixstr = new StringBuilder();

    Stack<Token> in_fix = new Stack<>();
    Stack<Token> post_fix = new Stack<>();

    for (int i = tokens.length - 1; i >= 0; i--) {
        t = new Token(tokens[i]);
        in_fix.push(t);
    }

    //there are still tokens to process
    while (!in_fix.empty()) {
        //is a number
        if (in_fix.peek().type == 1) {     
            postfixstr.append(in_fix.pop().toString());
        } 

        //is an operator and the stack is empty
        else if (in_fix.peek().type == 3 && post_fix.empty()) {   
            post_fix.push(in_fix.pop());
        } 

        // is an operator that has higher priority than the operator on the stack
        else if (in_fix.peek().type == 3 && in_fix.peek().isOperator() > post_fix.peek().isOperator()) {
            post_fix.push(in_fix.pop());
        } 

        // is an operator that has lower priority than the operator on the stack
        else if (in_fix.peek().type == 3 && in_fix.peek().isOperator() <= post_fix.peek().isOperator()) {
            postfixstr.append(post_fix.pop());
            post_fix.push(in_fix.pop());
        } 

        //puts the rest of the stack onto the output string
        if (in_fix.empty()) {
            while (!post_fix.empty()) {
                postfixstr.append(post_fix.pop());
            }
        }
    }

    return postfixstr.toString();
}

回答1:


You need to push the left parenthesis onto the stack, and process the stack like so when you encounter a right parenthesis:

// opening (
if (in_fix.peek().type == 4) {   
    post_fix.push(in_fix.pop());
}
//closing )
if(in_fix.peek().type == 5){
    while(!(post_fix.isEmpty() || post_fix.peek().type == 4)){
         postfixstr.append(post_fix.pop());
    }
    if (post_fix.isEmpty())
        ; // ERROR - unmatched )
    else
        post_fix.pop(); // pop the (
    in_fix.pop(); // pop the )
} 



回答2:


Try this way:

    //opening Parenthesis 
        if (in_fix.peek().type == 4) {   
                    post_fix.push(in_fix.pop());
        }
        //closing Parenthesis 
        if(in_fix.peek().type == 5){
             //Till opening parenthesis encountered in stack, append operators to postfix. and pop parenthesis and do not append to post_fix.
             while(post_fix.peek().type!=4){
                 postfixstr.append(post_fix.pop());
             }
            //finally pop left parenthesis from post_fix stack.
            post_fix.pop();
        } 


来源:https://stackoverflow.com/questions/19721072/handling-parenthesis-while-converting-infix-expressions-to-postfix-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!