中缀式变后缀式

 

 

package huawei;

import java.util.Stack;

public final class Demo {

    private static final int DIGIT_TYPE = 1;
    private static final int LOW_CALC_TYPE = 2;
    private static final int HIGHT_CALC_TYPE = 3;
    private static final int LEFT_BRACKETS_TYPE = 4;
    private static final int RIGHT_BRACKETS_TYPE = 5;

    private static int getCharacterType(char c) {
        int type = -1;
        if (Character.isDigit(c)) {
            type = DIGIT_TYPE;
        } else if (c == '(') {
            type = LEFT_BRACKETS_TYPE;
        } else if (c == '*' || c == '/') {
            type = HIGHT_CALC_TYPE;
        } else if (c == '+' || c == '-') {
            type = LOW_CALC_TYPE;
        } else if (c == ')') {
            type = RIGHT_BRACKETS_TYPE;
        }

        return type;
    }

    public static String expChange(String strInExp) {
        if (null == strInExp) {
            return null;
        }

        int length = strInExp.length();
        if (0 == length || length > 1000) {
            return null;
        }

        Stack<Character> stack = new Stack<Character>();
        StringBuffer sb = new StringBuffer();
        char[] c = strInExp.toCharArray();
        // System.out.println(expChange("(1+21)*(3+4)"));
        for (int i = 0; i < length; i++) {
            int type = getCharacterType(c[i]);
            if (type == DIGIT_TYPE) {
                sb.append(c[i]);
                if (i == length - 1) {
                    sb.append(" ");
                } else {
                    int tempType = getCharacterType(c[i + 1]);
                    if (tempType != DIGIT_TYPE) {
                        sb.append(" ");
                    }
                }
            } else {
                if (stack.isEmpty()) {
                    stack.push(c[i]);
                } else {
                    if (type == RIGHT_BRACKETS_TYPE) {
                        while (!stack.isEmpty()
                                && getCharacterType(stack.peek()) != LEFT_BRACKETS_TYPE) {
                            sb.append(stack.pop() + " ");
                        }
                        stack.pop();
                    } else {
                        while (!stack.isEmpty() && type <= getCharacterType(stack.peek())
                                && getCharacterType(stack.peek()) != LEFT_BRACKETS_TYPE) {
                            sb.append(stack.pop() + " ");
                        }

                        stack.push(c[i]);
                    }
                }
            }
        }

        while (!stack.isEmpty()) {
            sb.append(stack.pop() + " ");
        }

        return sb.toString().trim();
    }
    
    public static void main(String[] args) {
        System.out.println(expChange("8+9"));
        System.out.println(expChange("1+2+3"));
        System.out.println(expChange("(1+2)*3"));
        System.out.println(expChange("1+2*3"));
        System.out.println(expChange("(1+21)*(3+4)"));
        System.out.println(expChange("((1+2)+3)*(3+4)"));
        System.out.println(expChange("(12+13)*1"));
        System.out.println(expChange("3+4*(12+13)"));
        System.out.println(expChange("(1+2)/(3+4)*5"));
    }
}

8 9 +
1 2 + 3 +
1 2 + 3 *
1 2 3 * +
1 21 + 3 4 + *
1 2 + 3 + 3 4 + *
12 13 + 1 *
3 4 12 13 + * +
1 2 + 3 4 + / 5 *

 

posted @ 2018-10-19 14:29  牧 天  阅读(120)  评论(0)    收藏  举报