中缀表达式到后缀表达式的转换

规则:
相同等级迅运算符,栈内高于栈外
栈外的左括号优先级最高,入栈
栈内的左括号优先级最低
栈外右括号优先级最低,与栈内左括号优先级相同
优先级的定义:

public class Constent {
//数字越小优先级越高
    public static final int OPERATORS_PRIO_PLUS_IN = 4;  //栈内加法
    public static final int OPERATORS_PRIO_SUB_IN  =  4;   //栈内减法
    public static final int  OPERATORS_PRIO_MULTY_IN  =  2; //栈内乘法
    public static final int OPERATORS_PRIO_DIV_IN  =  2 ;  //栈内除法
    public static final int OPERATORS_PRIO_LEFT_BRAK_IN  =  10;  //栈内左括号

    public static final int OPERATORS_PRIO_PLUS_OUT  =  5 ; //栈外加法
    public static final int OPERATORS_PRIO_SUB_OUT  =   5;   //栈外减法
    public static final int OPERATORS_PRIO_MULTY_OUT  =  3; //栈外乘法
    public static final int OPERATORS_PRIO_DIV_OUT  =  3;   //栈外除法
    public static final int OPERATORS_PRIO_LEFT_BRAK_OUT =  1;  //栈外左括号
    public static final int OPERATORS_PRIO_RIGHT_BRAK_OUT =  10;  //栈外右括号
    public static final int OPERATORS_PRIO_ERROR = -1;

}
public class str {

    public static int Get_Prio(char opera,boolean instack)
    {
        int prio = Constant.OPERATORS_PRIO_ERROR;
        if(instack)
        {
            switch(opera)
            {
            case '+':
                prio = Constant.OPERATORS_PRIO_PLUS_IN;
                break;
            case '-':
                prio = Constant.OPERATORS_PRIO_SUB_IN;
                break;
            case '*':
                prio = Constant.OPERATORS_PRIO_MULTY_IN;
                break;
            case '/':
                prio = Constant.OPERATORS_PRIO_DIV_IN;
                break;
            case '(':
                prio = Constant.OPERATORS_PRIO_LEFT_BRAK_IN;
                break;
            default:
                prio = Constant.OPERATORS_PRIO_ERROR;
                break;
            }
        }
        else
        {
            switch(opera)
            {
            case '+':
                prio = Constant.OPERATORS_PRIO_PLUS_OUT;
                break;
            case '-':
                prio = Constant.OPERATORS_PRIO_SUB_OUT;
                break;
            case '*':
                prio = Constant.OPERATORS_PRIO_MULTY_OUT;
                break;
            case '/':
                prio = Constant.OPERATORS_PRIO_DIV_OUT;
                break;
            case '(':
                prio = Constant.OPERATORS_PRIO_LEFT_BRAK_OUT;
                break;
            case ')':
                prio = Constant.OPERATORS_PRIO_RIGHT_BRAK_OUT;
                break;
            default:
                prio = Constant.OPERATORS_PRIO_ERROR;
                break;
            }
        }
        return prio;
    }


    public static void strMisToLast(String strMid,char[] strLast ){
        char[] stack = new char[strMid.length()];
        int top = 0;
        int len = strMid.length();
        int i = 0;//计数
        int j = 0;//strLast的下标
        int prioIn;//栈内优先级
        int prioOut;//栈外优先级
        while(i != len){
            //判断当前字符是否是数字
            if(Character.isDigit(strMid.charAt(i))){
                strLast[j++] = strMid.charAt(i);
                i++;
            }
            else{
                if(top == 0){//栈为空
                    stack[top++] = strMid.charAt(i);
                    i++;
                }
                else{//栈不为空
                    prioIn = getPrio(stack[top-1],true);
                    prioOut = getPrio(strMid.charAt(i),false)
                    //栈内优先级高
                    if(prioIn<prioOut){
                        strLast[j++] = stack[--top];//出栈
                    }
                    else if(prioIn == prioOut){//左右括号
                        top--;
                        i++;
                    }else{//站外优先级高于栈内
                        stack[top++] = strMid.charAt(i);
                        i++;
                    }
                }
            }
        }
        //判断栈内是否含有运算符
        while(top>0){
            strLast[j++] = stack[--top];
        }
    }
    //show
       public static void show(char[] strLast){
        for (int i = 0; i < strLast.length; i++) {
            System.out.print(strLast[i]+" ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        String strMid = "2+3*5-4*(5-3)";  //中缀表达式
        char[] strLast = new char[strMid.length()];
        strMidToLast(strMid,strLast);
        show(strLast);
    }
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/Mars1997/article/details/80257502