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
| #include <iostream>
#include <iterator>
using namespace std;
struct LinkedListNodeBase {
LinkedListNodeBase* prev;
LinkedListNodeBase* next;
LinkedListNodeBase() noexcept : prev(this), next(this) {}
LinkedListNodeBase(LinkedListNodeBase* before) noexcept : prev(before->prev), next(before) {
prev->next = next->prev = this;
}
virtual ~LinkedListNodeBase() noexcept {
prev->next = next;
next->prev = prev;
}
};
template<typename T>
struct LinkedListNode : public LinkedListNodeBase {
T value;
LinkedListNode(const T& value, LinkedListNodeBase* after) : LinkedListNodeBase(after), value(value) {}
};
template<typename T>
struct LinkedListIterator {
using Self = LinkedListIterator<T>;
using Node = LinkedListNode<T>;
using NodeBase = LinkedListNodeBase;
// для совместимости с stl
typedef ptrdiff_t difference_type;
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
NodeBase* node;
LinkedListIterator() : node() {}
LinkedListIterator(NodeBase* node) : node(node) {}
reference operator*() const {
return static_cast<Node*>(node)->value;
}
pointer operator->() const {
return &static_cast<Node*>(node)->value;
}
Self& operator++() {
node = node->next;
return *this;
}
Self operator++(int) {
Self result(*this);
node = node->next;
return result;
}
Self& operator--() {
node = node->prev;
return *this;
}
Self operator--(int) {
Self result(*this);
node = node->prev;
return result;
}
bool operator==(const Self& other) const {
return node == other.node;
}
bool operator!=(const Self& other) const {
return node != other.node;
}
};
template<typename T>
struct ConstLinkedListIterator {
using Self = ConstLinkedListIterator<T>;
using Node = const LinkedListNode<T>;
using NodeBase = const LinkedListNodeBase;
using Iterator = LinkedListIterator<T>;
// для совместимости с stl
typedef ptrdiff_t difference_type;
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
NodeBase* node;
ConstLinkedListIterator() : node() {}
ConstLinkedListIterator(NodeBase* node) : node(node) {}
ConstLinkedListIterator(const Iterator& iterator) : node(iterator.node) {}
reference operator*() const {
return static_cast<Node*>(node)->value;
}
pointer operator->() const {
return &static_cast<Node*>(node)->value;
}
Self& operator++() {
node = node->next;
return *this;
}
Self operator++(int) {
Self result(*this);
node = node->next;
return result;
}
Self& operator--() {
node = node->prev;
return *this;
}
Self operator--(int) {
Self result(*this);
node = node->prev;
return result;
}
bool operator==(const Self& other) const {
return node == other.node;
}
bool operator!=(const Self& other) const {
return node != other.node;
}
};
template<typename T>
class LinkedList {
public:
using Node = LinkedListNode<T>;
using Iterator = LinkedListIterator<T>;
using ConstIterator = ConstLinkedListIterator<T>;
LinkedList() : base() {}
LinkedList(const LinkedList<T>&) = delete;
LinkedList<T>& operator=(const LinkedList<T>&) = delete;
~LinkedList() {
clear();
}
bool isEmpty() const {
return base.next == &base;
}
void clear() {
while (!isEmpty()) {
delete base.next;
}
}
Iterator begin() {
return Iterator(base.next);
}
Iterator end() {
return Iterator(&base);
}
ConstIterator begin() const {
return ConstIterator(base.next);
}
ConstIterator end() const {
return ConstIterator(&base);
}
// inserts value before position
Iterator insert(ConstIterator position, const T& value) {
return new Node(value, const_cast<LinkedListNodeBase*>(position.node));
}
Iterator erase(ConstIterator position) {
Iterator result = position.node->next;
delete position.node;
return result;
}
private:
LinkedListNodeBase base;
};
template<typename Iterator>
size_t distance(Iterator begin, Iterator end) {
size_t result = 0;
for (; begin != end; ++begin, ++result);
return result;
}
template<typename Iterator>
void advance(Iterator& iterator, size_t n) {
while (n--) {
++iterator;
}
}
template<typename Iterator, typename T, typename Comparator>
Iterator lowerBound(Iterator begin, Iterator end, const T& value, Comparator compare) {
size_t count = ::distance(begin, end);
while (count > 0) {
size_t half = count / 2;
Iterator middle = begin;
advance(begin, half);
if (compare(*middle, value)) {
begin = ++middle;
count -= half + 1;
}
else {
count = half;
}
}
return begin;
}
class Znak {
public:
Znak() : surname(), name(), zodiac(), dob() {}
Znak(const string& surname, const string& name, const string& zodiac, int day, int month, int year)
: surname(surname), name(name), zodiac(zodiac), dob{ day, month, year } {}
const string& getSurname() const {
return surname;
}
const string& getName() const {
return name;
}
const string& getZodiac() const {
return zodiac;
}
int getDay() const {
return dob[0];
}
int getMonth() const {
return dob[1];
}
int getYear() const {
return dob[2];
}
private:
string surname;
string name;
string zodiac;
int dob[3];
};
bool operator==(const Znak& a, const Znak& b) {
return a.getSurname() == b.getSurname() && a.getName() == b.getName() && a.getZodiac() == b.getZodiac() &&
a.getDay() == b.getDay() && a.getMonth() == b.getMonth() && a.getYear() == b.getYear();
}
bool isLessByZodiac(const Znak& a, const Znak& b) {
return a.getZodiac() < b.getZodiac();
}
istream& operator>>(istream& in, Znak& znak) {
string surname, name, zodiac;
int day, month, year;
in >> surname >> name >> zodiac >> day >> month >> year;
znak = { surname, name, zodiac, day, month, year }; // воспользуюсь оператором присваивания
return in;
}
ostream& operator<<(ostream& out, const Znak& znak) {
return out << "name=" << znak.getName()
<< ", surname=" << znak.getSurname()
<< ", zodiac=" << znak.getZodiac()
<< ", dob=" << znak.getDay() << "." << znak.getMonth() << "." << znak.getYear();
}
template<typename T>
ostream& operator<<(ostream& out, const LinkedList<T>& list) {
for (auto i = list.begin(), end = list.end(); i != end;) {
out << *i;
if (++i != end) {
out << "\n";
}
}
return out;
}
template<typename Iterator, typename OutputIterator, typename Predicate>
OutputIterator copyIf(Iterator begin, Iterator end, OutputIterator output, Predicate predicate) {
for (; begin != end; ++begin) {
if (predicate(*begin)) {
*output = *begin;
++output;
}
}
return output;
}
template<typename First, typename Second>
struct Pair {
First first;
Second second;
};
template<typename F, typename S>
ostream& operator<<(ostream& out, const Pair<F, S>& p) {
return out << "{" << p.first << ", " << p.second << "}";
}
template<typename Iterator, typename Comparator>
Iterator maxElement(Iterator begin, Iterator end, Comparator compare) {
if (begin == end) {
return end;
}
Iterator result = begin++;
for (; begin != end; ++begin) {
if (compare(*begin, *result)) {
result = begin;
}
}
return result;
}
void menu() {
cout << "0. Exit" << "\n";
cout << "1. Input" << "\n";
cout << "2. Output " << "\n";
cout << "3. Output man k " << "\n";
cout << "4. Output max znak" << '\n';
cout << "5. Delete element k" << '\n';
cout << "6. Redact element k" << '\n';
cout << "7. Add element k" << '\n';
cout << "/////////////////////////////////////////////////////////" << "\n";
}
int main() {
LinkedList<Znak> znakList;
Znak znak;
LinkedList<Pair<string, size_t>> groupedBySign;
bool f = false, fl = false;
int choise = 10, n;
while (choise) {
menu(); cin >> choise;
switch (choise) {
case 0:
f = true;
cout << "Exit" << "\n";
break;
case 1:
cout << "Input" << "\n";
cin >> n;
while (n--) {
cin >> znak;
// позиция для вставки
auto position = lowerBound(znakList.begin(), znakList.end(), znak, isLessByZodiac);
// вставка в позицию
znakList.insert(position, znak);
}
fl = true;
break;
case 2:
cout << "Output\n";
if (fl) cout << "All:" << endl << znakList << endl;
else cout << "Input dannie" << "\n";
break;
case 3:
cout << "Output man k\n";
if (fl) {
string surname; cin >> surname;
cout << "All with surname " << surname << endl;
copyIf(znakList.begin(), znakList.end(), ostream_iterator<Znak>(cout, "\n"),
[&surname](const Znak& znak) { return znak.getSurname() == surname; });
}
else
cout << " Dannie deleted" << '\n';
break;
case 4:
cout << " Output max znak\n";
if (fl) {
// подсчёт людей по знакам зодиака
for (auto& z : znakList) {
auto position = lowerBound(groupedBySign.begin(), groupedBySign.end(), z.getZodiac(),
[](auto& pair, auto& value) { return pair.first < value; });
if ((position != groupedBySign.end()) && position->first == z.getZodiac()) {
++(position->second);
}
else {
groupedBySign.insert(position, { z.getZodiac(), 1 });
}
}
cout << "People count by sign:" << endl << groupedBySign << endl;
if (groupedBySign.isEmpty()) {
cout << "No people." << endl;
}
else {
auto max = maxElement(groupedBySign.begin(), groupedBySign.end(),
[](auto& a, auto& b) { return a.second > b.second; });
cout << "Maximum people for sign:" << endl << *max << endl;
}
}
break;
case 5:
cout << "Delete man k\n";
if (fl) {
}
else cout << "Etih dannih i tak ne bilo \n";
case 6:
cout << "Redact element k\n";
if (fl) {
}
else cout << "Dannih net\n";
default:
cout << "Exit" << "\n"; f = true;
break;
} cout << "/////////////////////////////////////////////////////////" << "\n";
if (f) break;
} return 0;
} |