All pastes #253863 Raw Edit

Stuff

public text v1 · immutable
#253863 ·published 2006-11-21 05:04 UTC
rendered paste body
import java.util.Stack;
import java.lang.*;
public class PostfixConversion
{
        public static String convertToPostfix(String inputInfo)
        {
		  			 int p = 0;
                // Create a new, empty stack
                Stack parenthesis = new Stack();
					 Stack<Character> post  = new Stack<Character>();
					 boolean b = true;
					 for(int i = 0; i < inputInfo.length(); i++)
					 { 
						char c;
						c = inputInfo.charAt(i);
					 	if(c == '(')
						parenthesis.push(p);
						if(c == ')')
						{
							if(parenthesis.empty() == true)
							{
							System.out.print("There is no matching right parenthesis.\n");
							b = false;
							}
							else
							parenthesis.pop();
						}
					 }
					if(parenthesis.empty() == false && b == true)
					System.out.print("There is no matching left parenthesis.\n");
					String exp = new String();
					
					for(int i = 0; i < inputInfo.length(); i++)
					{
						if(inputInfo.charAt(i) != '+' && inputInfo.charAt(i) != '-' && inputInfo.charAt(i) != '*' && inputInfo.charAt(i) != '/' &&
						   inputInfo.charAt(i) != '(' && inputInfo.charAt(i) != ')')
								exp += inputInfo.charAt(i);
							
						if(inputInfo.charAt(i) == '+')
						{
							boolean a = true;
							if(post.isEmpty() == false)
							{	
									a = precedence('+', post.peek());
									while(a == true)
									{
										exp += post.pop();
										if(post.isEmpty() == false)
											a = precedence('+', post.peek());
										else
											a = false;
									}
									
							}
							post.push('+');
						}
						
						if(inputInfo.charAt(i) == '-')
						{
							boolean q = true;
							if(post.isEmpty() == false)
							{	
									q = precedence('-', post.peek());
									while(q == true)
									{
										exp += post.pop();
										if(post.isEmpty() == false)
											q = precedence('-', post.peek());
										else
											q = false;
									}
									
							}
							post.push('-');
						}
					
						if(inputInfo.charAt(i) == '*')
						{
							boolean c = true;
							if(post.isEmpty() == false)
							{	
									c = precedence('*', post.peek());
									while(c == true)
									{
										exp += post.pop();
										if(post.isEmpty() == false)
											c = precedence('*', post.peek());
											else
											c = false;
									}
					
							}
							post.push('*');
						}
						
						if(inputInfo.charAt(i) == '/')
						{
							boolean d =  true;
							if(post.isEmpty() == false)
							{	
									d = precedence('/', post.peek());
									while(d == true)
									{
										exp += post.pop();
										if(post.isEmpty() == false)
										d = precedence('/', post.peek());
										else
										d = false;
									}
									
								
							}
							post.push('/');
						}
						
						if(inputInfo.charAt(i) == ')')
						{
							boolean e = true;
							if(post.isEmpty() == false)
							{
								e = precedence(')', post.peek());
								while(e == true)
								{	exp += post.pop();
									if(post.isEmpty() == false)
										e = precedence(')', post.peek());
									else
										e = false;
								}
							}
						}
						
						if(inputInfo.charAt(i) == '(')
							post.push('(');
						
					}
					while(post.isEmpty() == false)
						{
							if(post.peek() == '(')
								post.pop();
							else
								exp += post.pop();
						}
					return exp;
					
        }
		  
		  public static boolean precedence(char a, char b)
		  {	int c = 0;
		  		int d = 0;
		  		if(a == '+' || a == '-')
					c = 0;
				else
					c = 1;
					
				if(b == '+' || b== '-')
					d = 0;
				else
					d = 1;
					
		  		if(a== '(' || b == '(')
				return false;
				
				if(c >= d)
				return true;
				else
				return false;
				
		  }

}