带括号的Infix到Postfix转换器

时间:2013-09-10 03:26:50

标签: java calculator postfix-notation infix-notation

我正在做一个infixToPostfix转换器用于计算器分配。我的问题是输出似乎在它不应该有的时候显示括号。我坚持这个。有人可以帮帮我吗?感谢。

import java.util.ArrayList;
import java.util.Stack;

class infixToPostfix{

    Stack<String> stack;
    ArrayList<String> operators;

    String postFix;

    int[] operand = {-1, -1, 1};
    int[] plusorminus = {1,2,-1};
    int[] timesordivide = {3,4,-1};
    int[] raiseto = {6,5,-1};
    int[] openparenthesis = {-1,0,-1};

    public infixToPostfix(String infix) {

        stack = new Stack<String>();
        operators = new ArrayList<String>();

        operators.add("+");
        operators.add("-");
        operators.add("x");
        operators.add("/");
        operators.add("^");
        operators.add("(");
        operators.add(")");

        postFix = new String();

        while(infix.length() > 1){

            String operand = new String();
            String operator = new String();

            if(!operators.contains(infix.substring(0, 1))){
                while(!operators.contains(infix.substring(0, 1)) && !infix.isEmpty()){
                    operand = infix.substring(0,1);
                    infix = infix.substring(1);
                }
                postFix = postFix + operand;
            }
            else if(operators.get(5).equals(infix.substring(0, 1))){
                stack.push(infix.substring(0, 1));
                infix = infix.substring(1);
            }
            else if(operators.get(6).equals(infix.substring(0, 1))){
                while(!stack.peek().equals("(")){
                    postFix = postFix + stack.pop();
                }
                stack.pop();
                infix = infix.substring(1);
            }
            else{
                operator = infix.substring(0,1);

                int[] current = getICPandISP(operator);

                if(!stack.isEmpty()){
                    int[] top = getICPandISP(stack.peek());
                    while(current[0] < top[1] && !stack.isEmpty()){
                        postFix = postFix + stack.pop();
                        if(!stack.isEmpty())
                            top = getICPandISP(stack.peek());
                    }
                }
                stack.push(operator);
                infix = infix.substring(1);
            }
        }
        postFix = postFix + infix;

        while(!stack.isEmpty()){
            postFix = postFix + stack.pop();
        }
    }

    public String toString(){
        return postFix;
    }

    private int[] getICPandISP(String operator){
        if(operator.equals("+") || operator.equals("-")){
            return plusorminus;
        }
        else if(operator.equals("x") || operator.equals("/")){
            return timesordivide;
        }
        else if(operator.equals("^")){
            return raiseto;
        }
        else{
            return openparenthesis;
        }
    }

    public static void main(String[] args){
        infixToPostfix convert = new infixToPostfix("A+B/C-(A/D)*(A+(C-E^F))");
        System.out.println(convert);
    }

}

1 个答案:

答案 0 :(得分:1)

代码中有两个小错误。首先,您正在跳过表达式中的最后一个字符 - 事实证明,它是一个右括号:

while(infix.length() > 1){ //should be infix.length() > 0
    // ....
}

其次,您的代码使用'x'作为乘法运算符,而您的表达式使用'*'