0 / 0 / 0
Регистрация: 26.05.2016
Сообщений: 14
|
|
1
|
Реализация класса полином
26.05.2016, 23:38. Показов 1730. Ответов 1
Help(((
C++ | 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
| #ifndef CLASS_POLINOM_CPP
#define CLASS_POLINOM_CPP
class Polynom
{
private:
unsigned int deg; //bazmandami astichany
double *coef; //cucich gortsakitsneri zangvatsi vra
void CorrectDeg();
public:
Polynom(); //aranc parametreri constructor
Polynom(unsigned int, double*); //parametrerov
Polynom(const Polynom &); //copy constructor
~Polynom(); //destructor
unsigned int GetDeg(); //astichan stanalu function
double GetCoef(unsigned int); //i-rd astichani bazmandami gortsakocneri stacum
unsigned int SetCoef(double, unsigned int); //i-rd astichani bazmandami gortsakci taly
Polynom operator + (const Polynom &);
Polynom operator = (const Polynom &);
friend Polynom MultConst(double, Polynom &); //bazmandamneri bazmapatkum
void InputPolynom(); //neratselu function
void OutputPolynom(); //artatselu function
};
#endif
#include <iostream>
using namespace std;
// 0 astichani ev 0 gortsakicnerov
Polynom::Polynom()
{
deg=0;
coef=new double[deg+1];
coef[0]=0.0;
}
Polynom::Polynom(unsigned int new_deg, double *new_coef)
{
deg=0;
for(unsigned int i=0;i <new_deg+1;i++)
if(new_coef[i]!=0)
deg=i;
coef=new double[deg+1];
for(unsigned int i=0;i<=deg;i++)
coef[i]=new_coef[i];
}
Polynom::Polynom (const Polynom &f)
{
deg=f.deg;
coef=new double[deg+1];
for(unsigned int i=0;i<=deg;i++)
coef[i]=f.coef[i];
}
Polynom::~Polynom()
{
delete[] coef;
}
unsigned int Polynom::GetDeg()
{
return deg;
}
double Polynom::GetCoef(unsigned int i)
{
if(i<=deg)
return coef[i];
else
return 0.0;
}
unsigned int Polynom::SetCoef(double new_coef, unsigned int i)
{
if(i<=deg)
coef[i]=new_coef;
else
return deg;
CorrectDeg();
return deg;
}
Polynom Polynom::operator + (const Polynom &t)
{
Polynom *result;
if(deg>=t.deg)
{
result=new Polynom(deg,coef);
for(unsigned i=0;i<=t.deg;i++)
result->coef[i]=result->coef[i]+t.coef[i];
}
else
{
result=new Polynom(t.deg,t.coef);
for(unsigned i=0;i<=deg;i++)
result->coef[i]=result->coef[i]+coef[i];
}
result->CorrectDeg();
return *result;
}
Polynom Polynom::operator = (const Polynom &t)
{
deg=t.deg;
delete[] coef;
coef=new double[deg+1];
for(unsigned int i=0;i<=deg;i++)
coef[i]=t.coef[i];
return *this;
}
//bazmandamy tvov bazmapatkeleu function
Polynom MultConst(double k, Polynom &t)
{
if(k==0)
{
Polynom result;
return result;
}
else
{
int deg=t.deg;
double *tmp_coef=new double[deg+1];
for(int i=0;i<=deg;i++)
tmp_coef[i]=k*t.coef[i];
Polynom result(deg,tmp_coef);
delete[] tmp_coef;
return result;
}
}
/*astichani chshgrtum
;orinak ete deg=4,bayc dra gortsakicy 0 e,ays funkcian deg-y kdardzni 3 ev aydpes minchev vor gortsakicy chlini 0 */
void Polynom::CorrectDeg()
{
if(coef[deg]==0)
while(deg && coef[deg]==0)
deg--;
}
void Polynom::InputPolynom()
{
cout << "Input degree: ";
cin >> deg;
delete[] coef;
coef=new double[deg+1];
for(unsigned int i=0;i<deg;i++)
cin >> coef[i];
}
void Polynom::OutputPolynom()
{
if(coef[deg]==1)
cout << "x^" << deg;
else
if(coef[deg]==-1)
cout << "-x^" << deg;
else
cout << coef[deg] << "x^" << deg;
for(int i=deg-1;i>0;i--)
{
if(coef[i]>0)
{
if(coef[i]==1)
cout << " + " << "x^" << i;
else
cout << " + " << coef[i] << "x^" << i;
}
else
if(coef[i]<0)
if(coef[i]==-1)
cout << " - " << "x^" << i;
else
cout << " - " << (-1)*coef[i] << "X^" << i;
}
if(coef[0]>0)
cout << " + " << coef[0] << endl;
else
if(coef[0]<0)
cout << " - " << (-1)*coef[0] << endl;
}
//main
#include <iostream>
using namespace std;
#include "Class_Polinom.h"
int main()
{
Polynom a;
a.InputPolynom();
a.OutputPolynom();
Polynom b;
b.InputPolynom();
b.OutputPolynom();
Polynom sum;
sum=a+b;
sum.OutputPolynom();
return 0;
} |
|
0
|