Infix expression to postfix expression

Rule: For operators of the same level, the priority of the left parenthesis in the stack is higher than that of the outside of the stack. 
The left parenthesis outside the stack has the highest
priority
and is pushed into the stack. The left parentheses in the stack have the same priority




//
The application of the station /* The infix expression is converted into a postfix expression Infix expression strMid: 2+3*5-4*(5-3) Postfix expression strMid: 235*+453-*- * * * * */ package com.tulun; import java.util.Arrays; public class TestMl3 { class Constant { /** * means addition / _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ / In-stack division public static final int OPERATORS_PRIO_LEFT_BRAK_IN = 10; // In-stack left bracket public static final int OPERATORS_PRIO_PLUS_OUT = 5 ; // Addition outside the stack public static final int OPERATORS_PRIO_SUB_OUT = 5; // Subtraction outside the stack public static final int OPERATORS_PRIO_MULTY_OUT = 3; // Multiplication outside the stack public static final int OPERATORS_PRIO_DIV_OUT = 3; // Stack External division method public static final int OPERATORS_PRIO_LEFT_BRAK_OUT = 1; // Left parenthesis outside the stack public static final intOPERATORS_PRIO_RIGHT_BRAK_OUT = 10; // Right bracket outside the stack public static final int OPERATORS_PRIO_ERROR = -1 ; } 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; // count is used to count the subscript of strMid int j=0; // count is used to indicate the subscript of strMid int prioIn; // the priority in the stack int prioOut; // Priority outside the stack // traverse strMid while (i!= len){ if (Character.isDigit(strMid.charAt(i))){ strLast[j]=strMid.charAt(i); j++; i++; } else { if (top==0){ // stack is empty stack[top]= strMid.charAt(i); top++; 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]; } } public static void show(char[]strLast){ for(int i=0;i<strLast.length;i++){ System.out.println(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)); } }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325902096&siteId=291194637