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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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.