中序表达式变成后序表达式 JAVA实现版本

package algorithm;

import java.util.*;

public class ConvertMiddleOrder2PostOrder {
	   /*
	    * 
	    * convert middleOrder math expression to PostOrde
	    * for example: A-B*(C+D)/E (in-order)
	    * converted result: ABCD*E/- (post-oder)
	    */
	
	   public static String ConvertInOrder2PostOrderUtil(String inOrderString){
	
		    //support:  + - * / 
		    /* element content:
		     * l = <
		     * g = >
		     * e = =
		     * n = not available 
		     * each operation map to array index: 
		     * + =   0
		     * - =   1
		     * * =   2
		     * / =   3
		     * for example: oprPriority[2][3]=‘e',  就是 operator * 跟栈类的  /比较优先级,结果是优先级 相等。
		     * (, ),比较特殊,就在代码中处理了。
		     */
		   
	     char [][] oprPriority = new char[][]{
	    	 {'e','e','l','l'},
	    	 {'e','e','l','l'},
	    	 {'g','g','e','e'},
	    	 {'g','g','e','e'}
	    	 }; 
		
	    if(inOrderString.length()<3) {
	    	System.out.println("bad expression data");
	    	return null;
	    }
	    	
	    char[] resultarray = new char[inOrderString.length()];
	    int rstindex=0;
	    int len=inOrderString.length();
	    int curposition=0;
	    
	    //save operators
	    Stack<String> mystack = new Stack<String>();
	    char[] tmparray = new char[2];
	    char stackchar,curchar;
	    while(len>0){
	    	System.out.println("converted str="+new String(resultarray));
	    	curchar=inOrderString.charAt(curposition);
	    	switch(curchar) {
	    	case '0':
	    	case '1':
	    	case '2':
	    	case '3':
	    	case '4':
	    	case '5':
	    	case '6':
	    	case '7':
	    	case '8':
	    	case '9':
	    		resultarray[rstindex++]=curchar;
	    		curposition++;
	    		len--;
	    		break;
	    	case '+':
	    	case '-':
	    	case '*':
	    	case '/':
	    		if(!mystack.empty()){
	    		   //compare operation
	    			tmparray=mystack.peek().toCharArray();
	    		    if(tmparray[0]=='(') {
	    		    	//push this operator as '(' in stack is lowest
		    			tmparray[0]=curchar;
		    			mystack.push(new String(tmparray));
		    			curposition++;
		    			len--;
	    		    } else {
	    		     int column,row;
	    		     switch(curchar){
	    		        case '+':
	    		        	row=0;
	    		        	break;
	    		    	case '-':
	    		    		row=1;
	    		        	break;
	    		    	case '*':
	    		    		row=2;
	    		        	break;
	    		    	case '/':
	    		    		row=3;
	    		        	break;
	    		        default:
	    		        	System.out.println("expression error");
	    		    		return null;
	    		     }
	    		     switch(tmparray[0]){
	    		        case '+':
	    		        	column=0;
	    		        	break;
	    		    	case '-':
	    		    		column=1;
	    		        	break;
	    		    	case '*':
	    		    		column=2;
	    		        	break;
	    		    	case '/':
	    		    		column=3;
	    		        	break;
	    		        default:
	    		        	System.out.println("expression error");
	    		    		return null;
	    		     }
	    		     
	    		    char result=oprPriority[row][column];	
	    		    switch(result){
	    		     case 'g': // incoming > operator in stack
	    		    	    //push this operator if it is higher than stack top
			    			tmparray[0]=curchar;
			    			mystack.push(new String(tmparray));
			    			curposition++;
			    			len--;
			    			break;
	    		     case 'e': //=
	    		     case 'l': // <
	    		    	    //pop up the top operator in stack
	    		    	    mystack.pop();
	    		    	    resultarray[rstindex++]=tmparray[0];
	    		    		break;
	    		     default:
	    		        	System.out.println("expression error");
	    		    		return null;
	    		     }
	    		   }
	    		    
	    		} else {
	    			//push this operator
	    			tmparray[0]=curchar;
	    			mystack.push(new String(tmparray));
		    		curposition++;
		    		len--;
	    		}
	    		
	    		break;
	    		
	    	case '(':
	    		//push into stack
	    		tmparray[0]=curchar;
    			mystack.push(new String(tmparray));
	    		curposition++;
	    		len--;
	    		break;
	    		
	    	case ')':
	    		//pop up all operator until it meet '('
	    		while(!mystack.empty()){
	    			tmparray=mystack.peek().toCharArray();
	    			mystack.pop();
	    			if(tmparray[0]=='('){
	    				break;
	    			} else {
	    			  //save this operator to result str
	    			  resultarray[rstindex++]=tmparray[0];	
	    			}
	    		}
	    		curposition++;
	    		len--;
	    		
	    		break;
	    	default:
	    		System.out.println("expression error");
	    		return null;
	    	}	    	
	    }
	    
	    while(!mystack.empty()) {
	    	tmparray=mystack.pop().toCharArray();
	    	resultarray[rstindex++]=tmparray[0];
	    	System.out.println("converted str="+new String(resultarray));
	    }
		String result=new String(resultarray);
		return result;	
	}
}


测试语句:


ConvertMiddleOrder2PostOrder.ConvertInOrder2PostOrderUtil("1-2*(3+4)/5"))


//output is 

converted post-order expression is: 1234+*5/-


你可能感兴趣的:(算法,后序表达式,中序表达式)