-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.cpp
244 lines (230 loc) · 5.05 KB
/
source.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
#include <iostream>
#include <cmath>
using namespace std;
class Polynomial {
protected:
class Term {
public:
int exponent;
int coefficient;
Term* next;
Term(int exp, int coeff, Term* n) {
exponent = exp;
coefficient = coeff;
next = n;
}
friend class Polynomial;
};
public:
Polynomial() {
term = new Term(0, 0, nullptr);
}
Polynomial(const Polynomial& p) {
Term* q;
Term* terms = nullptr;
bool flag = true;
for (q = p.term; q != nullptr; q = q->next) {
if (flag) {
flag = false;
terms = new Term(q->exponent, q->coefficient, nullptr);
term = terms;
}
else {
terms->exponent = q->exponent;
terms->coefficient = q->coefficient;
}
if (q->next != nullptr) {
terms->next = new Term(0, 0, nullptr);
terms = terms->next;
}
}
}
~Polynomial() {
while (true) {
if (term->next == nullptr)
break;
Term* p = term;
term = term->next;
delete p;
}
delete term;
}
Polynomial& operator=(const Polynomial& p) {
while (true) {
if (term->next == nullptr)
break;
Term* pterm = term;
term = term->next;
delete pterm;
}
delete[] term;
Term* q;
Term* terms;
bool flag = true;
for (q = p.term; q != nullptr; q = q->next) {
if (flag) {
flag = false;
terms = new Term(q->exponent, q->coefficient, nullptr);
term = terms;
}
else {
terms->exponent = q->exponent;
terms->coefficient = q->coefficient;
}
if (q->next != nullptr) {
terms->next = new Term(0, 0, nullptr);
terms = terms->next;
}
}
return *this;
}
void addTerm(int expon, int coeff) {
if (term->exponent == 0 && term->coefficient == 0) {
term->coefficient = coeff;
term->exponent = expon;
}
else {
Term* q = nullptr;
Term* p = term;
if (term->exponent < expon) {
q = new Term(expon, coeff, nullptr);
q->next = term;
term = q;
}
else if (term->exponent == expon) {
term->coefficient += coeff;
if (term->coefficient == 0) {
if (term->next == nullptr)
term->exponent = 0;
else {
term = term->next;
delete p;
}
}
}
else {
bool flag = false;
while (true) {
if (p->next == nullptr)
break;
q = p->next;
if (q->exponent == expon) {
flag = true;
break;
}
if (q->exponent < expon)
break;
p = p->next;
}
if (flag) {
q->coefficient += coeff;
if (q->coefficient == 0) {
p->next = q->next;
delete q;
}
}
else {
q = new Term(expon, coeff, nullptr);
q->next = p->next;
p->next = q;
}
}
}
}
double evaluate(double x) {
double sum = 0;
double value;
Term* p;
for (p = term; p != nullptr; p = p->next) {
value = pow(x, p->exponent);
sum += (p->coefficient * value);
}
return sum;
}
friend Polynomial operator+(const Polynomial& p, const Polynomial& q) {
Polynomial result;
Term* terms_p, * terms_q;
for (terms_p = p.term; terms_p != nullptr; terms_p = terms_p->next)
result.addTerm(terms_p->exponent, terms_p->coefficient);
for (terms_q = q.term; terms_q != nullptr; terms_q = terms_q->next)
result.addTerm(terms_q->exponent, terms_q->coefficient);
return result;
}
friend Polynomial operator*(const Polynomial& p, const Polynomial& q) {
Polynomial result;
Term* terms_p, * terms_q;
for (terms_p = p.term; terms_p != nullptr; terms_p = terms_p->next) {
for (terms_q = q.term; terms_q != nullptr; terms_q = terms_q->next) {
result.addTerm(terms_q->exponent + terms_p->exponent, terms_q->coefficient * terms_p->coefficient);
}
}
return result;
}
friend ostream& operator<<(ostream& out, const Polynomial& p) {
bool flag = true;
if (p.term->coefficient == 0)
out << "0";
else {
for (Term* q = p.term; q != nullptr; q = q->next) {
if (flag) {
if (q->coefficient < 0)
out << "- ";
}
else {
if (q->coefficient > 0)
out << "+ ";
else if (q->coefficient < 0)
out << "- ";
}
if (q->coefficient == 0)
continue;
else if (abs(q->coefficient) == 1) {
if (q->exponent == 0)
out << abs(q->coefficient);
}
else out << abs(q->coefficient);
if (q->exponent == 0)
continue;
else if (q->exponent == 1)
out << "x";
else
out << "x^" << q->exponent;
if (q->next != nullptr)
out << " ";
flag = false;
}
}
return out;
}
private:
Term* term;
};
int main()
{
Polynomial p;
cout << "Enter number of terms for the first polynomial P(x): " << '\n';
int nump;
cin >> nump;
cout << "Add terms: " << '\n';
for (int i = 1; i <= nump; i++) {
int a, b;
cin >> a >> b;
p.addTerm(a, b);
}
Polynomial q;
cout << "Enter number of terms for the second polynomial Q(x): " << '\n';
int numc;
cin >> numc;
cout << "Add terms: " << '\n';
for (int i = 0; i < numc; i++) {
int c, d;
cin >> c >> d;
q.addTerm(c, d);
}
cout << "P(x) = " << p << '\n';
cout << "P(1) = " << p.evaluate(1) << '\n';
cout << "Q(x) = " << q << '\n';
cout << "Q(1) = " << q.evaluate(1) << '\n';
cout << "(P+Q)(x) = " << p + q << '\n';
cout << "(P*Q)(x) = " << p * q << '\n';
}