We call expressions that are usually calculated as infix expressions, such as "2+3*5-4*(5-3)", because the calculation symbol is in the middle of the number. Now we need to convert infix expressions to postfix expressions.
First we need a newstack[ ] stack and strLast[ ] array.
1. If a number is encountered, store the number in the array strLast
2. When the operator is encountered, if the newstack is empty, it will be stored in it
3. When the operator is encountered, if the newstack is not empty, follow the following rules
Infix expression to postfix expression
* Rules:
* Operators of the same level, inside the stack are higher than outside the stack
* The left parenthesis outside the stack has the highest priority
* The left parenthesis inside the stack has the lowest priority
* The right parenthesis outside the stack has the lowest priority
If the outside of the stack is higher than the inside, push it to newstack
Otherwise, pop the elements in the stack until the stack is higher than the stack or the stack is empty
If equal ----" encounter () discard! ! ! Either
Code
public class Constant { /** * operator constant */ public static final int OPERATORS_PRIO_PLUS_IN = 4; //In-stack addition public static final int OPERATORS_PRIO_SUB_IN = 4; //Subtract on the stack public static final int OPERATORS_PRIO_MULTY_IN = 2; //Multiplication on the stack public static final int OPERATORS_PRIO_DIV_IN = 2 ; //In-stack division public static final int OPERATORS_PRIO_LEFT_BRAK_IN = 10; //Left parenthesis in stack public static final int OPERATORS_PRIO_PLUS_OUT = 5 ; //Addition outside the stack public static final int OPERATORS_PRIO_SUB_OUT = 5; //Subtraction off the stack public static final int OPERATORS_PRIO_MULTY_OUT = 3; //Multiplication off the stack public static final int OPERATORS_PRIO_DIV_OUT = 3; //Division outside the stack public static final int OPERATORS_PRIO_LEFT_BRAK_OUT = 1; //left parenthesis outside the stack public static final int OPERATORS_PRIO_RIGHT_BRAK_OUT = 10; //Right parenthesis outside the stack public static final int OPERATORS_PRIO_ERROR = -1; }
public class Test21 { // method to get priority 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; } //strMid infix expression strLast postfix expression public static void strMidTolast(String strMid,char[] strLast){ int len; int i = 0;//traverse infix expressions len = strMid.length(); char[] newstack = new char[len];//Open a new stack int j = 0;//traverse strLast int top = 0;//traverse newstack int inprio;//Element priority in stack int putprio;//Priority of elements outside the stack while(i!=len){//traverse infix expressions if(Character.isDigit(strMid.charAt(i))){//Determine whether it is a number strLast[j++] = strMid.charAt(i);//For numbers, put them into strLast array i++;//The infix expression moves backwards }else if(top == 0){//Determine whether newstack[] is empty newstack[top++] = strMid.charAt(i);//Push the stack if it is empty i++;//The infix expression moves backwards }else{ inprio = Get_Prio(newstack[top-1],true);//Priority in stack putprio = Get_Prio(strMid.charAt(i),false);//Out-of-stack priority if(inprio > putprio){//The priority outside the stack is less than that inside the stack newstack[top++] = strMid.charAt(i);//Push the stack i++;//The infix expression moves backwards } if(inprio == putprio){//If it is equal to ---->(), it will be discarded! ! ! ! top--; i++;//The infix expression moves backwards } if(inprio < putprio){//greater than strLast[j++] = newstack[--top];//Put the strLast array and continue to compare the priority with the next one on the stack } } } while(top != 0){//Put the remaining elements on the stack and store them in the strLast array strLast[j++] = newstack[--top]; } } public static void main(String[] args) { String strMid = "2+3*5-4*(5-3)"; char[] strLast = new char[strMid.length()]; strMidTolast(strMid,strLast); System.out.println(Arrays.toString(strLast)); } }