-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.cpp
266 lines (221 loc) · 8.9 KB
/
Polynomial.cpp
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
/* -----------------------------------------------------------------------------
FILE NAME: Polynomial.cpp
DESCRIPTION: This is the implementation file for the polynomial class.
USAGE: This program is linkked to the driver via a .o file
COMPILER: g++ compiler c++11 standard
NOTES: Some cout statements are included here just to let the user know what exactly is going on
----------------------------------------------------------------------------- */
#include "Polynomial.h"
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial()
PURPOSE: default constructur
RETURNS: Nothing (void function)
NOTES: This is the default constructor that sets al the private members to 0
----------------------------------------------------------------------------- */
Polynomial::Polynomial()
{
degree = 0;
for(int i = 0; i < ARRAY_SIZE(coef); i++)
coef[i]= 0;
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: ~Polynomial()
PURPOSE: Destructor
RETURNS: Nothing (void function)
NOTES: This destroys the polynomial class. Because I use static memory there is nothing in this class
----------------------------------------------------------------------------- */
Polynomial::~Polynomial()
{
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial(const Polynomial &)
PURPOSE: copy constructor
RETURNS: Nothing (void function)
NOTES: This copy constructor makes a Polynomial class with the values from another class
----------------------------------------------------------------------------- */
Polynomial::Polynomial(const Polynomial& tpoly)
{
degree = tpoly.degree;
for(int i = 0; i < ARRAY_SIZE(coef); i++)
coef[i]= tpoly.coef[i];
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial(int kr, int coe[9])
PURPOSE: Overloaded (parameterized) constructor
RETURNS: Nothing (void function)
NOTES: This constructor takes two valules and initialized the cass with the values passed to it.
----------------------------------------------------------------------------- */
Polynomial::Polynomial(int kr, int coe[9])
{
degree = kr;
for (int i = 0; i < 9; i++)
coef[i] = coe[i];
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial operator +(const Polynomial &)
PURPOSE: This overloads the + operator to work with classes
RETURNS: a polynomial class p2
NOTES: This overloaded operator makes addition of two Polynomials with just a +
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator + (const Polynomial &p1)
{
Polynomial p2;
p2.degree = MAX(degree, p1.degree);
for (int i = degree; i >= 0; i--)
{ p2.coef[i] = coef[i] + p1.coef[i]; }
return p2;
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial operator -(const Polynomial &)
PURPOSE: This overloads the - operator to work with classes
RETURNS: p2
NOTES: This overloaded operator makes subtractions of two polynomials possible with just a -
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator - (const Polynomial &p1)
{
Polynomial p2;
p2.degree = MAX(degree, p1.degree);
for (int i = degree; i >= 0; i--)
{ p2.coef[i] = coef[i] - p1.coef[i]; }
return p2;
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial operator =(const Polynomial &)
PURPOSE: This overloads the = operator to work with classes
RETURNS: *this
NOTES: This overloaded operator sets one polynomial equal to another one, similar to the copy constructor. It returns *this which is the polynomial
----------------------------------------------------------------------------- */
Polynomial& Polynomial::operator = (const Polynomial &p1)
{
degree = p1.degree;
for (int i = p1.degree; i >= 0; i--)
{ coef[i] = p1.coef[i]; }
return *this;
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: friend ostream& operator << (ostream &strm, const Polynomial &p)
PURPOSE: Streamline output of class
RETURNS: strm with would be the stream of data to output
NOTES: This makes output of polynomials classes possible with regular << outstream ouperator
----------------------------------------------------------------------------- */
ostream& operator << (ostream &strm, const Polynomial &p)
{
for (int i = p.degree; i>= 0; i--)
{
if (i < p.degree)
{
if (p.coef[i] >= 0) strm << " + ";
else strm << " ";
}
strm << p.coef[i];
if (i > 1) strm << "x" << i;
if (i==1) strm <<"x";
}
return strm;
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: friend ostream& operator << (ostream &strm, const Polynomial &p)
PURPOSE: Streamline input of
RETURNS: istrm with would be the stream of input data
NOTES: This makes input of polynomials classes possible with regular >> outstream ouperator
----------------------------------------------------------------------------- */
istream& operator >> (istream &istrm, Polynomial &p)
{
cout << "\nDegree of polynomial: ";
istrm >> p.degree;
cout << "\n Enter " << p.degree+1 << " coefficients" << endl;
for (int i = p.degree; i>= 0; i--){
istrm >> p.coef[i];}
return istrm;
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial operator ==(const Polynomial &)
PURPOSE: This fuctions test whether two Polynomials are equivalent
RETURNS: Nothing
NOTES: Just set two polynomials to the test with ==
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator ==(const Polynomial &p9)
{
bool test = false;
if (degree == p9.degree)
{
for (int i = p9.degree; i>= 0; i--)
{
if (coef[i] == p9.coef[i])
{
test = true;
break;
}
else {test = false; break;}
}
}
cout << "\nTrue indicates equal, false indicates inequal: " << boolalpha << test;
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial operator ()(const float &)
PURPOSE: This function evaluates the value of a function at a certain value x
RETURNS: Nothing
NOTES: This mimics true mathematical evaluation, usage is polynomial_name(x)
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator ()(const float &tr)
{
cout << "\nwith x = " << tr;
int value = 0;
float var = tr;
for (int i = degree; i >= 0; i--)
{
value = value + ((pow(var, i)) * coef[i]);
}
cout << "\nf(x) with x = " << var << " comes to " << value;
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial operator *(const Polynomial &)
PURPOSE: Overload * operator
RETURNS: A polynomial p5 that is a product of two polynomials
NOTES: PThis overload operator makes the multiplication of polynomials simple with just a *
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator *(const Polynomial &p7)
{
Polynomial p5;
p5.degree = degree + p7.degree;
for (int i = degree; i >= 0; i--)
{
for (int j = p7.degree; j >= 0; j--)
{
p5.coef[i+j] += coef[i] * p7.coef[j];
}
}
return p5;
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial operator --(int k)
PURPOSE: Overload -- operator
RETURNS: A polynomial p3 that is the derivative of a polynomial
NOTES: This operator overload is accessed with -- i.e. p5 = p3--
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator --(int k)
{
Polynomial p3;
p3.degree = (degree - 1);
for (int i = degree; i>= 0; i--)
{
p3.coef[i] = (coef[i+1] * (i+1));
}
return p3;
}
/* -----------------------------------------------------------------------------
FUNCTION NAME: Polynomial operator ++(int j)
PURPOSE: Overload ++ operator
RETURNS: A polynomial p1 that is the integral of a polynomial
NOTES: This operator overload is accessed with ++ i.e. p0 = p1 ++
----------------------------------------------------------------------------- */
Polynomial Polynomial::operator ++(int j)
{
Polynomial p1;
p1.degree = (degree + 1);
for (int i = degree; i>= 0; i--)
{
p1.coef[i+1] = (coef[i] / (i+1));
}
return p1;
}