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
| #include <iostream>
#include <string.h>
using namespace std;
struct Record
{
char Name[20];
char LastName[20];
unsigned int PhoneNumber;
char Address[50];
};
Record *AddStruct(Record* Obj, const int size);
void AddRecord(Record* Obj, const int size);// - добавление записи в книгу
void PrintBook(Record* Obj, const int size);// - печать всей книги
void PrintRecord(Record* Obj, int num);// - печать одной записи
void PrintMenu(); //- печать меню
int FindRecordByName(Record* Obj, const int size);// - поиск записи по имени, возвращает номер элемента
int FindRecordByLastName(Record* Obj, const int size);// - поиск записи по фамилии, возвращает номер элемента
int FindRecordByNumber(Record* Obj, const int size);// - поиск записи по номеру телефона, возвращает номер элемента
Record *AfterDel(Record* Obj, const int size, int num);// - удаление записи по номеру элемента, возвращает массив структур
void EditRecord(Record* Obj, const int size, int num);// - редактирование записи по номеру
void main()
{
Record* AllRecords = NULL;
int NumRec = 0;
bool AddMore = 0;
do
{
AllRecords = AddStruct(AllRecords, NumRec);
AddRecord(AllRecords, NumRec);
NumRec++;
cout << "Add more records to the Address Book?\n\tPress \"1\" to add more,\n\tPress \"0\" to finish;\n";
cin >> AddMore;
} while (AddMore != 0);
system("cls");
PrintBook(AllRecords, NumRec);
PrintMenu();
int Menu;
do
{
cin >> Menu;
cout << endl;
int Num = -1;
switch (Menu)
{
default:
system("cls");
cout << "Option not found! Try again!\n\n";
PrintMenu();
break;
case 1://Печать всей книги
{
system("cls");
PrintBook(AllRecords, NumRec);
PrintMenu();
}
break;
case 2://Поиск по имени
{
system("cls");
cout << "\n\tSEARCH BY NAME:\n";
int sRes = FindRecordByName(AllRecords, NumRec);
if (sRes >= 0)
PrintRecord(AllRecords, sRes);
else
cout << "Record not Found!\n\n";
PrintMenu();
}
break;
case 3://Поиск по фамилии
{
system("cls");
cout << "\n\tSEARCH BY LAST NAME:\n";
int sRes = FindRecordByLastName(AllRecords, NumRec);
if (sRes >= 0)
PrintRecord(AllRecords, sRes);
else
cout << "Record not Found!\n\n";
PrintMenu();
}
break;
case 4://Поиск по номеру телефона
{
system("cls");
cout << "\n\tSEARCH BY PHONE NUMBER:\n";
int sRes = FindRecordByNumber(AllRecords, NumRec);
if (sRes >= 0)
PrintRecord(AllRecords, sRes);
else
cout << "Record not Found!\n\n";
PrintMenu();
}
break;
case 5: //Сортировка по Имени
system("cls");
cout << "sort by name\n";
PrintMenu();
break;
case 6://Сортировка по фамилии
system("cls");
cout << "sort byLastName\n";
PrintMenu();
break;
case 7://Удаление записи по имени
{
system("cls");
cout << "\n\tSearch for record to delete:\n";
int sRes = FindRecordByName(AllRecords, NumRec);
if (sRes >= 0)
{
PrintRecord(AllRecords, sRes);
AllRecords = AfterDel(AllRecords, NumRec, sRes);
NumRec--;
cout << "Record has been deleted!\n\n\n";
}
else
cout << "Record not Found!\n\n";
}
PrintMenu();
break;
case 8://Редактирование записи
{
system("cls");
cout << "\n\tSEARCH BY NAME:\n";
int sRes = FindRecordByName(AllRecords, NumRec);
if (sRes >= 0)
{
PrintRecord(AllRecords, sRes);
EditRecord(AllRecords, NumRec, sRes); //Ломает все!
PrintRecord(AllRecords, sRes);
}
else
cout << "Record not Found!\n\n";
PrintMenu();
}
break;
case 9:
{
bool AddMore = 0;
do
{
AllRecords = AddStruct(AllRecords, NumRec);
AddRecord(AllRecords, NumRec);
NumRec++;
cout << "\n\tPress \"1\" to add more records,\n\tPress \"0\" to finish;\n";
cin >> AddMore;
} while (AddMore != 0);
system("cls");
PrintBook(AllRecords, NumRec);
PrintMenu();
}
break;
case 10:
system("cls");
cout << "\n\t\t\tThank You!\n\n\n";
break;
}
} while (Menu != 10);
}
Record *AddStruct(Record* Obj, const int size)
{
if (size == 0)
{
Obj = new Record[size + 1];
}
else
{
Record* tempObj = new Record[size + 1];
for (int i = 0; i < size; i++)
{
tempObj[i] = Obj[i];
}
delete[] Obj;
Obj = tempObj;
}
return Obj;
}
void AddRecord(Record* Obj, int size)
{
char name[20];
char lastName[20];
unsigned int phoneNumber;
char address[50];
cout << "New Record \n~~~~~~~~~~~~~" << endl;
cout << "Name:\t\t";
cin >> name;
cout << "Last Name:\t";
cin >> lastName;
cout << "Phone Number:\t";
cin >> phoneNumber;
cout << "Address:\t";
cin >> address;
strcpy_s(Obj[size].Name, 19, name);
strcpy_s(Obj[size].LastName, 19, lastName);
Obj[size].PhoneNumber = phoneNumber;
strcpy_s(Obj[size].Address, 19, address);
cout << "_________________________________________________________" << endl;
}
void PrintBook(Record* Obj, int size)
{
cout << "\n\tYOUR ADDRESS BOOK\n\t***************" << endl;
if (size==0)
cout << "Nothing to Show!";
else
{
for (int i = 0; i < size; i++)
{
cout << "Record " << i + 1 << ':' << endl;
cout << "\tName:\t\t" << Obj[i].Name << endl;
cout << "\tLast Name:\t" << Obj[i].LastName << endl;
cout << "\tPhone Number:\t" << Obj[i].PhoneNumber << endl;
cout << "\tAddress:\t" << Obj[i].Address << endl;
cout << "\t_________________________________________________________" << endl << endl;
}
}
}
void PrintRecord(Record* Obj, int num)
{
cout << "Record you have searched for:" << endl;
cout << "\tName:\t\t" << Obj[num].Name << endl;
cout << "\tLast Name:\t" << Obj[num].LastName << endl;
cout << "\tPhone Number:\t" << Obj[num].PhoneNumber << endl;
cout << "\tAddress:\t" << Obj[num].Address << endl;
cout << "\t_________________________________________________________" << endl << endl;
}
void PrintMenu()
{
cout << "\tMENU\n\t~~~~~" << endl;
cout << "1\tTo display all book - \tpress 1;\n";
cout << "2\tTo find record by Name - \tpress 2;\n";
cout << "3\tTo find record by Last Name - \tpress 3;\n";
cout << "4\tTo find record by Phone Number - \tpress 4;\n";
cout << "5\tTo sort records by Name and display - \tpress 5;\n";
cout << "6\tTo sort records by Last Name and display - \tpress 6;\n";
cout << "7\tTo find a record by Name and delete it - \tpress 7;\n";
cout << "8\tTo find a record by Name and to edit it - \tpress 8;\n";
cout << "9\tTo add more records to AddressBook - \tpress 9;\n";
cout << "10\tTo finish work with AddressBook - \tpress 10;\n\n\n";
}
int FindRecordByName(Record* Obj, const int size)
{
cout << "Enter the Name for search:\n";
char sName[20];
int NumFound = -1;
cin >> sName;
for (int i = 0; i < size; i++)
{
if (strcmp(Obj[i].Name, sName) == 0)
{
NumFound = i;
break;
}
else continue;
}
return NumFound;
}
int FindRecordByLastName(Record* Obj, const int size)
{
cout << "Enter the Last Name for search:\n";
char lName[20];
int NumFound = -1;
cin >> lName;
for (int i = 0; i < size; i++)
{
if (strcmp(Obj[i].LastName, lName) == 0)
{
NumFound = i;
break;
}
else continue;
}
return NumFound;
}
int FindRecordByNumber(Record* Obj, const int size)
{
cout << "Enter the Phone Number for search:\n";
unsigned int PhNum;
int NumFound = -1;
cin >> PhNum;
for (int i = 0; i < size; i++)
{
if (Obj[i].PhoneNumber == PhNum)
{
NumFound = i;
break;
}
else continue;
}
return NumFound;
}
Record *AfterDel(Record* Obj, const int size, int num)
{
Record* tempObj = new Record[size -1];
for (int i = 0; i < num; i++)
{
tempObj[i] = Obj[i];
}
for (int i = num; i < size-1; i++)
{
tempObj[i] = Obj[i+1];
}
delete[] Obj;
Obj = tempObj;
return Obj;
};
void EditRecord(Record* Obj, const int size, int num)
{
char name[20]="0";
char lastName[20] = "0";
unsigned int phoneNumber;
char address[50] = "0";
cout << "Insert New Name if you want to change it (or insert '0'):\n";
cin>>name;
if (name != "0")
{
strcpy_s(Obj[size].Name, 19, name);
}
cout << "\nInsert New Last Name if you want to change it (or insert '0'):\n";
cin >> lastName;
if (lastName !="0" )
{
strcpy_s(Obj[size].LastName, 19, lastName);
}
cout << "\nInsert New Phone Number if you want to change it (or insert '0'):\n";
cin >> phoneNumber;
if (phoneNumber !=0)
{
Obj[size].PhoneNumber = phoneNumber;
}
cout << "\nInsert New Address if you want to change it (or insert '0'):\n";
cin >> address;
if (address !="0")
{
strcpy_s(Obj[size].Address, 19, address);
}
} |