Java -- 后缀表达式转中缀表达式

  1. 中缀表达式 转 后缀表达式 举例示意图:
    这里写图片描述
  2. 全部代码:
package ShunXuZhan;

import java.util.Arrays;

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 TestM11 {
    public static int Get_Prio(char opera,boolean instack)//char类型的一个字符,布尔类型进栈
    {
        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()];//new 一个和中缀表达式长度相等的字符数组类型的对象 stack
        int top=0;//令栈为空
        int len=strMid.length();//将中缀表达式的长度赋值给变量len
        int i=0;//计数
        int j=0;//strLast的下标
        int prioIn;//栈内优先级
        int prioOut;//栈外优先级
        while(i!=len){//遍历中缀表达式
            //判断当前的字符是不是数字
            if(Character.isDigit(strMid.charAt(i))){//i表示的是下标,如果 strMid[i] 是数字
                /*
                 //String.charAt(int index)方法
                  public char charAt(int i) {//为什么里面形参是int 型,外面是char类型,可以这样用吗?
                        if ((i < 0) || (i >= strMid.length)) {
                            throw new StringIndexOutOfBoundsException(i);//抛出一个异常
                            }
                                return strMid[i];//返回
                            }     
                 //这个方法的作用就是输出str.Mid[i]的值
                 */
                strLast[j++]=strMid.charAt(i);//是数字的情况下,将 strMid[i]值放入后缀表达式数组中
                i++;//计数
            }else{//若 strMid[i] 不是数字

                if(top==0){//栈为空
                    stack[top++]=strMid.charAt(i);//将 strMid[i] 放入栈中
                    i++;//计数
                }else{//栈不为空
                    prioIn=Get_Prio(stack[top-1],true);//栈内优先级
                    prioOut=Get_Prio(strMid.charAt(i),false);//栈外优先级
                    //数字越小,优先级越高
                    if(prioIn < prioOut){//栈内优先级高 
                        strLast[j++]=stack[--top];//将栈内 字符 放入后缀表达式中,top减一
                    }else if(prioIn==prioOut){//如果优先级相等
                        top--;//删除栈顶 元素
                        i++;//计数
                    }else{//栈外优先级高于栈内
                        stack[top++]=strMid.charAt(i);//将strMid[i]放入栈内,top加一
                        i++;//计数
                    }
                }
            }
        }//中缀表达式遍历完了之后
        //判断栈内是否还有运算符
        while(top > 0){//若栈不为空
            strLast[j++]=stack[--top];//将栈内剩余的元素全部放入后缀表达式
        }
    }

    public 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);
        System.out.println(Arrays.toString(strLast));
    }
}
  1. 输出结果:
    这里写图片描述
  2. public static void strMidToLast(String strMid,char[] strLast){}方法的逻辑图解释:
    这里写图片描述

猜你喜欢

转载自blog.csdn.net/xyxy66/article/details/80251941