InFix到Post Fix转换器

时间:2015-04-13 18:14:02

标签: java

我试图更改我的代码并调试它,但我总是得到错误的转换器。任何人都可以帮我告诉我错误在哪里?

public class Driver {
    static String postfix;

    public static void main(String[] args) {
        String me = "((5+6)*(7-8))";
        String a =changeFromInfixToPostfi(me);
        System.out.println(a);
    }

    public static String changeFromInfixToPostfi(String s) {
        LinkedStack stack = new LinkedStack();
        String postfix = "";
        for (int i = 0; i < s.length(); i++) {
            if (Character.isDigit(s.charAt(i)))
                postfix += s.charAt(i);
            else {
                postfix += ",";
                if (stack.isEmpty())
                    stack.Deposit(s.charAt(i));
                else {
                    while (!stack.isEmpty()) {
                        if (!checkstackTop(stack.MyPeek(), s.charAt(i) +"")) {
                            stack.Deposit(s.charAt(i));
                            break;
                        }
                        else {
                            postfix += stack.Withdraw() + ",";
                            if (stack.isEmpty()) {
                                stack.Deposit(s.charAt(i));
                                break;
                            }
                        }
                    }
                }
            }
        }
        while (!stack.isEmpty())
            postfix += "," + stack.Withdraw();
        return postfix;
    }

    public static boolean checkstackTop(Node top, String schar) {
        if ((top.equals("*") || top.equals("/"))
                && (schar.equals("+") || schar.equals("-")))
            return true;
        else if ((top.equals("+") || top.equals("-"))
                && (schar.equals("+") || schar.equals("-")))
            return true;
        else if (top.equals("(") && schar.equals(")"))
            return true;
        else if (top.equals("(") && schar.equals("("))
            return true;
        else if ((top.equals("*") || top.equals("/"))
                && (schar.equals("*") || schar.equals("/")))
            return true;
        else if ((top.equals("+") || top.equals("-") || top.equals("*") || top
                    .equals("/")) && schar.equals(")"))
            return true;
        return false;
    }
}

0 个答案:

没有答案