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

运行级别的比较

规则:
1.相同等级的运算符,栈内高于栈外
2.栈外的左括号优先级最高
3.栈内的左括号优先级最低
4.栈外的右括号优先级最低(低到和栈内的左括号相同)

public class Constant {
    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 TestMl {
    public static int getPrio(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 strMidToLast(String strMid,char[] strLast){
        char[] stack = new char[strMid.length()];
        int top = 0;
        int len = strMid.length();
        //i用来计数
        int i = 0;
        //用来表示strLast的下标
        int j = 0;
        //栈内优先级
        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];
        }
    }
    /**
     * 打印输出数组
     * @param strLast
     */
    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) {
        // TODO Auto-generated method stub
        //中缀表达式
        String strMid = "2+3*5-4*(5-3)";
        char[] strLast = new char[strMid.length()];
        strMidToLast(strMid,strLast);
        show(strLast);
    }

}

运行结果:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值