include iostream using namespace std class vect double ìàññèâ int dim

  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
#include <iostream>
using namespace std;
class vect{
double* b; //ìàññèâ
int dim; //ðàçìåðíîñòü
static int count;
public:
int num;//íîìåð êîíêðåòíîãî âåêòîðà
vect();
vect(int n, double x[]);
vect(vect &x);
~vect();
void print();
void operator= (vect &r);
vect operator +(vect &r);
friend vect * operator-(vect &l, vect &r);
vect operator-(); //óíàðíûé ìèíóñ
friend vect * operator*(double k, vect &r);
friend vect * operator*(vect &r, double k);
friend vect * operator*(vect &r, vect &l);
};
int vect::count=0;
vect::vect(){
b = 0;
dim = 0;
count++;
num = count;
cout<<"Konstruktor vect() sozdal vector - "<<num<<endl;
}
vect::vect(int n, double x[]){
count++;
num = count;
dim=n;
b=new double[dim];
for(int i=0; i<dim;i++)
b[i]=x[i];
cout<<"Konstruktor vect(int n, double x[]) sozdal vector - "<<num<<endl;
}
vect::vect(vect &x){
count++;
num = count;
dim = x.dim;
b = new double [x.dim];
for(int i = 0; i<dim; i++ ){
b[i] = x.b[i];
}
cout<<"Konstruktor vect(vect &x) sozdal vector - "<<num<<endl;
}
void vect::print(){
cout<< "dim vectora:" << " " << dim<< endl;
cout<< "Vector: ";
cout << "{ ";
for(int i=0; i<dim; i++)
{
cout << b[i] << (i!=dim-1 ? ", " : "");
}
cout << " }" << endl;
}
vect::~vect(){
delete []b;
cout<<"Uni4tozhaem vector - "<<num<<endl;
}
void vect::operator=(vect &r){
if (b == NULL) {
dim = r.dim;
b = new double[dim];
}
for (int i = 0; i<dim; i++) {
b[i] = r.b[i];
}
cout<<"Peregruzka operatora ="<<endl;
}
vect vect::operator+(vect &r){
cout<<"Summirovanie: vector "<< num << "+ vector "<<r.num<<endl;
if(b==NULL){
dim = r.dim;
b = new double[dim];
}
for (int i = 0 ; i<dim; i++) {
b[i] += r.b[i];
}
cout<<"Vypolnyaetsya slozhenie"<<endl;
return *this;
}
vect vect::operator-(){
for (int i = 0; i<dim; i++) {
b[i] = 0 - b[i];
}
cout<<"Unarniy minus"<<endl;
return *this;
}
vect* operator *(vect &r, double k){
for (int i = 0; i<r.dim; i++) {
r.b[i] = r.b[i] *k;
}
cout<<"Peregruzka operatora *"<<endl;
return &r;
}
vect* operator *(double k, vect &r){
for (int i =0; i<r.dim; i++) {
r.b[i] = r.b[i] *k;
}
cout<<"Peregruzka operatora *"<<endl;
return &r;
}
vect* operator *(vect &r, vect &l){
for (int i = 0 ; i<r.dim; i++) {
r.b[i] = r.b[i] * l.b[i];
}
cout<<"Peregruzka operatora *"<<endl;
return &r;
}
vect* operator-(vect &l, vect &r){
for (int i = 0; i<r.dim; i++) {
l.b[i] = l.b[i] - r.b[i];
}
cout<<"Peregruzka operatora -"<<endl;
return &l;
}
/*class matr
{
static int count;
double *a;//iaiiia?iue iannea ?acia?iinoe dim^2
int dim;
public:
int num;
matr();
matr(int n); //aaeie?iay iao?eoa ?acia?iinoe n
matr(int n, double *x); //nicaaiea iieiioaiiie iao?eou
matr(matr &x); //eiino?oeoi? eiie?iaaiey
~matr(); //aeno?oeoi?
void print(); // ia?aou
void operator =(matr &r);//eiino?oeoi? i?e?aaieaaiey
matr operator +(matr &r);//eiino?oeoi? nei?aiey
matr operator -(); //oia?iue ieion
friend matr operator -(matr &r);//eiino?oeoi? au?eoaiey
matr operator *(matr &r);//eiino?oeoi? oiii?aiey iao?eo
friend matr operator *(double k, matr&r);//oiii?aiea iao?eou ia ?enei
//friend vect & operator *(matr&r, vect &r); // oiii?aiea iao?eou ia aaeoi?
};
int matr::count=0;
matr:: matr()
{count++;
num=count;
cout<<"Konstructor matr() sozdal matr ?"<<num<<endl;
}
matr:: matr(int n)
{count ++;
num=count;
dim = n;
a=new double [dim*dim];
for (int i=0; i<dim; i++)
{
for (int j=0; j<dim; j++)
if (i=j) a[i*j]=1; else a[i*j]=0;
}
cout<<"Konstructor matr(int n) sozdal matr ?"<<num<<endl;
}
matr:: matr(int n, double *x)
{
count ++;
num=count;
dim = n;
a=new double [dim*dim];
for (int i=0; i<dim; i++)
for (int j=0; j<dim; j++)
a[i*j]=x[i*j];
cout<<"Konstructor matr(int n, double *x) sozdal matr ?"<<num<<endl;
}
matr:: matr(matr &x)
{
count ++;
num=count;
dim=x.dim;
a=new double [dim*dim];
for (int i=0; i<dim; i++)
for (int j=0; j<dim; j++)
a[i*j]=x.a[i*j];
cout<<"Konstructor vect (vect &x) sozdal matr ?"<<num<<"copy matr"<<x.dim<<endl;
}
matr:: ~matr()
{
//for (int i=0; i<dim; i++)
//for (int j=0; j<dim; j++)
delete []a;
cout<<"delete matr ?"<<num<<endl;
}
void matr:: print () //eiino?oeoi? ia?aoe
{cout<<"dim="<<dim;
for (int i=0; i<dim; i++)
{
cout<<endl;
for (int j=0; j<dim; j++)
cout<<a[i*j]<<" ";
}
}
void matr:: operator =(matr &r)
{
dim=r.dim;
if (a==NULL)
a=new double [dim*dim];
else{
for (int i=0; i<dim; i++)
for (int j=0; j<dim; j++)
a[i*j]=r.a[i*j];
cout<<"operator = priravnyal matr"<<num<<endl;
return;
}
}
matr matr::operator +(matr &r)
{
double *y=new double [r.dim];
for (int i=0; i<r.dim; i++)
for (int j=0; j<r.dim; j++)
y[i*j]=0;
matr tmp(r.dim, y);
for(int i=0; i<r.dim; i++)
for(int j=0; j<r.dim; j++)
tmp.a[i*j]=a[i*j]+r.a[i*j];
return tmp;
cout<<"Summirovanie: matr ?"<<num<<"and matr ?"<<r.num;
}
matr matr:: operator -()
{
for (int i=0; i<dim; i++)
for (int j=0; j<dim; j++)
a[i*j]= 0 - a[i*j];
cout<<"Konstructor unary minus izmenil matr ?"<<num<<endl;
return *this;
}
matr operator -(matr &l, matr &r)
{
matr tmp=l;
for (int i=0; i<l.dim; i++)
for (int j=0; j<l.dim; j++)
tmp.a[i*j]-=r.a[i*j];
cout<<"konstruktor - vychel matr "<<r.num<<" iz matr "<<l.num;
return tmp;
}
matr operator *(double k, matr&r)
{
for (int i=0; i<r.dim; i++)
for (int j=0; j<r.dim; j++)
r.a[i*j]=k*r.a[i*j];
cout<<"konstruktor * na chislo umnozil vect"<<r.num<<"na chislo"<<k<<endl;
return r;
}
*/
int main() {
int dim = 3;
double *b = new double [dim];
b[0] = 3;
b[1] = 3;
b[2] = 3;
int dim2 = 3;
double *b1 = new double [dim2];
b1[0] = 2;
b1[1] = 2;
b1[2] = 2;
vect a1(dim, b);
vect a2(dim2, b1);
a1*a2;
a1.print();
a2.print();
system("PAUSE");
return 0;
}