-
Notifications
You must be signed in to change notification settings - Fork 7
/
32_Polynomial.cpp
133 lines (112 loc) · 3.39 KB
/
32_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
#include <iostream>
#include<math.h>
using namespace std;
class Polynomial {
public:
int degree;
double* coeff;
Polynomial(int degree, double* coeff);
Polynomial(const Polynomial& p);
~Polynomial();
Polynomial operator+(const Polynomial& p);
Polynomial operator-(const Polynomial& p);
Polynomial operator*(const Polynomial& p);
double evaluate(double x);
};
Polynomial::Polynomial(int degree, double* coeff) {
this->degree = degree;
this->coeff = new double[degree + 1];
for (int i = 0; i <= degree; i++) {
this->coeff[i] = coeff[i];
}
}
Polynomial::Polynomial(const Polynomial& p) {
degree = p.degree;
coeff = new double[degree + 1];
for (int i = 0; i <= degree; i++) {
coeff[i] = p.coeff[i];
}
}
Polynomial::~Polynomial() {
delete[] coeff;
}
Polynomial Polynomial::operator+(const Polynomial& p) {
int maxDegree = max(degree, p.degree);
double* resultCoeff = new double[maxDegree + 1];
for (int i = 0; i <= maxDegree; i++) {
double coeff1 = (i <= degree) ? coeff[i] : 0;
double coeff2 = (i <= p.degree) ? p.coeff[i] : 0;
resultCoeff[i] = coeff1 + coeff2;
}
Polynomial result(maxDegree, resultCoeff);
delete[] resultCoeff;
return result;
}
Polynomial Polynomial::operator-(const Polynomial& p) {
int maxDegree = max(degree, p.degree);
double* resultCoeff = new double[maxDegree + 1];
for (int i = 0; i <= maxDegree; i++) {
double coeff1 = (i <= degree) ? coeff[i] : 0;
double coeff2 = (i <= p.degree) ? p.coeff[i] : 0;
resultCoeff[i] = coeff1 - coeff2;
}
Polynomial result(maxDegree, resultCoeff);
delete[] resultCoeff;
return result;
}
Polynomial Polynomial::operator*(const Polynomial& p) {
int resultDegree = degree + p.degree;
double* resultCoeff = new double[resultDegree + 1];
for (int i = 0; i <= resultDegree; i++) {
resultCoeff[i] = 0;
for (int j = 0; j <= degree; j++) {
if (i - j >= 0 && i - j <= p.degree) {
resultCoeff[i] += coeff[j] * p.coeff[i - j];
}
}
}
Polynomial result(resultDegree, resultCoeff);
delete[] resultCoeff;
return result;
}
double Polynomial::evaluate(double x) {
double result = 0;
for (int i = 0; i <= degree; i++) {
result += coeff[i] * pow(x, i);
}
return result;
}
int main() {
// Example usage
double coeff1[] = {1, 2, 3}; // 1 + 2x + 3x^2
double coeff2[] = {2, 3}; // 2 + 3x
Polynomial poly1(2, coeff1);
Polynomial poly2(1, coeff2);
Polynomial sum = poly1 + poly2;
Polynomial diff = poly1 - poly2;
Polynomial product = poly1 * poly2;
cout << "Sum: ";
// Output sum
for (int i = 0; i <= sum.degree; i++) {
cout << sum.coeff[i] << "x^" << i;
if (i != sum.degree) cout << " + ";
}
cout << endl;
cout << "Difference: ";
// Output difference
for (int i = 0; i <= diff.degree; i++) {
cout << diff.coeff[i] << "x^" << i;
if (i != diff.degree) cout << " + ";
}
cout << endl;
cout << "Product: ";
// Output product
for (int i = 0; i <= product.degree; i++) {
cout << product.coeff[i] << "x^" << i;
if (i != product.degree) cout << " + ";
}
cout << endl;
// Evaluation example
cout << "Evaluation of poly1 at x=2: " << poly2.evaluate(0) << endl;
return 0;
}