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
| /*
Оператор мобильной связи организовал базу данных абонентов, содержащую сведения
о телефонах, их владельцах и используемых тарифах, в виде бинарного дерева.
Составьте программу, которая:
обеспечивает начальное формирование базы данных в виде бинарного дерева;
производит вывод всей базы данных;
производит поиск владельца по номеру телефона;
выводит наиболее востребованный тариф (по наибольшему числу абонентов).
*/
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <map>
#include <string>
#include <windows.h>
///////////////////////////////////////////////////////////////////////////////
struct T_subscriber_node;
///////////////////////////////////////////////////////////////////////////////
typedef std::string T_str;
typedef T_subscriber_node* T_subscriber_tree;
typedef std::map <T_str, int > T_count_of_tariff;
///////////////////////////////////////////////////////////////////////////////
struct T_subscriber
{
//-------------------------------------------------------------------------
T_str phone_num_;
T_str name_;
T_str tariff_;
T_str phone_num_title_;
T_str name_title_;
T_str tariff_title_;
//-------------------------------------------------------------------------
T_subscriber()
:
phone_num_title_ ( "номер телефона\t: " ),
name_title_ ( "имя\t\t: " ),
tariff_title_ ( "тариф\t\t: " )
{}
//-------------------------------------------------------------------------
bool operator< ( T_subscriber const & subscriber ) const
{
return phone_num_ < subscriber.phone_num_;
}
//-------------------------------------------------------------------------
void input()
{
std::cout << std::endl
<< "Введите данные абонента:"
<< std::endl;
input_phone();
std::cout << '\t' << name_title_;
std::cin >> name_;
std::cout << '\t' << tariff_title_;
std::cin >> tariff_;
}
//-------------------------------------------------------------------------
void input_phone()
{
std::cout << '\t' << phone_num_title_;
std::cin >> phone_num_;
}
//-------------------------------------------------------------------------
void print() const
{
std::cout << phone_num_title_ << phone_num_ << std::endl
<< name_title_ << name_ << std::endl
<< tariff_title_ << tariff_ << std::endl;
}
//-------------------------------------------------------------------------
};
///////////////////////////////////////////////////////////////////////////////
struct T_subscriber_node
{
//-------------------------------------------------------------------------
T_subscriber subscriber_;
T_subscriber_tree left_;
T_subscriber_tree right_;
//-------------------------------------------------------------------------
T_subscriber_node( T_subscriber const & subscriber )
:
subscriber_ ( subscriber ),
left_ (),
right_ ()
{}
//-------------------------------------------------------------------------
};
///////////////////////////////////////////////////////////////////////////////
class T_subscribers
{
//-------------------------------------------------------------------------
T_subscriber_tree subscriber_tree_;
//-------------------------------------------------------------------------
public:
//-------------------------------------------------------------------------
T_subscribers()
:
subscriber_tree_()
{}
//-------------------------------------------------------------------------
void add_subscriber()
{
T_subscriber subscriber;
subscriber.input();
auto bool_res = successfully_insert_subscriber_in_tree
(
subscriber,
subscriber_tree_
);
std::cout << (
bool_res
? "Абонент успешно добавлен в базу."
: "Абонент с таким номером телефона уже существует."
)
<< std::endl;
}
//-------------------------------------------------------------------------
void print_all_subscribers() const
{
if( !subscriber_tree_ )
{
std::cout << "База абонентов пуста."
<< std::endl;
}
else
{
std::cout << "Все абоненты базы:"
<< std::endl;
int counter = 0;
print_tree
(
subscriber_tree_,
counter
);
}//else
}
//-------------------------------------------------------------------------
void find_subscriber_by_phone_num() const
{
T_subscriber subscriber_with_phone_num;
std::cout << "Введите для поиска ";
subscriber_with_phone_num.input_phone();
auto phone_num_tree = get_tree_with_phone_num
(
subscriber_tree_,
subscriber_with_phone_num
);
if( !phone_num_tree )
{
std::cout << "Абонента с таким номером телефона не существует."
<< std::endl;
}
else
{
std::cout << "Абонент с этим номером телефона найден:"
<< std::endl;
phone_num_tree->subscriber_.print();
}
}
//-------------------------------------------------------------------------
void find_most_popular_tariff() const
{
T_count_of_tariff count_of_tariff;
int popular_tariff_count{};
T_str popular_tariff;
find_popular_tariff_in_tree
(
count_of_tariff,
popular_tariff_count,
popular_tariff,
subscriber_tree_
);
if( !popular_tariff_count )
{
std::cout << "Тариф не найден."
<< std::endl;
}
else
{
std::cout << "Самый популярный тариф у абонентов "
<< popular_tariff
<< "."
<< std::endl;
}
}
//-------------------------------------------------------------------------
private:
//-------------------------------------------------------------------------
bool successfully_insert_subscriber_in_tree
(
T_subscriber const & subscriber,
T_subscriber_tree & subscriber_tree
)
{
if( !subscriber_tree )
{
subscriber_tree = new T_subscriber_node( subscriber );
return true;
}
else if (
subscriber < subscriber_tree->subscriber_
)
{
return successfully_insert_subscriber_in_tree
(
subscriber,
subscriber_tree->left_
);
}
else if (
subscriber_tree->subscriber_ < subscriber
)
{
return successfully_insert_subscriber_in_tree
(
subscriber,
subscriber_tree->right_
);
}
else
{
return false;
}
}
//-------------------------------------------------------------------------
void print_tree
(
T_subscriber_tree subscriber_tree,
int counter
) const
{
if( subscriber_tree )
{
print_tree
(
subscriber_tree->left_,
counter
);
std::cout << std::endl
<< "Абонент № "
<< ++counter
<< std::endl;
subscriber_tree->subscriber_.print();
print_tree
(
subscriber_tree->right_,
counter
);
}//if
}
//-------------------------------------------------------------------------
T_subscriber_tree get_tree_with_phone_num
(
T_subscriber_tree subscriber_tree,
T_subscriber const & subscriber_with_phone_num
) const
{
if( !subscriber_tree )
{
return subscriber_tree;
}
else
{
if (
subscriber_with_phone_num
< subscriber_tree->subscriber_
)
{
return get_tree_with_phone_num
(
subscriber_tree->left_,
subscriber_with_phone_num
);
}
else if (
subscriber_tree->subscriber_
< subscriber_with_phone_num
)
{
return get_tree_with_phone_num
(
subscriber_tree->right_,
subscriber_with_phone_num
);
}
else
{
return subscriber_tree;
}
}//else
}
//-------------------------------------------------------------------------
void find_popular_tariff_in_tree
(
T_count_of_tariff & count_of_tariff,
int & popular_tariff_count,
T_str & popular_tariff,
T_subscriber_tree subscriber_tree
) const
{
if( subscriber_tree )
{
auto & tariff_cur = subscriber_tree->subscriber_.tariff_;
auto count_cur = ++count_of_tariff[ tariff_cur ];
if( count_cur > popular_tariff_count )
{
popular_tariff_count = count_cur;
popular_tariff = tariff_cur;
}
find_popular_tariff_in_tree
(
count_of_tariff,
popular_tariff_count,
popular_tariff,
subscriber_tree->left_
);
find_popular_tariff_in_tree
(
count_of_tariff,
popular_tariff_count,
popular_tariff,
subscriber_tree->right_
);
}//if
}
//-------------------------------------------------------------------------
};
///////////////////////////////////////////////////////////////////////////////
enum class T_action
{
ADD_SUBSCRIBER = 1,
PRINT_ALL_SUBSCRIBERS,
FIND_SUBSCRIBER_BY_PHONE_NUM,
FIND_MOST_POPULAR_TARIFF,
EXIT
};
///////////////////////////////////////////////////////////////////////////////
void print_menu()
{
std::cout << std::endl
<< "Выберите действие:"
<< std::endl;
std::cout
<< '\t' << int( T_action::ADD_SUBSCRIBER )
<< '\t' << "добавить абонента" << std::endl
<< '\t' << int( T_action::PRINT_ALL_SUBSCRIBERS )
<< '\t' << "напечатать данные всех абонентов" << std::endl
<< '\t' << int( T_action::FIND_SUBSCRIBER_BY_PHONE_NUM )
<< '\t' << "найти абонента по номеру телефона" << std::endl
<< '\t' << int( T_action::FIND_MOST_POPULAR_TARIFF )
<< '\t' << "напечатать самый популярный тариф" << std::endl
<< '\t' << int( T_action::EXIT )
<< '\t' << "выйти из программы" << std::endl;
}
///////////////////////////////////////////////////////////////////////////////
T_action input_and_get_action()
{
std::cout << "Введите номер действия: ";
int int_action;
std::cin >> int_action;
return T_action( int_action );
}
///////////////////////////////////////////////////////////////////////////////
int main()
{
SetConsoleCP (1251);
SetConsoleOutputCP (1251);
T_subscribers subscribers;
for(;;)
{
print_menu();
auto action = input_and_get_action();
if( action == T_action::EXIT )
{
break;
}
switch( action )
{
case T_action::ADD_SUBSCRIBER :
subscribers.add_subscriber();
break;
case T_action::PRINT_ALL_SUBSCRIBERS :
subscribers.print_all_subscribers();
break;
case T_action::FIND_SUBSCRIBER_BY_PHONE_NUM :
subscribers.find_subscriber_by_phone_num();
break;
case T_action::FIND_MOST_POPULAR_TARIFF :
subscribers.find_most_popular_tariff();
break;
default:
;
}
}//for
system("pause");
} |