14. Use stack to implement infix expression to postfix expression

Conversion rules:

  • For operators of the same level, the outside of the stack is higher than the inside of the stack.
  • Left parentheses off the stack have the highest precedence.
  • The left parenthesis on the stack has the lowest precedence.
  • Off-stack closing parentheses have the lowest precedence. As low as the precedence of the left parenthesis on the stack.

1. Write a class to store the priority, the smaller the number, the higher the priority.

public class 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;
}

2. Get the priority of the current operator.

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;
	}

3. Convert infix expression to postfix expression code.

public static void strMidToLast(String strMid,char[] strLast){
		char[] stack = new char[strMid.length()];//Storage operator
		int top = 0;
		int len = strMid.length();
		int i = 0; // count
		int j = 0;// subscript of strLast
		int prioIn;
		int prioOut;
		while(i != len){
			//Determine whether the current character is a number or not.
			if(Character.isDigit(strMid.charAt(i))){
				strLast[j++] = strMid.charAt(i);
				i++;
			}else{//Not a number
				if(top == 0){//The stack is empty.
					stack[top++] = strMid.charAt(i);
					i++;
				}else{//The stack is not empty.
					prioIn = Get_Prio(stack[top-1],true);//栈内
					prioOut = Get_Prio(strMid.charAt(i),false);//栈外    
					//In-stack priority is higher than out-of-stack. The smaller the number, the higher the priority. ---> pop the stack
					if(prioIn < prioOut){
						strLast[j++] = stack[--top];
						// equal priority. (encountered parentheses)
					}else if(prioIn == prioOut){//The case of brackets
							top--;
							i++;
					}else{
						//Outside the stack is higher than inside the stack. ----> Push to stack
						stack[top++] = strMid.charAt(i);
						i++;
					}
				}
			}
		}
		//Check if there is an operator in the stack
		while(top > 0){
			strLast[j++] = stack[--top];
		}
	}

Guess you like

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