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
| #pragma hdrstop
#pragma argsused
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
class Complex
{
int *n;
double *koef;
void ShowTermOfComplex(double mas[],int i,int pr);
void ShowTermOfComplex1(double mas[],int i, int pr, ostream& mystream);
public:
Complex();
Complex(int k);
Complex(int k, double mas[]);
Complex(const Complex&ob);
~Complex(){delete n; delete[]koef;}
void GiveMemory(int k);
void SetComplex();
void SetComplex(int k, double mas[]);
void SetDegree(int t){*n=t;};
void ClearComplex();
void ShowCoefficients();
void ShowDegree();
void ShowComplex();
void OnlyShowComplex();
void CalculateValue();
int GetDegree(){return *n;};
double GetOneCoefficient(int i){return(koef[i]);};
Complex operator+(Complex ob);
Complex operator-(Complex ob);
Complex operator*(Complex ob);
/*Complex operator/(Complex ob); */
double& operator[](int i){return(koef[i]);}
Complex& operator=(const Complex p){
if(&p==this) return *this;
if(koef) delete [] koef;
*n=*p.n;
koef=new double [*p.n+1];
for(int i=0; i<=*p.n; i++)
koef[i]=p.koef[i];
return *this;
}
friend ostream& operator<<(ostream& mystream, Complex &ob);
friend istream& operator>>(istream& mystream, Complex &ob);
int min(int i, int m);
int max(int n, int m);
};
void Complex::ShowTermOfComplex(double mas[], int i, int pr)
{
if(pr==0){
if(mas[i]==0) return;
else cout<<mas[i];
if(i==0) return;
else cout<<"x";
if(i==1) return;
else cout<<"^"<<i;
}
else {
if(mas[i]==0) return;
else{
if(mas[i]>0)cout<<"+";
cout<<mas[i];
}
if(i==0) return;
else cout<<"x";
if(i==1) return;
else cout<<"^"<<i;
}
}
void Complex::ShowTermOfComplex1(double mas[], int i, int pr, ostream& mystream)
{
if(pr==0) {
if(mas[i]==0) return;
else{mystream<<"^"<<0; mystream<<"+";
mystream<<mas[i]; }
if(i==0) return;
else mystream<<"x";
if(i==1) return;
else cout<<"^"<<i;
}
}
Complex::Complex()
{
int i;
randomize();
n=new int;
if(!n){cout<<"Oshibka vbIdeleniq pamqti!!!"; getch(); return;}
koef=new double[*n+1];
if(!koef){ cout<<"Oshibka vbIdeleniq pamqti!!!"; getch(); return;}
for(i=*n; i>=0; i--)
koef[i]=random(10)-5;
}
Complex::Complex(int k)
{
int i;
randomize();
n=new int;
if(!n){cout<<"Oshibka vbIdeleniq pamqti!!!"; getch(); return;}
*n=k;
koef=new double[*n+1];
if(!koef){ cout<<"Oshibka vbIdeleniq pamqti!!!"; getch(); return;}
for(i=*n; i>=0; i--)
koef[i]=random(10)-5;
}
Complex::Complex(int k, double mas[])
{
int i;
n=new int;
if(!n){cout<<"Oshibka vbIdeleniq pamqti!!!"; getch(); return;}
*n=k;
koef=new double[*n+1];
if(!koef){ cout<<"Oshibka vbIdeleniq pamqti!!!"; getch(); return;}
for(i=*n; i>=0; i--)
koef[i]=mas[i];
}
Complex::Complex(const Complex&ob)
{
int i;
n=new int;
if(!n){cout<<"Oshibka vbIdeleniq pamqti!!!"; getch(); return;}
*n=*ob.n;
koef=new double[*n+1];
if(!koef){ cout<<"Oshibka vbIdeleniq pamqti!!!"; getch(); return;}
for(i=0; i<=*n; i--)
koef[i]=ob.koef[i];
}
void Complex::GiveMemory(int k)
{
if(koef) delete [] koef;
koef=new double[k+1];
if(!koef){cout<<"Oshibka vbIdeleniq pamqti!!!"; getch(); return;}
}
void Complex::SetComplex()
{
if(n) delete n;
n=new int;
int i;
cout<<"Vvedite stepen mnogochlena: ";
cin>>*n;
cout<<endl;
if(koef) delete [] koef;
koef=new double[*n+1];
for(i=*n; i>=0; i--)
{
cout<<"Vvedite koeficent pri stepeni" <<i<<":";
cin>>koef[i];
}
}
void Complex::SetComplex(int k, double mas[])
{
int i;
if(n) delete n;
n=new int;
*n=k;
if(koef) delete [] koef;
koef=new double [*n+1];
for(i=*n; i>=0; i--)
koef[i]=mas[i];
}
void Complex::ClearComplex()
{
int i;
for(i=0; i<=*n; i++)
koef[i]=0;
*n=0;
}
void Complex::ShowCoefficients()
{
int i;
for(i=*n; i>=0; i--)
{
cout<<"Koefficent pri stepeni"<<i;
cout<<"raven;";
cout<<koef[i]<<endl;
}
while(!kbhit());
}
void Complex::ShowDegree()
{
cout<<"stepen mnogochlena ="<<*n;
while(!kbhit());
}
void Complex::ShowComplex()
{
int i,pr=0;
cout << "Nash mnogochlen:" << endl;
cout << "f(x)=";
for( i = *n; i >= 0; i--)
{
ShowTermOfComplex(koef,i,pr);
if(koef[i]!=0)pr=1;
}
if (pr==0) cout << 0;
cout << endl;
while(!kbhit());
}
void Complex::OnlyShowComplex()
{
int i,pr=0;
for(i=*n;i>=0;i--)
{
ShowTermOfComplex(koef,i,pr);
if(koef[i]!=0)pr=1;
}
if(pr==0)cout<<0;
cout << endl;
}
void Complex::CalculateValue()
{
double s,x;
int i;
cout << "Mnogochlen f(x)=";
OnlyShowComplex();
cout << "Vvedite znachenie x:";
cin >> x;
cout << "f(" << x << ") =";
for(s=koef[0],i=1;i<=*n;i++)
s=s+koef[i]*pow(x,i);
cout << s;
while(!kbhit());
}
Complex Complex::operator+(Complex ob)
{
int i;
Complex rab;
rab.GiveMemory(max(*n,ob.GetDegree()));
for(i=0; i<=min(*n,ob.GetDegree()); i++)
rab.koef[i]=koef[i]+ob.GetOneCoefficient(i);
if(*n<ob.GetDegree())
{
for(i=min(*n,ob.GetDegree())+1; i<=*n; i++)
rab.koef[i]=ob.GetOneCoefficient(i);
*rab.n=ob.GetDegree();
}
else
{
for(i=min(*n,ob.GetDegree())+1; i<=*n; i++)
rab.koef[i]=koef[i];
*rab.n=*n;
}
return rab;
}
Complex Complex::operator-(Complex ob)
{
int i;
Complex rab;
rab.GiveMemory(max(*n,ob.GetDegree()));
for(i=0; i<=min(*n,ob.GetDegree()); i++)
rab.koef[i]=koef[i]-ob.GetOneCoefficient(i);
if(*n<ob.GetDegree())
{
for(i=min(*n,ob.GetDegree())+1; i<=ob.GetDegree(); i++)
rab.koef[i]=-ob.GetOneCoefficient(i);
*rab.n=ob.GetDegree();
}
else
{
for(i=min(*n,ob.GetDegree())+1; i<=*n; i++)
rab.koef[i]=koef[i];
*rab.n=*n;
}
return rab;
}
Complex Complex::operator*(Complex ob)
{
int i,j,k;
double s;
Complex rab;
rab.GiveMemory(*n+ob.GetDegree());
for(i=0;i<=*n+ob.GetDegree();i++)
{ s=0;
for(j=0;j<=*n;j++)
for(k=0;k<=ob.GetDegree();k++)
if(j+k==i)s=s+koef[j]*ob.GetOneCoefficient(k);
rab.koef[i]=s;
}
rab.n=n+ob.GetDegree();
return rab;
}
/*
Complex Complex::operator/(Complex ob)
{
int i,j,k;
double s;
Complex rab;
rab.GiveMemory(n+ob.GetDegree());
for(i=0;i<=n-ob.GetDegree();i++)
{ s=0;
for(j=0;j<=n;j++)
for(k=0;k<=ob.GetDegree();k++)
if(j+k==i)s=s-koef[j]*ob.GetOneCoefficient(k);
rab.koef[i]=s;
}
rab.n=n+ob.GetDegree();
return rab;
}*/
ostream& operator<<(ostream& mystream,Complex &ob)
{
int i, pr=0;
mystream<<"f(x)=";
for(i=*ob.n; i>=0; i--)
{
ob.ShowTermOfComplex1(ob.koef,i,pr,mystream);
if(ob.koef[i]!=0) pr=1;
}
if(pr==0) mystream <<0;
return mystream;
}
istream& operator>>(istream& mystream,Complex &ob)
{
int i;
cout<<"Enter Degree:";
mystream>>*ob.n;
cout<<endl;
for(i=*ob.n;i>=0;i--)
{
cout<<"Enter koeff "<<i<<":";
mystream>>ob.koef[i];
}
return mystream;
}
int Complex::min(int n,int m)
{
if(n<m) return n;
else return m;
}
int Complex::max(int n, int m)
{
if(n<m) return m;
else return n;
}
int main(int argc, char* argv[])
{
Complex p,masp[10],*p1,s;
int n=5,i;
double K[6]={1.0,3.2,0.0,4.1,0.0,1.1};
p1=new Complex(n,K);
p.ShowComplex();
for(i=0; i<10; i++)
masp[i].ShowComplex();
p1->ShowComplex();
p.SetComplex();
p.ShowDegree();
p.ShowCoefficients();
p1->CalculateValue();
cout<<"Mnogochlen f(x)= ";
p.OnlyShowComplex();
cout<<"Mnogochlen g(x)= ";
masp[0].OnlyShowComplex();
s=p+masp[0];
cout<< "Mnogochlen f(x)+g(x) = ";
s.OnlyShowComplex();
s=p-masp[0];
cout<<"Mnogocnlen f(x)-g(x) = ";
s.OnlyShowComplex();
s=p*masp[0];
cout<<"Mnogochlen f(x)*g(x) = ";
s.OnlyShowComplex();
/*s=p/masp[0];
cout<<"Mnogochlen f(x)/d(x) = ";
s.OnlyShowComplex(); */
s=masp[0];
for(i=1;i<10;i++)
s=s+masp[i];
cout<<"Summa: ";
s.OnlyShowComplex();
cout<<"Nagmite klavishy!"<<endl;
while(!kbhit());
delete p1;
return 0;
} |