forked from UNN-ITMM-Software/mp2-lab2-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutmatrix.h
296 lines (260 loc) · 10.4 KB
/
utmatrix.h
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
// ННГУ, ВМК, Курс "Методы программирования-2", С++, ООП
//
// utmatrix.h - Copyright (c) Гергель В.П. 07.05.2001
// Переработано для Microsoft Visual Studio 2008 Сысоевым А.В. (21.04.2015)
//
// Верхнетреугольная матрица - реализация на основе шаблона вектора
#ifndef __TMATRIX_H__
#define __TMATRIX_H__
#include <iostream>
using namespace std;
const int MAX_VECTOR_SIZE = 100000000;
const int MAX_MATRIX_SIZE = 10000;
// Шаблон вектора
template <class ValType>
class TVector
{
protected:
ValType *pVector;
int Size; // размер вектора
int StartIndex; // индекс первого элемента вектора
public:
TVector(int s = 10, int si = 0);
TVector(const TVector &v); // конструктор копирования
~TVector();
int GetSize() { return Size; } // размер вектора
int GetStartIndex(){ return StartIndex; } // индекс первого элемента
ValType& operator[](int pos); // доступ
bool operator==(const TVector &v) const; // сравнение
bool operator!=(const TVector &v) const; // сравнение
TVector& operator=(const TVector &v); // присваивание
// скалярные операции
TVector operator+(const ValType &val); // прибавить скаляр
TVector operator-(const ValType &val); // вычесть скаляр
TVector operator*(const ValType &val); // умножить на скаляр
// векторные операции
TVector operator+(const TVector &v); // сложение
TVector operator-(const TVector &v); // вычитание
ValType operator*(const TVector &v); // скалярное произведение
// ввод-вывод
friend istream& operator>>(istream &in, TVector &v)
{
for (int i = 0; i < v.Size; i++)
in >> v.pVector[i];
return in;
}
friend ostream& operator<<(ostream &out, const TVector &v)
{
for (int i = 0; i < v.StartIndex; i++)
out << "0" << "\t";
for (int i = v.StartIndex; i < v.Size; i++)
out << v.pVector[i] << "\t";
return out;
}
};
template <class ValType>
TVector<ValType>::TVector(int s, int si)
{
if ((s<0)||(s>MAX_VECTOR_SIZE)) throw "Wrong vector size";
if ((si<0)||(si>s)) throw "Wrong index";
Size=s;
StartIndex=si;
pVector =new ValType[Size];
for (int i=StartIndex;i<Size;i++)
pVector[i]=0;
} /*-------------------------------------------------------------------------*/
template <class ValType> //конструктор копирования
TVector<ValType>::TVector(const TVector<ValType> &v)
{
Size=v.Size;
StartIndex=v.StartIndex;
pVector =new ValType[Size];
for (int i=StartIndex;i<Size;i++)
pVector[i]=v.pVector[i];
} /*-------------------------------------------------------------------------*/
template <class ValType>
TVector<ValType>::~TVector()
{
delete [] pVector;
} /*-------------------------------------------------------------------------*/
template <class ValType> // доступ
ValType& TVector<ValType>::operator[](int pos)
{
if ((pos<StartIndex)||(pos>=Size)) throw "Wrong position";
return pVector[pos];
} /*-------------------------------------------------------------------------*/
template <class ValType> // сравнение
bool TVector<ValType>::operator==(const TVector &v) const
{
bool res=true;
if (this == &v) return res;
if ((Size!=v.Size)||(StartIndex!=v.StartIndex)) res=false;
else for (int i=StartIndex;i<Size;i++)
if (pVector[i]!=v.pVector[i]) {res=false; break;}
return res;
} /*-------------------------------------------------------------------------*/
template <class ValType> // сравнение
bool TVector<ValType>::operator!=(const TVector &v) const
{
bool res=false;
if (this == &v) return res;
if ((Size!=v.Size)||(StartIndex!=v.StartIndex)) res=true;
else for (int i=StartIndex;i<Size;i++)
if (pVector[i]!=v.pVector[i]) {res=true; break;}
return res;
} /*-------------------------------------------------------------------------*/
template <class ValType> // присваивание
TVector<ValType>& TVector<ValType>::operator=(const TVector &v)
{
if(this!=&v){
Size=v.Size;
StartIndex=v.StartIndex;
delete []pVector;
pVector=new ValType[Size];
for(int i=StartIndex;i<Size;i++)
pVector[i]=v.pVector[i];
}
return *this;
} /*-------------------------------------------------------------------------*/
template <class ValType> // прибавить скаляр
TVector<ValType> TVector<ValType>::operator+(const ValType &val)
{
TVector sum(Size,StartIndex);
for(int i=StartIndex; i<Size;i++)
sum.pVector[i]=pVector[i]+val;
return sum;
} /*-------------------------------------------------------------------------*/
template <class ValType> // вычесть скаляр
TVector<ValType> TVector<ValType>::operator-(const ValType &val)
{
TVector sum(Size,StartIndex);
for(int i=StartIndex; i<Size;i++)
sum.pVector[i]=pVector[i]-val;
return sum;
} /*-------------------------------------------------------------------------*/
template <class ValType> // умножить на скаляр
TVector<ValType> TVector<ValType>::operator*(const ValType &val)
{
TVector comp(Size,StartIndex);
for(int i=StartIndex; i<Size;i++)
comp.pVector[i]=pVector[i]*val;
return comp;
} /*-------------------------------------------------------------------------*/
template <class ValType> // сложение
TVector<ValType> TVector<ValType>::operator+(const TVector<ValType> &v)
{
if ((Size!=v.Size)||(StartIndex!=v.StartIndex)) throw "Wrong size";
TVector sum(Size,StartIndex);
for(int i=StartIndex; i<Size;i++)
sum.pVector[i]=pVector[i]+v.pVector[i];
return sum;
} /*-------------------------------------------------------------------------*/
template <class ValType> // вычитание
TVector<ValType> TVector<ValType>::operator-(const TVector<ValType> &v)
{
if ((Size!=v.Size)||(StartIndex!=v.StartIndex)) throw "Wrong size";
TVector sum(Size,StartIndex);
for(int i=StartIndex; i<Size;i++)
sum.pVector[i]=pVector[i]-v.pVector[i];
return sum;
} /*-------------------------------------------------------------------------*/
template <class ValType> // скалярное произведение
ValType TVector<ValType>::operator*(const TVector<ValType> &v)
{
if ((Size!=v.Size)||(StartIndex!=v.StartIndex)) throw "Wrong size";
ValType comp=0;
for(int i=StartIndex; i<Size;i++)
comp+=pVector[i]*v.pVector[i];
return comp;
} /*-------------------------------------------------------------------------*/
// Верхнетреугольная матрица
template <class ValType>
class TMatrix : public TVector<TVector<ValType> >
{
public:
TMatrix(int s = 10);
TMatrix(const TMatrix &mt); // копирование
TMatrix(const TVector<TVector<ValType> > &mt); // преобразование типа
bool operator==(const TMatrix &mt) const; // сравнение
bool operator!=(const TMatrix &mt) const; // сравнение
TMatrix& operator= (const TMatrix &mt); // присваивание
TMatrix operator+ (const TMatrix &mt); // сложение
TMatrix operator- (const TMatrix &mt); // вычитание
// ввод / вывод
friend istream& operator>>(istream &in, TMatrix &mt)
{
for (int i = 0; i < mt.Size; i++)
in >> mt.pVector[i];
return in;
}
friend ostream & operator<<( ostream &out, const TMatrix &mt)
{
for (int i = 0; i < mt.Size; i++)
out << mt.pVector[i] << endl;
return out;
}
};
template <class ValType>
TMatrix<ValType>::TMatrix(int s): TVector<TVector<ValType> >(s)
{
if ((s<0)||(s>MAX_MATRIX_SIZE)) throw "Wrong matrix size";
for(int i=0; i<s; i++)
pVector[i]=TVector <ValType>(s,i);
for(int i=0; i<s; i++)
for(int j=i; j<s; j++)
pVector[i][j]=0;
} /*-------------------------------------------------------------------------*/
template <class ValType> // конструктор копирования
TMatrix<ValType>::TMatrix(const TMatrix<ValType> &mt):
TVector<TVector<ValType> >(mt) {}
template <class ValType> // конструктор преобразования типа
TMatrix<ValType>::TMatrix(const TVector<TVector<ValType> > &mt):
TVector<TVector<ValType> >(mt) {}
template <class ValType> // сравнение
bool TMatrix<ValType>::operator==(const TMatrix<ValType> &mt) const
{
bool res=true;
if ((Size!=mt.Size)||(StartIndex!=mt.StartIndex)) res=false;
else for (int i=StartIndex;i<Size;i++)
if (pVector[i]!=mt.pVector[i]) {res=false; break;}
return res;
} /*-------------------------------------------------------------------------*/
template <class ValType> // сравнение
bool TMatrix<ValType>::operator!=(const TMatrix<ValType> &mt) const
{
bool res=false;
if (this == &mt) return res;
if ((Size!=mt.Size)||(StartIndex!=mt.StartIndex)) res=true;
else for (int i=StartIndex;i<Size;i++)
if (mt.pVector[i]!=pVector[i]) {res=true; break;}
return res;
} /*-------------------------------------------------------------------------*/
template <class ValType> // присваивание
TMatrix<ValType>& TMatrix<ValType>::operator=(const TMatrix<ValType> &mt)
{
if(this!=&mt){
if(Size!=mt.Size)
{
delete [] pVector;
pVector = new TVector<ValType>[mt.Size];
}
Size=mt.Size;
StartIndex=mt.StartIndex;
for(int i=0;i<Size;i++)
pVector[i]=mt.pVector[i];
}
return *this;
} /*-------------------------------------------------------------------------*/
template <class ValType> // сложение
TMatrix<ValType> TMatrix<ValType>::operator+(const TMatrix<ValType> &mt)
{
return TVector<TVector<ValType> >::operator+(mt);
} /*-------------------------------------------------------------------------*/
template <class ValType> // вычитание
TMatrix<ValType> TMatrix<ValType>::operator-(const TMatrix<ValType> &mt)
{
return TVector<TVector<ValType> >::operator-(mt);
} /*-------------------------------------------------------------------------*/
// TVector О3 Л2 П4 С6
// TMatrix О2 Л2 П3 С3
#endif