Polynomial Class

I wasn't given much information other than: develop a C++ class to describe and manipulate polynomials.

I tried but I messed up, and I have restarted several times but end up messing up more each time.

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Polynomial.h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <iostream>
using std::ostream;
using std::istream;




class Polynomial
{

  public:
    Polynomial();
    Polynomial( int dgr, const double* clist );
    Polynomial( const Polynomial& );
    ~Polynomial();

    const Polynomial& operator=( const Polynomial& clist);
    bool operator==( const Polynomial& clist) const;


	int setDegree(int dgr); 
    int getDegree() const;

    void printPolynomial( int dgr, const double* clist );

	
     double operator[]( int index ) const;
    double& operator[]( int index );

  private:
	  int    degree;
	  double *coefficients; // list of coefficients

};

ostream& operator<<( ostream& , const Polynomial& );
istream& operator>>( istream& , Polynomial& );


#endif


// Polynomial.cpp

#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::istream;
using std::ostream;
using std::cerr;
using std::endl;

#include <cstdlib>
using std::exit;

#include "Polynomial.h"


Polynomial::Polynomial()
{
	degree        = 0;
	coefficients  = 0;

}


Polynomial::Polynomial(int dgr, const double* clist)
{
	 getDegree();
	 
}

int Polynomial::getDegree() const
{
	return degree; 
}

Polynomial:: ~Polynomial()
{
	setDegree(0); 
}

int Polynomial::setDegree(int dgr)
{

	if( dgr <0 ) {
    cerr << "Error:  attempted to set a negative degree" << endl;
    exit(1);  
  } else {
    if( coefficients!=0 ) {
      delete [] coefficients;
      coefficients = 0;
      degree       = 0;
    }
    if( dgr !=0 ) {
      coefficients = new double [dgr];
      if( coefficients==0 ) {
        degree =0;
        cerr << "Warning:  unable to allocate enough space for list" << endl;
        exit(2); // this surely is reasonable.
      } else {
        degree=dgr;
      }
    }
  }
  return dgr;


}

//copy constructor 
Polynomial::Polynomial(const Polynomial& clist) : degree(0), coefficients(0)
{
  if( clist.getDegree()<=0 ) {
    setDegree(0);
  } else {
    setDegree(clist.getDegree());
    for (int i=0; i< degree ; i++) {
      coefficients[i]=clist.coefficients[i];
    }
  }
}


const Polynomial& Polynomial :: operator=(const Polynomial& clist) 
{
	if ( &clist==this ) 
	{
    cerr << "Warning:  attempt to copy Polynomial onto self" << endl;
    } 
	else {
    if( clist.getDegree()<=0 ) {
      setDegree(0);
    } else {
      setDegree(clist.getDegree());
      for (int i=0; i< degree ; i++) {
        coefficients[i] = clist.coefficients[i];
      }
    }
  }
  return *this;
}

bool  Polynomial::operator==(const Polynomial& clist) const
{
	bool result=true;
  if( degree!=clist.getDegree() ) {
    result=false;
  } else {
    for(int i=0; i< degree && result == true; i++) {
      result = coefficients[i]==clist.coefficients[i];
    }
  }
  return result;
}


double Polynomial::operator[]( int index ) const
{
  if( index<0 || index>= degree ) {
    cerr << "Attempt to access element outside index bounds" 
      << endl;
    exit(3); // Maybe not reasonable. 
  } else {
    return coefficients[index];
  }
}


double& Polynomial::operator[]( int index )
{
  if( index < 0 || index>= degree  ) {
    cerr << "Attempt to access element outside index bounds" 
      << endl;
    exit(4); // Maybe not reasonable but what the heck.
  } else {
    return coefficients[index];
  }
}






void printPolynomial( int dgr, const double* clist )
{   int beru;    

	for (int i = 0; i <= dgr ; i ++ ){
 


		
 
	cout << clist[i] << "X^(" << beru << ")" ;
	
		if (dgr != 0) {
		beru = dgr - 1;
		}
		else 
		{
		beru = dgr;  
		}
	

	if (beru > 0){
	
	cout << " + "; 
	} 

}

}

ostream& operator<<( ostream& left, const Polynomial& right)
{
  if( right.getDegree() > 0 ) {
    left << endl;
    for( int i=0; i< right.getDegree(); i++ ) {
      left << "[" << i << "] " << right[i] << endl;
    }
  } else {
    cerr << "Warning:  Attempt to display empty IntList" << endl;
  }
  return left;
}


istream& operator>>( istream& left, Polynomial& right)
{
  int tmp;
  left >> tmp;
  if( tmp<=0 ) {
    cerr << "First value expected to be list size, > 0." << endl;
  } else {
    if( tmp!=right.getDegree() ) {
      right.setDegree(tmp);
    }
    for (int i=0; i<right.getDegree(); i++) {
      left >> right[i];
    }
  }
  return left;
}

// Prog11.cpp

 
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

#include "Polynomial.h"
#include "Polynomial.cpp"

int main()
{
  Polynomial A;
  cout << "(1) Testing `cout << A':  " << A << endl;

  cout << "(2) Testing `cin >> A':\n";
  cout << "Enter the polynomial (integer order then double coefficients):\n\t ";
  cin >> A;
  cout << endl;
  cout << "(3) Second look at A:  " << A << endl;

  Polynomial B(A);
  cout << "(4) Testing `Polynomial B(A)':  " << B << endl;

  double clist[]={8, 4.5, 1};
  Polynomial C(2, clist);
  cout << "(5) Testing `Polynomial C(2, clist)':  " << C << endl;

  Polynomial D=C;
  cout << "(6) Testing D = C):  " << D << endl;

  cout << "(7) Testing A == B :  " << (A==B ? "TRUE" : "FALSE") << endl;
  cout << "(8) Testing A == D :  " << (A==D ? "TRUE" : "FALSE") << endl;  

  return 0;
}



Example of what the basic out put should look like:
(1) Testing `cout << A': empty
(2) Testing `cin >> A':
Enter the polynomial (integer degree then double coefficients):
3 -1 2.09 -5.3 -0.98
(3) Second look at A: -1x^(3) +2.09x^(2) -5.3x^(1) -0.98
(4) Testing `Polynomial B(A)': -1x^(3) +2.09x^(2) -5.3x^(1) -0.98
(5) Testing `Polynomial C(2, clist)': +1x^(2) +4.5x^(1) +8
(6) Testing D = C: +1x^(2) +4.5x^(1) +8
(7) Testing A == B : TRUE
(8) Testing A == D : FALSE
Topic archived. No new replies allowed.