You are using a version without Ads of this website. Please, consider donating:

Polynomial Long Division

This is my polynomial class along with the consttructor and destructor. I'm having problems making the algorith for Division. My operator +,- and * are working. I still need operator / and %.

Can anyone help me with this?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````class Polynomial { public: Polynomial (); Polynomial (double Coeff [], int inputDegree);//constructor Polynomial (Polynomial&);// copy constructor int setDegree(int deg); int setCoeff(int size); void output(void); ~Polynomial(); Polynomial& operator = (const Polynomial&); Polynomial operator +(const Polynomial&); Polynomial operator -(const Polynomial&); Polynomial operator *(const Polynomial&); Polynomial operator /(const Polynomial&); Polynomial operator %(const Polynomial&); void divide(const Polynomial&,Polynomial&,Polynomial&); friend ostream& operator <<(ostream &, Polynomial &); private: double *pointerCoeff;// array where coefficients are stored int Degree;// the degree of the polynomial is one less than the size of the array of coefficients }; Polynomial::Polynomial() //This is the default constructor { Degree = 0; pointerCoeff = new double[Degree + 1]; pointerCoeff[0] = 0; } Polynomial::Polynomial( double Coeff[], int inputDegree)// this is the main contstructor { Degree =inputDegree; pointerCoeff= new double[ Degree +1 ];// allocate an array to hold the coefficient values for(int i=0; i<(Degree + 1); i++) pointerCoeff[i] = Coeff[i]; } Polynomial:: Polynomial (Polynomial& copy)//This is the copy constructor { Degree=copy.Degree; copy.pointerCoeff = new double [copy.Degree + 1]; for(int i=0; i<(Degree+1); i++) copy.pointerCoeff[i]; } Polynomial::~Polynomial()//destructor { if( pointerCoeff ) { delete [] pointerCoeff; pointerCoeff= NULL; } void Polynomial::divide(const Polynomial &Divisor, Polynomial &q, Polynomial &r) { Polynomial trial; q.pointerCoeff=new double [this->Degree-Divisor.Degree+2]; int i=this->Degree-Divisor.Degree; double lead= Divisor.pointerCoeff[Divisor.Degree]; q.Degree=this->Degree-Divisor.Degree; q.pointerCoeff [q.Degree]= this->pointerCoeff [this->Degree]/lead; for(int i = q.Degree-1; i >= 0; i--) { q.pointerCoeff[i] = r.pointerCoeff[r.Degree]/lead; trial = Divisor; trial*q; r.Degree--; for(int j=0; j< trial.Degree; j++) { r.pointerCoeff[i+j] -= trial.pointerCoeff[j]; } } } }``````

Last edited on
Topic archived. No new replies allowed.