Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Infix to postfix error

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



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>