-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.h
More file actions
213 lines (196 loc) · 3.77 KB
/
Array.h
File metadata and controls
213 lines (196 loc) · 3.77 KB
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
#pragma once
#include<iostream>
#include<Windows.h>
#include <iomanip>
using namespace std;
template<typename T>
class Array
{
int size_n;
int size_m;
T** mas;
public:
Array();
Array(int,int);
Array(const Array&);
Array& operator=(const Array&) const;
void Vvod();
void Print();
Array operator+(const Array&) const;
Array operator-(const Array&) const;
T MaxElem();
T MinElem();
~Array();
};
template<typename T>
inline Array<T>::Array()
{
this->size_n = 0;
this->size_m = 0;
this->mas = nullptr;
}
template<typename T>
inline Array<T>::Array(int n, int m)
{
this->size_n = n;
this->size_m = m;
this->mas = new T * [this->size_n] {};
for (int i = 0; i < this->size_n; i++)
{
this->mas[i] = new T[this->size_m]{};
}
}
template<typename T>
inline Array<T>::Array(const Array& ArCopy)
{
this->size_n = ArCopy.size_n;
this->size_m = ArCopy.size_m;
this->mas = new T * [this->size_n];
for (int i = 0; i < this->size_n; i++)
{
this->mas[i] = new T[this->size_m];
}
for (int i = 0; i < this->size_n; i++)
{
for (int j = 0; j < this->size_m; j++)
{
this->mas[i][j] = ArCopy.mas[i][j];
}
}
}
template<typename T>
inline Array<T>& Array<T>::operator=(const Array<T>& ArCopy) const
{
if (*this != &ArCopy)
{
if (this != nullptr)
{
for (int i = 0; i < this->size_n; i++)
if (this->mas[i] != nullptr)
delete[] this->mas[i];
delete[] this->mas;
}
this->size_n = ArCopy.size_n;
this->size_m = ArCopy.size_m;
this->mas = new T * [this->size_n];
for (int i = 0; i < this->size_n; i++)
{
this->mas[i] = new T[this->size_m];
}
for (int i = 0; i < this->size_n; i++)
{
for (int j = 0; j < this->size_m; j++)
{
this->mas[i][j] = ArCopy.mas[i][j];
}
}
}
return Array();
}
template<typename T>
inline void Array<T>::Vvod()
{
cout << "Vvedit znachennia elementiv massuva:\n";
for (int i = 0; i < this->size_n; i++)
{
for (int j = 0; j < this->size_m; j++)
{
cout << "element: " << i << "-" << j << ":\t";
cin >>this-> mas[i][j];
}
}
}
template<typename T>
inline void Array<T>::Print()
{
for (int i = 0; i < this->size_n; i++)
{
for (int j = 0; j < this->size_m; j++)
{
cout << setw(6) << this->mas[i][j] << "\t";
}
cout << "\n";
}
cout << "-----------------------------------------------------------\n";
}
template<typename T>
inline Array<T> Array<T>::operator+(const Array<T>& ar) const
{
if (this->size_n != ar.size_n || this->size_m != ar.size_m)
{
cout << "Dodavannia nemogluve.\n";
exit(1);
}
else
{
Array tmp(this->size_n, this->size_m);
for (int i = 0; i < this->size_n; i++)
{
for (int j = 0; j < this->size_m; j++)
{
tmp.mas[i][j] = this->mas[i][j] + ar.mas[i][j];
}
}
return tmp;
}
}
template<typename T>
inline Array<T> Array<T>::operator-(const Array<T>& ar) const
{
if (this->size_n != ar.size_n && this->size_m != ar.size_m)
{
cout << "Vidnimannia nemogluve.\n";
exit(1);
}
else
{
Array tmp(this->size_n, this->size_m);
for (int i = 0; i < this->size_n; i++)
{
for (int j = 0; j < this->size_m; j++)
{
tmp.mas[i][j] = this->mas[i][j] - ar.mas[i][j];
}
}
return tmp;
}
}
template<typename T>
inline T Array<T>::MaxElem()
{
T Max = this->mas[0][0];
for (int i = 0; i < this->size_n; i++)
{
for (int j = 0; j < this->size_m; j++)
{
if (this->mas[i][j] > Max)
Max = this->mas[i][j];
}
}
return Max;
}
template<typename T>
inline T Array<T>::MinElem()
{
T Min = this->mas[0][0];
for (int i = 0; i < this->size_n; i++)
{
for (int j = 0; j < this->size_m; j++)
{
if (this->mas[i][j] < Min)
Min = this->mas[i][j];
}
}
return Min;
}
template<typename T>
inline Array<T>::~Array()
{
for (int i = 0; i < this->size_n; i++)
{
delete []this->mas[i];
this->mas[i] = nullptr;
}
delete[]this->mas;
this->mas = nullptr;
}