1 Replies - 241 Views - Last Post: 02 March 2013 - 10:56 AM Rate Topic: -----

#1 ninjachaser21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-February 13

Infix to postfix error

Posted 27 February 2013 - 10:59 PM

I've been working on this Homework for quite a while now trying to convert infix to postfix, but I don't know my code only works for something like "a+b;", not something like (a*(b+c)+d)+e;.

 public static String convertit(String infix)
		 {
			 String postfix = "";
		        Character char1;
		        char temp2;
		  
		    	LinkedStack<Character> infixstack = new LinkedStack<Character>(); 
		    	for (int index = 0; index < infix.length() ; index++)
		    	{   	
		    		char1 = infix.charAt(index);
		    	    char temp = char1;
		    	   if (Character.isLetter(char1))  
		    	   {
		    		  postfix += char1;    		   
		    	   }
		    	   else  // else1 
		    	   {  
		    		   
		    		  if(char1.equals('+')) //  can also use case switch statement... which actually is a better way to go
		    		  {
		    			  if(infixstack.isEmpty())
		    			  {
		    				  infixstack.push(char1);    				  
		    			  }
		    			  else
		    			  {
		    				 if(infixstack.peek().equals('*'))
		    			      {	 
		    				     while(infixstack.peek().equals('*'))
		    				       {
		    					  postfix+=infixstack.pop();
		    				       }
		    					  infixstack.push(char1);  // the new +
		    		          }
		    			 
		    			     else
		    			    {
		    				  if (infixstack.peek().equals('+'))
		    				  
		    				  {
		    					postfix+= infixstack.pop(); 
		    					infixstack.push(char1); 
		    				  }	 
		    				  else
		    					if (infixstack.peek().equals('('))
		    					{
		    						infixstack.push(char1); 
		    					}
		    					    
		    			   }
		    		  }	   
		    	   }
		    		  else
		    		  {
		    			  if(char1.equals('*'))
		    			  {
		    				 // put all the other code here for "*"....
		    			  }	
		    			  else
		    			  {
		    				  if(char1.equals('('))
		    				  {
		    					// code goes here  
		    				  }
		    				  else
		    				  {  
		    					  if(char1.equals(')'))
		    					  {
		    				    // code goes here  
		    					  }
		    					  else
		    					  {  
		    						 if (char1.equals(';'))
		    								 {
		    							     	while(!infixstack.isEmpty())
		    							     	{
		    							     		temp2=infixstack.pop();
		    							     		if (temp2!='(')
		                                          postfix+=temp2;    							     			
		    				
		    							     	}
		    							 
		    								 }
		    					  } 
		    				  }
		    			  }
		    		  }
		    	 
		    }   
		    	   
		    }   




Is This A Good Question/Topic? 0
  • +

Replies To: Infix to postfix error

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,863
  • Joined: 18-April 07

Re: Infix to postfix error

Posted 02 March 2013 - 10:56 AM

Well one thing I found the most useful for this project is to create some separate functions that do some simple things.

1) Create a simple function called isOperator and takes a character that you will check if is an operator like + - / * etc

2) Create a simple function called isOperand and takes a character that you will check is not an operator (call the first function) or a open/close parenthesis.

3) Create a function called compareOperators and will take two characters that you will then compare to see which one has higher precedence. For instance if operator1 is * or / it will have higher precedence than operator2 etc.

Now with these three functions you can boil down the main loop into a much easier to manage set of instructions. If you are wanting to see more to this you can check out my blog The Coders Lexicon which has an infix to postfix conversion in C++ (which you can usually get an idea of what is happening and convert it to Java) (hint: its pointer arithmetic can be done with charAt()).

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1