The infix expression into postfix expression

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
package org.lazyzhong.MyStack;
import java.util.Stack;
public class test1 {
	static Stack stack = new Stack(); 
	static Stack stack1 = new Stack();
	static String newstr="";
	public static void main(String[] args) {
		String myimg="2*(1+3)-5*(1+2)";
		System.out.println(toSuffixExpression(myimg));
		System.out.println(doParse(toSuffixExpression(myimg)));
	}
	
	public static String toSuffixExpression(String mystr) {  
        
        for(int i=0;i<mystr.length();i++){
        	char ch=mystr.charAt(i);
        	switch(ch){
        		case '+':
        		case '-':
        			getOP(ch,1);
        			break;
        		case '*':
        		case '/':
        			getOP(ch,2);
        			break;
        		case '(':
        			stack.push(ch);
        			break;
        		case ')':
        			getParen(ch);
        			break;
        		default:
        			newstr+=ch;
        			break;
        	}
        }
        
        while(!stack.isEmpty()){
        	newstr+=stack.pop();
        }
        return newstr;
	}
	
	public static void getOP(char ch,int p){
		while(!stack.isEmpty()){
			char op=(char) stack.pop();
			if(op=='('){
				stack.push(op);
				break;
			}else{
				int p2;
				if(op=='+' || op=='-')
					p2=1;
				else p2=2;
				if(p2<p){
					stack.push(op);
					break;
				}
				else{
					newstr+=op;
				}
			}
		}
		stack.push(ch);
	}
	
	public static void getParen(char ch){
		while(!stack.isEmpty()){
			char chx=(char) stack.pop();
			if(chx=='(')
				break;
			else
				newstr+=chx;
		}
	}
	
// Calculate the value of the suffix expressions
	public static int doParse(String mystr){
		char ch;
		int num1,num2,result;
		for(int i=0;i<mystr.length();i++){
			ch=mystr.charAt(i);
			if(ch>='0' && ch<='9')
				stack1.push((int)(ch-'0'));
			else{
				num1=(int) stack1.pop();
				num2=(int) stack1.pop();
				switch(ch){
					case '+':result=num1+num2;break;
					case '-':result=num2-num1;break;
					case '*':result=num1*num2;break;
					case '/':result=num1/num2;break;
					default:result=0;
				}
				stack1.push(result);
			}
		}
		result=(int) stack1.pop();
		return result;
	}
}

/*
 *	To postfix expression: 
 *	(1)If it is (directly onto the stack. . 
 *	(2) If it is) will stack operator are added to the output, until(
 * 	(3)Not brackets situation, if not the operator, will directly add it to the output, if the operator is to compare. If the operator precedence over the top of the stack operator, is directly
 * 	The operator of the stack, otherwise the stack operators turn out to the output, until the meet operator precedence than the lower, and the operator into the stack, 
 * 	If there is still operator after scanning the stack, then turn out to output. 
 * 
 * 
 */

/*
 *  Suffix expression calculation
 *  The use of the stack. . 
 *  As long as meet numbers on the stack...
 *  When the operation of the sign of the time from the stack to take out two numbers to calculate.. And the calculated value onto the stack. . . 
 *  The last remaining is calculated. . . 
 *  
 */

/*
 * Here only to write only applies to 0-9.. If suitable for multi digit number, you simply add a judgment statement as a number as a digital. . 
 * 
 */
	

 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Malcolm at December 18, 2013 - 9:29 PM