从Infix转换为PostFix圆括号问题

时间:2015-11-10 05:29:50

标签: java

目前,我正在研究一种从Infix表示法转换为Postfix表示法的方法。我已经编写了多个JUnit测试,并且常规的东西如2 * 2或1 + 3正在工作,但带括号的任何东西都不起作用。

例如,(5 + 2)*(3 * 5)

在调试过程中,我总是看到我的postFixResult字符串总是添加了第一个括号,所以字符串的开头是5(2.我不确定我做错了什么,因为我已经跟着多个这里有关于这个问题的帖子。

public static String infixToPostFix(String message) throws PostFixException {
        MyStack<Character> operatorStack = new MyStack<Character>();
        String postFixResult = "";
        Scanner tokenizer = new Scanner(message);

        while (tokenizer.hasNext()) {
            if (tokenizer.hasNextInt()) {
                postFixResult += " " + tokenizer.nextInt();
            } else {
                String value = tokenizer.next();
                Operator op = new Operator(value.charAt(0));
                char c = value.charAt(0);
                String operators = "*/-+()";

                if (!isOperator(c)) {
                    //If the character is not an operator or a left parentheses.
                    throw new PostFixException("Invalid Operator");
                } else {
                    if (c == ')') {
                        while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {
                            postFixResult += " " + operatorStack.pop();
                        }
                        if (!operatorStack.isEmpty()) {
                            operatorStack.pop();
                        }
                    }
                    else {
                        if (!operatorStack.isEmpty() && !isLowerPrecedence(c, operatorStack.peek())) {
                            operatorStack.push(c);
                        }
                        else {
                            while (!operatorStack.isEmpty() && isLowerPrecedence(c, operatorStack.peek())) {
                                Character pop = operatorStack.pop();
                                if (c != '(') {
                                    postFixResult += " " + pop;
                                } else {
                                    c = pop;
                                }

                            }
                            operatorStack.push(c);
                        }
                    }
                }

            }
        }
        while (!operatorStack.isEmpty()) {
            postFixResult += " " + operatorStack.pop();
        }

        return postFixResult;
    }

    /**
     * Method that returns true if c is an operator.
     * @param c
     * @return
     */
    private static boolean isOperator(char c)
    {
        return c == '+' || c == '-' || c == '*' || c == '/' || c == '^'
                || c == '(' || c == ')';
    }

    /**
     * Determines whether or not the character is actually a number.
     * @param c
     * @return true if character is number false, otherwise.
     */
    private boolean isNumber(char c){
        return Character.isDigit(c);
    }

    /**
     * A method to determine precedence of operators.
     * @param c1 Operator 1
     * @param c2 Operator 2
     * @return true if c2 is lower precedence than c1.
     */
    private static boolean isLowerPrecedence(char c1, char c2)
    {
        switch (c1)
        {
            case '+':
            case '-':
                return !(c2 == '+' || c2 == '-');

            case '*':
            case '/':
                return c2 == '^' || c2 == '(';

            case '^':
                return c2 == '(';

            case '(':
                return true;

            default:
                //means that the character must have passed through as something else.
                return false;
        }
    }

1 个答案:

答案 0 :(得分:0)

后缀表达式中不需要括号,这就是它的美妙之处。 '('和')'不应该进入你的最终结果(postfix),并且当从堆栈本身弹出时应该被丢弃。