Polynomial class c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bitbyte89
    New Member
    • Nov 2008
    • 26

    Polynomial class c++

    im making a polynomial class, operator / is working while the operator /= overloaded function is not working.Someone pleasssssssssss ssseeeee help out. ive to submit it next week.

    Code:
    	int degree;
    	double ab[100];
    	char dummy;
    
    /////////////////////////////Division////////
    
       Poly  Poly::operator/(Poly& a)
       {
    	   Poly temp;
    	   double result[20],temp1[20],temp2[20],temp3[20];
    	   int count=0,cnt,i=0,j=0,k=0;
    
    	   for(i=0 ; i<=20 ; i++)
    	   {
    		   result[i]=0;temp1[i]=0;temp2[i]=0;temp3[i]=0;
    
    	   }
    
    	   if(degree>=a.degree)
    	   {
    		   for(i=degree; i>=0 ; i--)
    		   temp1[i]=ab[i]; 
    		   for(i=a.degree-1 ; i>=0 ;i--)
    		   temp2[i]=a.ab[i]*-1; 
    
    		   
    		   for(i=degree ; i>=a.degree ; i--)
    		   {
    			   cnt=0;
    			  
    			 result[count]=temp1[i];
    
    			   for(j=a.degree-1 ; j>=0 ; j--)
    			   {
    				   temp3[j]=temp2[j]*temp1[i];
    			   }
    
    			   for(k=i-1 ; k>=0 ; k--)
    			   {
    				   if(a.degree-1-cnt>=0)
    				   {
    					   temp1[k]+=temp3[a.degree-1-cnt];
    					   cnt++;
    				   }
    				   
    			   }
    
    			   count++;
    			  
    		   }
    		     count-=1;
    
    		   for(i=count ; i>=0 ; i--)
    			   temp.ab[count-i]=result[i];
    		   temp.degree=count;
    		   temp.dummy=dummy;
    	   }
    
    	   else
    		   cout<<" invalid input "<<"\n";
    
    	   return temp;
       }
    
    
    //////////not working
    
         Poly&  Poly::operator/=(Poly& a)
       {
    	   double result[20],temp1[20],temp2[20],temp3[20];
    	   int count=0,cnt,i=0,j=0,k=0;
    
    	   for(i=0 ; i<=20 ; i++)
    	   {
    		   result[i]=0;temp1[i]=0;temp2[i]=0;temp3[i]=0;
    
    	   }
    	   
    
    	   if(degree>=a.degree)
    	   {
    		    
    		   for(i=degree; i>=0 ; i--)
    		   temp1[i]=ab[i]; 
    		   for(i=a.degree-1 ; i>=0 ;i--)
    		   temp2[i]=a.ab[i]*-1; 
    
    		   
    		   for(i=degree ; i>=a.degree ; i--)
    		   {
    			   cnt=0;
    			  
    			 result[count]=temp1[i];
    
    			   for(j=a.degree-1 ; j>=0 ; j--)
    			   {
    				   temp3[j]=temp2[j]*temp1[i];
    			   }
    
    			   for(k=i-1 ; k>=0 ; k--)
    			   {
    				   if(a.degree-1-cnt>=0)
    				   {
    					   temp1[k]+=temp3[a.degree-1-cnt];
    					   cnt++;
    				   }
    				   
    			   }
    
    			   count++;
    			  
    		   } count--;
    
    			  this->degree=count;
    
    		   for(i=degree ; i>=0 ; i--)
    		    this->ab[degree-i]=result[i];
    
    		   cout<<"done";
    		    
    		  
    	   }
    
    	   else
    		   cout<<" invalid input "<<"\n";
    
    	   return *this;
      }
    Last edited by Banfa; Nov 16 '08, 10:37 AM. Reason: Added [Code]...[/code] tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    If your Poly::operator/() is working then why not just:
    Code:
    Poly& Poly::operator/=(Poly& a)
    {
         *this = *this / a;
         return *this;
    }
    ?

    Comment

    • bitbyte89
      New Member
      • Nov 2008
      • 26

      #3
      Originally posted by weaknessforcats
      If your Poly::operator/() is working then why not just:
      Code:
      Poly& Poly::operator/=(Poly& a)
      {
           *this = *this / a;
           return *this;
      }
      ?


      u r genius.......oo oohhhh how stupid of me.....its working now....thanks a lot
      a lot....im so happy......than ksss
      but still why that function /= isn't working ??? i jst copy paste that / code and change the assignment of result to temporary object to this pointer...

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Perhaps in your /= operator you are using this as if it was the original polynomial after you have modified it.

        Comment

        • bitbyte89
          New Member
          • Nov 2008
          • 26

          #5
          Originally posted by boxfish
          Perhaps in your /= operator you are using this as if it was the original polynomial after you have modified it.

          but ive used this pointer when all the calculations are done....at the end before ending if ive changed the the original attributes it should work

          Comment

          Working...