10.06.2015, 12:56. Показов 1407. Ответов 0
не сплю уже 2ые сутки кропаю данную задачу....не подскажете ли как еще возможно выполнить следующую задачу:
Создать массив объектов класса Matrix и передать его в функцию, кото-
рая изменяет i -ю матрицу путем возведения ее в квадрат. В головной
программе вывести результат в этом же коде...
заранее крайне благодарен
Добавлено через 14 минут
снова получился затык....вроде все нормально вставил но при выполнении на экране норма не выводится...хотя компилятор ошибок не показывает
подскажите пожалуйста, где ошибся в этот раз?
| 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
| #include "stdafx.h"
#include <vector>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <Windows.h>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <string>
#include <stdexcept>
#ifndef _MATRIX_H_
#define _MATRIX_H_
namespace MatrSpace
{
//Контейнерный класс матрицы
template<class T> // создаем шаблон класс
class AbstractMatr // создаем класс
{
public:
AbstractMatr() {} // Конструктор по умолчанию
AbstractMatr(size_t n, size_t m);
AbstractMatr(const AbstractMatr&Ob):Matrix(Ob.Matrix) {} // Конструктор копирования
virtual ~AbstractMatr() {}
void SetSize(size_t n, size_t m);
inline const size_t GetRow() const {return Matrix.size();}
inline const size_t GetCol() const {return Matrix[0].size();}
void swap(AbstractMatr&);
protected:
std::vector<std::vector<T> > Matrix;
};
//открытие математических функций матрицы AbstractMatr
template<class T> создаем шаблон класс
class MathMatr:public AbstractMatr<T> // создаем класс
{
public:
MathMatr():AbstractMatr() {}
MathMatr(size_t n, size_t m):AbstractMatr<T>(n, m) {}
MathMatr(const AbstractMatr<T>& Ob):AbstractMatr<T>(Ob) {} // Конструктор копирования
virtual ~MathMatr() {}
float norma(); //норма матрицы
const MathMatr<T>& operator =(const MathMatr&); // Перегрузка =
const MathMatr<T>& operator +=(const MathMatr&);
const MathMatr<T> operator +(const MathMatr&) const; // Сложение матриц
const MathMatr<T>& operator -=(const MathMatr&);
const MathMatr<T> operator -(const MathMatr&) const; // Вычитание матриц
const MathMatr<T>& operator *=(const MathMatr&);
const MathMatr<T> operator *(const MathMatr&) const; // Перемножение матриц
virtual void input(std::istream&) {}
virtual void output(std::ostream&) const {}
template<class T2>
friend std::ostream& operator <<(std::ostream&, const MathMatr<T2>& Ob); // Перегрузка оператора << для вывода матрицы
template<class T2>
friend std::istream& operator >>(std::istream&, MathMatr<T2>& Ob); // Перегрузка оператора >> для ввода матрицы
void random_fill();
};
//вывод ввод класса IOMatr из класса MathMatr
template<class T>
class IOMatr:public MathMatr<T>
{
public:
IOMatr():MathMatr<T>() {}
IOMatr(size_t n, size_t m):MathMatr(n, m) {}
IOMatr(const AbstractMatr<T>& Ob):MathMatr(Ob) {}
virtual ~IOMatr() {}
virtual void input(std::istream&)=0;
virtual void output(std::ostream&) const=0;
};
//вывод ввод класса IOMatr из класса MathMatr
template<class T>
class ConsoleMatr:public IOMatr<T>
{
public:
ConsoleMatr():IOMatr<T>() {}
ConsoleMatr(size_t n, size_t m):IOMatr(n, m) {}
ConsoleMatr(const AbstractMatr<T>& Ob):IOMatr(Ob) {} // Конструктор копирования
virtual ~ConsoleMatr() {}
float norma(); // Норма матрицы
const ConsoleMatr<T>& operator =(const ConsoleMatr&); // Перегрузка =
const ConsoleMatr<T>& operator +=(const ConsoleMatr&);
const ConsoleMatr<T> operator +(const ConsoleMatr&) const; // Сложение матриц
const ConsoleMatr<T>& operator -=(const ConsoleMatr&);
const ConsoleMatr<T> operator -(const ConsoleMatr&) const; // Вычитание матриц
const ConsoleMatr<T>& operator *=(const ConsoleMatr&);
const ConsoleMatr<T> operator *(const ConsoleMatr&) const; // Перемножение матриц
virtual void input(std::istream&);
virtual void output(std::ostream&) const;
};
//AbstractMatr функции класса
template<class T>
AbstractMatr<T>::AbstractMatr(size_t n, size_t m) // Конструктор копирования
{
Matrix.resize(n);
for(int i=0; i!=GetRow(); ++i)
{
Matrix[i].resize(m);
}
}
template<class T>
void AbstractMatr<T>::SetSize(size_t n, size_t m)
{
if(GetRow()!=0&&GetCol()!=0)
Matrix.clear();
Matrix.resize(n);
for(int i=0; i!=GetRow(); ++i)
{
Matrix[i].resize(m);
}
}
template<class T>
void AbstractMatr<T>::swap(AbstractMatr<T>& Ob)
{
Matrix.swap(Ob.Matrix);
}
//MathMatr функции класса
template<class T>
const MathMatr<T>& MathMatr<T>::operator =(const MathMatr<T>& Ob)
{
MathMatr<T> Temp(Ob);
Temp.swap(*this);
return *this;
}
//Сложение
template<class T>
const MathMatr<T>& MathMatr<T>::operator +=(const MathMatr<T>& Ob)
{
if(GetRow()!=Ob.GetRow()&&GetCol()!=Ob.GetCol())
throw std::invalid_argument("Size of two matrix for sum must be equal!");
for(int i=0; i!=Ob.GetRow(); ++i)
{
for(int j=0; j!=Ob.GetCol(); ++j)
{
Matrix[i][j]+=Ob.Matrix[i][j];
}
}
return *this;
}
template<class T>
const MathMatr<T> MathMatr<T>::operator +(const MathMatr<T>& Ob) const
{
MathMatr<T> Temp(*this);
Temp+=Ob;
return Temp;
}
//Вычитание
template<class T>
const MathMatr<T>& MathMatr<T>::operator -=(const MathMatr<T>& Ob)
{
if(GetRow()!=Ob.GetRow()&&GetCol()!=Ob.GetCol())
throw std::invalid_argument("Size of two matrix for sum must be equal!");
for(int i=0; i!=Ob.GetRow(); ++i)
{
for(int j=0; j!=Ob.GetCol(); ++j)
{
Matrix[i][j]-=Ob.Matrix[i][j];
}
}
return *this;
}
template<class T>
const MathMatr<T> MathMatr<T>::operator -(const MathMatr<T>& Ob) const
{
MathMatr<T> Temp(*this);
Temp-=Ob;
return Temp;
}
//Умножение
template<class T>
const MathMatr<T>& MathMatr<T>::operator *=(const MathMatr<T>& Ob)
{
if(GetCol()!=Ob.GetRow())
throw std::invalid_argument("Num of 1-st matrix cols must be equal to num of 2-nd matrix rows");
MathMatr Temp(GetRow(), Ob.GetCol());
for(int i=0; i!=Temp.GetRow(); ++i)
{
for(int j=0; j!=Temp.GetCol(); ++j)
{
Temp.Matrix[i][j]=0;
for(int k=0; k!=GetCol(); ++k)
{
Temp.Matrix[i][j]+=Matrix[i][k]*Ob.Matrix[k][j];
}
}
}
*this=Temp;
return *this;
}
template<class T>
const MathMatr<T> MathMatr<T>::operator *(const MathMatr<T>& Ob) const
{
MathMatr<T> Temp(*this);
Temp*=Ob;
return Temp;
}
template<class T>
void MathMatr<T>::random_fill()
{
for(int i=0; i!=GetRow(); ++i)
{
for(int j=0; j!=GetCol(); ++j)
{
Matrix[i][j]=1+rand()%50;
}
}
}
template<class T>
std::ostream& operator <<(std::ostream& os, const MathMatr<T>& Ob)
{
Ob.output(os);
return os;
}
template<class T>
std::istream& operator >>(std::istream& is, MathMatr<T>& Ob)
{
Ob.input(is);
return is;
}
//ConsoleMatr функции класса
template<class T>
void ConsoleMatr<T>::input(std::istream& is)
{
for(int i=0; i!=GetRow(); ++i)
{
for(int j=0; j!=GetCol(); ++j)
{
std::cout<<"Enter Matrix ["<<i+1<<"]["<<j+1<<"]: ";
is>>Matrix[i][j];
}
}
}
template<class T>
void ConsoleMatr<T>::output(std::ostream& os) const
{
for(int i=0; i!=GetRow(); ++i)
{
for(int j=0; j!=GetCol(); ++j)
{
os<<std::setw(5)<<Matrix[i][j]<<' ';
}
std::cout<<std::endl;
}
}
template<class T>
const ConsoleMatr<T>& ConsoleMatr<T>::operator =(const ConsoleMatr<T>& Ob)
{
ConsoleMatr Temp(Ob);
Temp.swap(*this);
return *this;
}
//сложение
template<class T>
const ConsoleMatr<T>& ConsoleMatr<T>::operator +=(const ConsoleMatr<T>& Ob)
{
if(GetRow()!=Ob.GetRow()&&GetCol()!=Ob.GetCol())
throw std::invalid_argument("Size of two matrix for sum must be equal!");
for(int i=0; i!=Ob.GetRow(); ++i)
{
for(int j=0; j!=Ob.GetCol(); ++j)
{
Matrix[i][j]+=Ob.Matrix[i][j];
}
}
return *this;
}
template<class T>
const ConsoleMatr<T> ConsoleMatr<T>::operator +(const ConsoleMatr<T>& Ob) const
{
ConsoleMatr<T> Temp(*this);
Temp+=Ob;
return Temp;
}
//вычитание
template<class T>
const ConsoleMatr<T>& ConsoleMatr<T>::operator -=(const ConsoleMatr<T>& Ob)
{
if(GetRow()!=Ob.GetRow()&&GetCol()!=Ob.GetCol())
throw std::invalid_argument("Size of two matrix for vichitanie must be equal!");
for(int i=0; i!=Ob.GetRow(); ++i)
{
for(int j=0; j!=Ob.GetCol(); ++j)
{
Matrix[i][j]-=Ob.Matrix[i][j];
}
}
return *this;
}
template<class T>
const ConsoleMatr<T> ConsoleMatr<T>::operator -(const ConsoleMatr<T>& Ob) const
{
ConsoleMatr<T> Temp(*this);
Temp-=Ob;
return Temp;
}
//умножение
template<class T>
const ConsoleMatr<T>& ConsoleMatr<T>::operator *=(const ConsoleMatr<T>& Ob)
{
if(GetCol()!=Ob.GetRow())
throw std::invalid_argument("Num of 1-st matrix cols must be equal to num of 2-nd matrix rows");
ConsoleMatr Temp(GetRow(), Ob.GetCol());
for(int i=0; i!=Temp.GetRow(); ++i)
{
for(int j=0; j!=Temp.GetCol(); ++j)
{
for(int k=0; k!=GetCol(); ++k)
{
Temp.Matrix[i][j]+=Matrix[i][k]*Ob.Matrix[k][j];
}
}
}
*this=Temp;
return *this;
}
template<class T>
const ConsoleMatr<T> ConsoleMatr<T>::operator *(const ConsoleMatr<T>& Ob) const
{
ConsoleMatr<T> Temp(*this);
Temp*=Ob;
return Temp;
}
}
#endif
int main()
{
using namespace MatrSpace;
srand(static_cast<unsigned>(time(NULL)));
ConsoleMatr<int> Ob1;
size_t row, col;
std::cout<<"Enter num of rows and cols for 1-st matrix: ";
std::cin>>row>>col;
Ob1.SetSize(row, col);
int choise=0;
std::cout<<"Enter 1 for fill matrix from keyboard\n"
<<"Enter 2 for random fill matrix\n";
std::cin>>choise;
if(choise==1)
std::cin>>Ob1;
else if(choise==2)
Ob1.random_fill();
else
{
std::cerr<<"There is no such option\n";
return 0;
}
std::cout<<std::endl;
ConsoleMatr<int> Ob2;
std::cout<<"Enter num of rows and cols for 2-nd matrix: ";
std::cin>>row>>col;
Ob2.SetSize(row, col);
choise=0;
std::cout<<"Enter 1 for fill matrix from keyboard\n"
<<"Enter 2 for random fill matrix\n";
std::cin>>choise;
if(choise==1)
std::cin>>Ob2;
else if(choise==2)
Ob2.random_fill();
else
{
std::cerr<<"There is no such option\n";
return 0;
}
std::cout<<std::endl;
{
std::cout<<"First matrix\n\n"<< Ob1 <<'\n';
std::cout<<"Second matrix\n\n"<< Ob2 <<'\n';
//вывод сложения на экран
ConsoleMatr<int> Ob3;
try
{
Ob3=Ob1+Ob2;
}
catch(const std::invalid_argument&e)
{
std::cout<<e.what()<<'\n';
return 0;
}
std::cout<<"Summary of first matrix to second matrix\n\n";
std::cout<<Ob3<<'\n';
}
//вывод умножения на экран
ConsoleMatr<int> Ob4;
try
{
Ob4=Ob1*Ob2;
}
catch(const std::invalid_argument&e)
{
std::cout<<e.what()<<'\n';
return 0;
}
std::cout<<"Multy of first matrix to second matrix\n\n";
std::cout<<Ob4<<'\n';
//вывод вычитания на экран
ConsoleMatr<int> Ob5;
try
{
Ob5=Ob1-Ob2;
}
catch(const std::invalid_argument&e)
{
std::cout<<e.what()<<'\n';
return 0;
}
std::cout<<"Vichitanie of first matrix to second matrix\n\n";
std::cout<<Ob5<<'\n';
ConsoleMatr<int> Ob6;
try
{ double LinesNorma(double** Ob6,int N,int M)
{ double S=0;
for(int i=0;i<N;i++)
{ double St=0;
for(int j=0;j<M;j++)
St+=fabs(Ob6[i][j]);
if(S<St)
St=S;
}
catch(const std::invalid_argument&e) };
{
return S;
}
std::cout<<" norma matrix\n\n";
std::cout<<Ob6<<'\n';
}
}
//normmatric |
|