-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab 6.cpp
315 lines (287 loc) · 5.09 KB
/
lab 6.cpp
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
// lab 6.cpp: определяет точку входа для консольного приложения.
//
#include <string>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <conio.h>
#include <time.h>
#include <algorithm>
using namespace std;
const int modul = 100;
class element
{
protected:
int data;
public:
element(int a = 0)
{
data = a%modul;
}
friend element operator+(const element& s1, const element& s2);
friend element operator+(element& s1, int s2);
friend bool operator==(element& s1, int s2);
friend bool operator<(element& s1, int s2);
friend element operator*(const element& s1, const element& s2);
friend ostream& operator << (ostream& out, element const& a);
friend istream& operator >> (istream& in, element& a);
};
element operator+(const element& s1, const element& s2)
{
int q = s1.data + s2.data;
element a(q);
return a;
}
element operator*(const element& s1, const element& s2)
{
int q = s1.data * s2.data;
element a(q);
return a;
}
bool operator==(element& s1, int s2)
{
if (s1.data == s2)
return 1;
else return 0;
}
bool operator<(element& s1, int s2)
{
if (s1.data < s2)
return 1;
else return 0;
}
element operator+(element& s1, int s2)
{
int q = s1.data + s2;
element a(q);
return a;
}
ostream& operator << (ostream& out, element const& a)
{
out << a.data;
return out;
}
istream& operator >> (istream& in, element& a)
{
string el;
in >> el;
stringstream ss;
ss << el;
ss >> a.data;
return in;
}
template<typename t, int s1, int s2>
class matrix
{
protected:
t** a;
public:
matrix(t** dv = 0)
{
a = new t*[s1];
for (int i = 0;i < s1; i++)
{
a[i] = new t[s2];
}
for (int i = 0;i < s1; i++)
{
for (int j = 0;j < s2; j++)
{
if (dv != 0)
{
a[i][j] = dv[i][j];
}
else a[i][j] = 0;
}
}
}
t poluch(int c, int b)
{
return a[c][b];
}
void vstav(int c, int b, t d)
{
a[c][b] = d;
return;
}
matrix<t, s1, s2> operator+(const matrix<t, s1, s2>& s)
{
matrix<t, s1, s2> q;
for (int i = 0;i < s1; i++)
{
for (int j = 0;j < s2; j++)
{
q.a[i][j] = a[i][j] + s.a[i][j];
}
}
return q;
}
template<int p>
matrix<t, s1, s2 + p> operator|(const matrix<t, s1, p>& s)
{
matrix<t, s1, s2 + p> q;
for (int i = 0;i < s1; i++)
{
for (int j = 0;j < (s2 + p); j++)
{
if (j < s2)
{
q.a[i][j] = a[i][j];
}
else q.a[i][j] = s.a[i][j - s2];
}
}
return q;
}
matrix<t, s1, s2> povorot(void)
{
matrix<t, s1, s2> q;
for (int i = 0; i<s1;i++)
{
for (int j = 0; j < s2;j++)
{
q.a[j][s1 - 1 - i] = a[i][j];
}
}
return q;
}
class iterator
{
private:
t** m;
int stroki, stolbcu;
public:
typedef ptrdiff_t difference_type;
typedef t value_type;
typedef t* pointer;
typedef t& reference;
typedef unsigned sizet_type;
typedef std::forward_iterator_tag iterator_category;
iterator(t** a, int b, int c) : m(a), stroki(b),stolbcu(c) {}
iterator& operator++()
{
stolbcu += 1;
if (stolbcu == s2)
{
stolbcu = 0;
stroki += 1;
}
return *this;
}
t& operator*()
{
return m[stroki][stolbcu];
}
t& operator->()
{
return &(m[stroki][stolbcu]);
}
friend bool operator!=(iterator& q, iterator& w)
{
return &(*q) != &(*w);
}
friend bool operator==(iterator& q, iterator& w)
{
return &(*q) != &(*w);
}
};
iterator begin()
{
return iterator(a, 0, 0);
}
iterator end()
{
return iterator(a, s1, 0);
}
template<typename t, int s1, int s2>
friend ostream& operator << (ostream& out, matrix<t, s1, s2> const& a);
template<typename t, int s1, int s2>
friend istream& operator >> (istream& in, matrix<t, s1, s2>& a);
};
template<typename t, int s1, int s2>
ostream& operator << (ostream& out, matrix<t,s1,s2> const& a)
{
for (int i = 0;i < s1; i++)
{
for (int j = 0;j < s2; j++)
{
out << ' ' << a.a[i][j];
}
out << endl;
}
return out;
}
template<typename t, int s1, int s2>
istream& operator >> (istream& in, matrix<t, s1, s2>& a)
{
string el;
//in >> el;
//stringstream ss;
//ss << el;
for (int i = 0;i < s1; i++)
{
for (int j = 0;j < s2; j++)
{
in >> el;
stringstream ss;
ss << el;
ss >> a.a[i][j];
}
}
return in;
}
bool les(element& a)
{
return (a < 30);
}
bool kx(element& a)
{
return ((2 * a + (-4)) == 0);
}
void pl(element& a)
{
a = a + 4;
return;
}
int main()
{
srand(time(NULL));
element** a;
a = new element*[5];
for (int i = 0;i < 5; i++)
{
a[i] = new element[5];
}
for (int i = 0;i < 5; i++)
{
for (int j = 0;j < 5; j++)
{
a[i][j] = rand();
}
}
matrix<element, 5, 5> b(a);
cout << "prost" << endl << b;
b = b + b;
cout << "plus" << endl << b;
for (auto i = b.begin();i != b.end();++i)
{
cout << '!' << *i;
}
cout << endl;
int l;
for_each(b.begin(), b.end(), pl);//+4
cout << b;
l = count(b.begin(), b.end(), 30);
cout << l << ' ';
l = count_if(b.begin(), b.end(), kx);
cout << l << ' ';
l = count_if(b.begin(), b.end(), les);
cout << l << ' ';
auto n = find(b.begin(), b.end(), 20);
if(n!=b.end())
cout << "est " << *n;
else cout << "net";
system("pause");
return 0;
}