First understand what infix and postfix expressions are
The infix expression is the formula we usually write, such as 2*(3+4)-8/2, and its corresponding suffix expression is 234+*82/-. Don't worry, let's see how to get the postfix expression.
When a number is encountered, it is output directly. When a symbol is encountered, it is pushed onto the stack. Before pushing the stack, it is necessary to judge the priority of each operator. The judgment rules are as follows.
=== off-stack>on-stack: push the off-stack operator onto the stack
===Outside the stack<Inside the stack: pop the in-stack operator from the stack
===Inside the stack=Outside the stack: It is specified that the inside of the stack is higher than the outside of the stack, and the inside of the stack is popped out of the stack
=== Encounter '(' : push directly onto the stack, and change its priority to the lowest after being pushed onto the stack (that is, outside the stack'('highest, inside the stack '('lowest )
Encounter ')': pop all operators until encounter '(' (the right parenthesis outside the stack has the lowest priority, and it is as low as the left parenthesis inside the stack)
The specific implementation is as follows: (the picture is a bit rough)
Next, use Java to implement infix expressions to postfix expressions
public class Constant {//Specifies the priority of each operator, the smaller the value, the higher the priority /** * means addition */ 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 TestM1 { public static int Get_Prio(char opera,boolean instack)//opera is the operator, instack indicates whether it is in the stack { int prio = Constant.OPERATORS_PRIO_ERROR; if(instack)//in the stack { 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//outside the stack { 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){//The former is an infix, the latter is a suffix char[] stack = new char[strMid.length()]; int top = 0; int len = strMid.length();//Length of infix expression int i = 0; // count int j = 0; // subscript of strLast int prioIn; // stack priority int prioOut; // stack priority while(i != len){ //Determine whether the current character is a number, if it is a number, it is directly pushed into the stack if(Character.isDigit(strMid.charAt(i))){//charAt is a method in Stirng type strLast[j++] = strMid.charAt(i);//Write the current character into the suffix expression i++; }else{ if(top == 0){//If there is no operator in the stack, let the current operator push the stack stack[top++] = strMid.charAt(i); }else{ prioIn = Get_Prio(stack[top - 1],true);//Get the priority of the top element of the stack prioOut = Get_Prio(strMid.charAt(i),false);//Get the priority of the element outside the stack if(prioIn < prioOut){//The priority in the stack is high, pop out of the stack strLast[j++] = stack[--top]; }else if(prioIn == prioOut){//Left parenthesis and right parenthesis meet top--;//Pop stack i++; }else{//The priority outside the stack is higher than that inside the stack stack[top++] = strMid.charAt(i); } } } } //Check if there is an operator in the stack while(top > 0){ strLast[j++] = stack[--top]; } }