-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.cpp
More file actions
422 lines (295 loc) · 13.8 KB
/
Polynomial.cpp
File metadata and controls
422 lines (295 loc) · 13.8 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* -----------------------------------------------------------------------------
FILE: Polynomial.cpp
DESCRIPTION: This is the implemetation file for the classes objects
COMPILER: Linux g++ compiler
NOTES: Put other information here ...
MODIFICATION HISTORY:
Author Date Version
--------------- ---------- --------------
Shawn Ray 2017-03-12 Version 1.0 started Project
Shawn Ray 2017-03-19 Version 1.1
Shawn Ray 2017-03-20 Version 1.2
Shawn Ray 2017-03-21 Version 1.3 constructor
Shawn Ray 2017-03-22 Version 1.4 overloaded + - *
Shawn Ray 2017-03-23 Version 1.5 overloaded << >>
Shawn Ray 2017-03-24 Version 1.6 added vectors
Shawn Ray 2017-03-25 Version 1.7 changed overloading
Shawn Ray 2017-03-26 Version 1.8
Shawn Ray 2017-03-30 Version 1.9 vectors
Shawn Ray 2017-03-31 Version 2.0
Shawn Ray 2017-04-01 Version 2.1 overloaded ==
Shawn Ray 2017-04-02 Version 2.2 overloaded ()
Shawn Ray 2017-04-03 Version 2.3 constructors
Shawn Ray 2017-04-04 Version 2.4 Dynamic/ integral
Shawn Ray 2017-04-05 Version 2.5 bug fixes
Shawn Ray 2017-04-06 Version 2.6 bug fixes
Shawn Ray 2017-04-07 Version 2.7 bug fixes
----------------------------------------------------------------------------- */
#include "Polynomial.h"
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial::Polynomial()
DESCRIPTION: This is the default constructor
RETURNS: Nothing (Void Function)
----------------------------------------------------------------------------- */
Polynomial::Polynomial(){
degree = 0;
}
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial::Polynomial(const int &size)
DESCRIPTION: Overloaded Constructor
RETURNS: Nothing (Void Function)
NOTES: constructs polynomials for dynamic allocation
----------------------------------------------------------------------------- */
Polynomial::Polynomial(const int &size){
coef = new double [size + 1];
degree = size;
for(int i = 0; i < size + 1; i++){
coef[i] = 0;
}
}
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial::Polynomial(const Polynomial &p1) : Polynomial::Polynomial(p1.degree)
DESCRIPTION: Copy contructor
RETURNS: Nothing (Void Function)
NOTES: Copies one poly to another, uses function inheretance
----------------------------------------------------------------------------- */
Polynomial::Polynomial(const Polynomial &p1) : Polynomial::Polynomial(p1.degree){
degree = p1.degree;
for(int i = p1.degree; i >= 0; --i)
{
coef[i] = p1.coef[i];
}
}
/* -----------------------------------------------------------------------------
FUNCTION: std::istream& operator >> (std::istream& instream, Polynomial &p1)
DESCRIPTION: this function takes input from the user
RETURNS: returns an istream operator (std::cin)
----------------------------------------------------------------------------- */
std::istream& operator >> (std::istream& instream, Polynomial &p1){
bool test = true;
do { // input validation
std::cout << "\n\tEnter degree of polynomial: ";
instream >> p1.degree;
if(instream.fail()){
instream.clear();
instream.ignore(256, '\n');
} else test = false; // test degree
p1.coef = new double [p1.degree + 1];
std::cout << "\tPlease enter the coefficients of the polynomial: ";
for (int i = p1.degree; i >= 0; --i) {
instream >> p1.coef[i];
if(instream.fail()){
instream.clear();
instream.ignore(256, '\n');
} else test = false; // test coef
}
std::cout << "\tPlease specify x values to evaluate polynomials at: ";
instream >> p1.x;
if(instream.fail()){
instream.clear();
instream.ignore(256, '\n');
} else test = false; // test x values
std::cout << "\tPlease specify bottom limit for definite integration: ";
instream >> p1.bottom;
if(instream.fail()){
instream.clear();
instream.ignore(256, '\n');
} else test = false; // test x values
std::cout << "\tPlease specify top limit for definite integration: ";
instream >> p1.top;
if(instream.fail()){
instream.clear();
instream.ignore(256, '\n');
} else test = false; // test x values
if(test) std::cout << "\n\tYou have entered an invalid selection, please try again.\n"; // print an invalid mes
}while(test);
return instream;
}
/* -----------------------------------------------------------------------------
FUNCTION: std::ostream& operator << (std::ostream& outstream, const Polynomial &p1)
DESCRIPTION: This function overloaded the ostream operator
RETURNS: returns ostream type (std::cout)
NOTES: tests for integral
----------------------------------------------------------------------------- */
std::ostream& operator << (std::ostream& outstream, const Polynomial &p1){
for(int i = p1.degree; i >= 0; i--){
if(i < p1.degree)
{
if(p1.coef[i] >= 0) outstream << " + ";
else outstream << " ";
}
if((p1.trigger_integral) && (i == 0)){
outstream << (char)p1.coef[0];
}else outstream << p1.coef[i];
if(i > 1)
outstream << "x^" << i;
else if (i == 1)
outstream << "x";
}
outstream << std::endl;
return outstream;
}
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial Polynomial::operator + (const Polynomial &p1) const
DESCRIPTION: Overloaded + operator
RETURNS: returns the polynymial p2
NOTES: declared const so no modification to this
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator + (const Polynomial &p1) const{
std::cout << "\tAdding the 2 Polynomials : ";
int size = MAX(degree, p1.degree);
Polynomial p2(size);
Polynomial Temp1(size);
Polynomial Temp2(size);
// temp polynomial class objects to account for varying sizes of objects
for(int y = degree; y >= 0; --y) Temp1.coef[y] = coef[y];
for(int w = p1.degree; w >= 0; --w) Temp2.coef[w] = p1.coef[w];
for(int i = p2.degree; i >= 0; i--){
p2.coef[i] = Temp1.coef[i] + Temp2.coef[i];
}
return p2;
}
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial Polynomial::operator - (const Polynomial &p1) const
DESCRIPTION: This function overloads the - operator
RETURNS: returns the p2 polynomial
NOTES: declared const so no modification to this
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator - (const Polynomial &p1) const{
std::cout << "\tSubtracting the 2 Polynomials : ";
int size = MAX(degree, p1.degree);
Polynomial p2(size);
Polynomial Temp1(size);
Polynomial Temp2(size);
// temp polynomial class objects to account for varying sizes of objects
for(int y = degree; y >= 0; --y) Temp1.coef[y] = coef[y];
for(int w = p1.degree; w >= 0; --w) Temp2.coef[w] = p1.coef[w];
for(int i = p2.degree; i >= 0; i--){
p2.coef[i] = Temp1.coef[i] - Temp2.coef[i];
}
return p2;
}
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial Polynomial::operator * (const Polynomial &p1) const
DESCRIPTION: This overloades the * operator
RETURNS: returns the polynomial p2
NOTES: declared const so no modification to this
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator * (const Polynomial &p1) const{
int size = degree + p1.degree;
Polynomial p2(size);
std::cout << "\tMultiplying the 2 Polynomials : ";
for(int i = degree; i >= 0; --i){
for(int j = p1.degree; j >= 0; --j){
p2.coef[i + j] += (coef[i] * p1.coef[j]);
}
}
return p2;
}
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial Polynomial::operator = (const Polynomial &p1)
DESCRIPTION: This function overloaded the = operator
RETURNS: returns the polynomial p2
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator = (const Polynomial &p1){
std::cout << "\n\tAssigning polynomial two to one : \n";
int size = MAX(degree, p1.degree);
Polynomial Temp1(size);
Polynomial Temp2(size);
// temp polynomial class objects to account for varying sizes of objects
for(int y = degree; y >= 0; --y) Temp1.coef[y] = coef[y];
for(int y = p1.degree; y >= 0; --y) Temp2.coef[y] = p1.coef[y];
delete coef;
coef = new double[size + 1];
degree = size;
for(int i = size; i >= 0; i--) {
coef[i] = Temp2.coef[i];
}
return *this;
}
/* -----------------------------------------------------------------------------
FUNCTION: bool Polynomial::operator == (const Polynomial &p1) const
DESCRIPTION: this function tests if two poly's are equal
RETURNS: returns a boolean for testing equality
NOTES: declared const, bool type for testing
----------------------------------------------------------------------------- */
bool Polynomial::operator == (const Polynomial &p1) const{
std::cout << "\tTesting if the Polynomials are equal : ";
if(degree == p1.degree){
for(int i = p1.degree; i >= 0; --i) {
if(coef[i] == p1.coef[i]){
}else return false;
}
return true;
}else return false;
}
/* -----------------------------------------------------------------------------
FUNCTION: void Polynomial::operator () (const int &x) const
DESCRIPTION: This function overloaded the () operator
RETURNS: Nothing (Void Function)
NOTES: declared const so not modification to this
----------------------------------------------------------------------------- */
double Polynomial::operator () (const double &x) const{
double sum = 0.0;
for(int i = degree; i >= 0; --i){
sum += (coef[i] * ((pow(x,i))));
}
return sum;
}
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial Polynomial::operator -- () const
DESCRIPTION: This function overloads the -- operator
RETURNS: returns the polynomial p2
NOTES: declared const so not modification to this
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator -- () const{
int size = degree - 1;
Polynomial p2(size);
std::cout << "\tEvaluating the Derivative : ";
for(int i = degree; i >= 0; i--) {
if (i) p2.coef[i - 1] = coef[i] * i;
}
return p2;
}
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial Polynomial::operator ++ () const
DESCRIPTION: This function overloads the ++ operator
RETURNS: returns the polynomial p2
NOTES: declared const so not modification to this
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator ++ () const{
int size = degree + 1;
Polynomial p2(size);
for(int i = size; i >= 0; --i) {
if(i > 0){
p2.coef[i] = ((coef[i - 1]) / i);
}
else{
p2.coef[0] = 67;
}
}
p2.trigger_integral = true;
return p2;
}
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial Polynomial::operator ++ (int) const
DESCRIPTION: This function overloads the ++ operator for definite integration
RETURNS: This function returns the polynomial p2
NOTES: Put other information here ...
----------------------------------------------------------------------------- */
double Polynomial::operator ++ (int) const{ // definite
Polynomial p2(++(*this));
double result = p2(top) - p2(bottom) ;
return result;
}
/* -----------------------------------------------------------------------------
FUNCTION: Polynomial::~Polynomial(){
DESCRIPTION: This is the destructor
RETURNS: Nothing (Void Function)
NOTES: destructs my dynamic allocated memory
----------------------------------------------------------------------------- */
Polynomial::~Polynomial(){
delete coef;
delete [] coef;
coef = nullptr;
}