中缀表达式变为后缀表达式

中缀表达式变为后缀表达式的简单算法是:例如,2+3*5-4*(5-3);那么,先括号里面的,2+3*5-4*(5-3);再而是乘除;2+(3*5)-(4*(5-3));最后是加减,((2+(3*5))-(4*(5-3)));之后就是从里到外一层一层去括号,去一层,把一个符号提出来写在外面;那么则((2+(3*5))-(4*(5-3)))去除最里面的括号为((2+35*)-(4*53-)),再去为(253*+-453-),再去为253+453--;所以2+3*5-4(5-3)的后缀表达式为253*+453-*-;

代码实现该过程用到了栈,

这里写图片描述

这是其中同一个包下面的类,该类把栈内栈外的符号优先级表示了出来:

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;

}

算法实现代码:

package com.xagy.zzhz;

import java.util.Arrays;

/*
 * 中缀    后缀
*/
class Testm {
    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();
        int i = 0;//计数,遍历中缀表达式
        int j = 0;//strLast的下标
        int prion;//栈内优先级
        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 {//栈不为空
                    prion = GetPrio(stack[top-1],true);
                    prioOut = GetPrio(strMid.charAt(i),false);
                    //栈内优先级高
                    if(prion < prioOut) {
                        strLast[j++] = stack[--top];
                    }
                    else if(prion == prioOut) {
                        top--;
                        i++;
                    }else {//栈外高于栈内
                        stack[top++] = strMid.charAt(i);
                        i++;
                    }
                }
            }
        }
        //栈内是否还有运算符
        while(top > 0) {
            strLast[j++] = stack[--top];   
        }
    }   
        public static void show(char[] strLast) {
            for(int k=0;k<strLast.length;k++) {
                System.out.print(strLast[k]+" ");
            }
            System.out.println();
        }
}   
public class Zhonzhuihouzhui {

    public static void main(String[] args) {
        String strMid = "2+3*5-4*(5-3)";
        char[] strLast = new char[strMid.length()];
        Testm.strMidToLast(strMid,strLast);
        System.out.println(Arrays.toString(strLast));

    }



}

运行结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq2899349953/article/details/80245402