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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
| #include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <string.h>
#include <iostream>
#include <windows.h>
#include <math.h>
#include <TIME.H>
#include <SYS\TYPES.H>
#include <locale>
using namespace std;
// Создаем структуру
const int STR_SIZE =20;
struct record
{ char PIB[STR_SIZE];
char Adresa[STR_SIZE];
char NomTel[STR_SIZE];
int Tarif;
int Vartist;};
//Структура закончена
//function prototypes
int ZastBeg();
void ProsmotrBD();
void CreateBD();
void AddRecord();
void AddRecord(const record & r);
void LoadBD(); //Загрузка базы данных
void SaveBD();
int ChooseRecord();//Выбор записи
void EditRecord();//Изменение записи
void DeleteRecord();
void FindRecord();
void SortBD();
int Exit();
void ZastEnd();// Заставка досвидания
int Get(int min, long max);//Мин. Заставка (1-9999)
//Globals
static record * records = 0;//Операция record *record=0
static long int nrecords = 0;
const int menupuncts = 10;
static char menu[menupuncts][30] = {"Просмотр базы данных",
"Добавить запись",
"Создать базу данных",
"Загрузить базу данных",
"Сохранить базу данных",
"Изменить запись",
"Удалить запись",
"Сортировка базы данных",
"Поиск записи",
"Выход" };
const int sortmenupuncts = 6;
static char sortmenu[sortmenupuncts][30] = {"Сортировка по имени и фамилии",
"Сортировка по адресу",
"Сортировка по номеру телефона",
"Сортировка по тарифу",
"Сортировка по стоимости",
"Вернутся в меню" };
//MENU
int YesNoDialog() //Вы уверены? Да, нет
{ printf("\n<Enter> - да, <Esc> - нет");
int c;
do
{c = getch();
}while(c != 13 && c != 27);
return c;}
int Menu()
{ int choice = 0;
int loop = 1;
while(loop == 1)
{ system("cls");
for(int i=0;i<menupuncts;++i)
{ if(i == choice)
printf("\n >%s< ", menu[i]);
else
printf("\n %s", menu[i]);}
printf("\n\nНажмите <Enter> для выбора действия");
int c = getch();
switch(c)
{ case 72: --choice;break;
case 80: ++choice;break;
case 13: loop = 0;break;
default:break; }
if(choice < 0) choice = menupuncts - 1;
if(choice >= menupuncts) choice = 0;}
return choice;}
void pos( int column, int line)
{
COORD coord;
coord.X = column;
coord.Y =line ;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord);
}
int ZastBeg()//ZAST_BEG
{system("color 12" );
pos(20,10);
printf("Курсовая Работа ");
pos(20,11);
printf("Учет абонентов телефонной сети");
pos(20,12);
printf("Выполнил курсант группы 3-037 Буховец Антон");
int c = getch(); system("cls");
system("color 07" );
return c; }
void ZastEnd()//Заставка Конец
{ system("cls");
printf("Досвидания");
getch();}
int Exit()//Vixod
{ system("cls");
printf("\n Вы уверены что хотите выйти?");
return YesNoDialog();}
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);// Указываем на каком языке будет курсовая
if(ZastBeg() != 27)
{ int loop = 1;
while(loop == 1)
{ int p = Menu();
switch(p)
{ case 0: ProsmotrBD(); break;
case 1: AddRecord(); break;
case 2: CreateBD(); break;
case 3: LoadBD(); break;
case 4: SaveBD(); break;
case 5: EditRecord(); break;
case 6: DeleteRecord(); break;
case 7: SortBD(); break;
case 8: FindRecord(); break;
case 9: if(Exit() != 27) loop = 0; break; }}}
else ZastEnd();
return 0;}
static char filename[32]; //FileDialog
char * FileDialog()
{ system("cls");
printf("Введите имя файла");
fscanf(stdin, "%s", filename);
return filename;}
void InputRecord(record & rec) //Vvod zapisi
{ system("cls");
int c;
if(kbhit())getch ();
rec.PIB[0] = 0;
rec.Adresa[0] = 0;
rec.NomTel[0] = 0;
do{ printf("\nВведите имя и фамилию :");
while(rec.PIB[0] == 0)gets(rec.PIB);
printf("\nВведите адресс :");
while(rec.Adresa[0] == 0)gets(rec.Adresa);
printf("\nВведите номер телефона :");
while(rec.NomTel[0] == 0)gets(rec.NomTel);
printf("\nВведите тариф :");
rec.Tarif = Get(1, 999);
printf("\nВведите стоимость :");
rec.Vartist = Get(1, 9999);
printf("\nВы уверены?");
c = YesNoDialog();
}while(c != 13);}
void AddRecord(const record & r) //DobavlenieRecord
{ ++nrecords;
record * newrecords = new record[nrecords];
if(records) {
memcpy(newrecords, records, (nrecords - 1) * sizeof(record));}
delete[] records;
records = newrecords;
records[nrecords - 1] = r;}
void AddRecord()
{ record r;
InputRecord(r);
AddRecord(r);}
int IsInString(const char *string, const char *search_for) //poisk zapisi
{ int maxpos=0;
while(search_for[++maxpos]!=0);
int len=0;
while(string[++len]!=0); // Если длина не равно 0
for (int i=0; i<len; i++) // len - длина записи
{ int add=0;
while(string[i+add] == search_for[add])
{ if(add==maxpos-1)return 1;
++add;}}
return 0;}
int RecordsSimilar(const record & a,const record & b)
{ int result = 1;
if(b.PIB[0] != '-' && b.PIB[1] != 0)if(IsInString(a.PIB, b.PIB) == 0)result = 0; // Ставим минус если мы не хотим искать по данному критерию
if(b.Adresa[0] != '-' && b.Adresa[1] != 0)if(IsInString(a.Adresa, b.Adresa) == 0)result = 0;
if(b.NomTel[0] != '-' && b.NomTel[1] != 0)if(IsInString(a.NomTel, b.NomTel) == 0)result = 0;
if(b.Tarif != -1)if(a.Tarif != b.Tarif)result = 0;
if(b.Vartist != -1)if(a.Vartist != b.Vartist)result = 0;
return result;}
void FindRecord()
{ if(!records)
{ system("cls");
printf("\nНет записи, нажмите любую клавишу");
getch(); }
else{ system("cls");
record rec;
if(kbhit())getch ();
rec.PIB[0] = 0;
rec.Adresa[0] = 0;
rec.NomTel[0] = 0;
printf("Введите название заголовка записи для поиска по\n");
printf("\nВведите имя и Фамилию ,\nНажмите - если вы не хотите искать по этому критерию:");
while(rec.PIB[0] == 0)gets(rec.PIB);
printf("\nВведите адресс ,\nНажмите - если вы не хотите искать по этому критерию:");
while(rec.Adresa[0] == 0)gets(rec.Adresa);
printf("\nВведите номер телефона ,\nНажмите - если вы не хотите искать по этому критерию:");
while(rec.NomTel[0] == 0)gets(rec.NomTel);
printf("\nВведите тариф,\nНажмите - если вы не хотите искать по этому критерию:");
rec.Tarif = Get(-1, 900);
printf("\nВведите стоимость,\nНажмите - если вы не хотите искать по этому критерию:");
rec.Vartist = Get(-1, 90000);
record * oldrecords = records;
int oldnrecords = nrecords;
records = 0;
nrecords = 0;
int n = 0;
for(int i=0;i<oldnrecords;++i) //++i добавляем а потомполучаем значение. i++ получаем значение а потом добавляем
{if(RecordsSimilar(oldrecords[i], rec) == 1)
{ AddRecord(oldrecords[i]);
++n;}
} if(n>0) // 6 пунктов для записи Если поставить все - то выбьет ничего не найдено
{ printf("\n%d Запись найдена, нажмите любую клавишу для просмотра", n);
getch();
ProsmotrBD();
} else
{ printf("Ничего не найдено");
getch(); }
if(records)delete[] records;
records = oldrecords;
nrecords = oldnrecords;}}
void OutputRecord(record & rec) //Vivod zapisi
{ printf("%-17s%-17s%-17s%-12d%-10d", rec.PIB, rec.Adresa, rec.NomTel, rec.Tarif, rec.Vartist);}
void OutputTable(int start, int n) //Vivod tablici
{ if(!records)
{ system("cls");
printf("\nНет записи, нажмите любую клавишу");
getch();
} else
{ if(start<0)start = 0;
if(start + n > nrecords) n = nrecords - start; //Старт + кол-во больше новых записей ) н приравнивается к старым записям - стартовая
if(n>0)
{ printf("N:| Имя и фамилия | Адрес | Номер телефона | Тариф |Стоимость\n");
for(int i=start;i<start+n;++i)
{ printf("\n%-3d", (i % nrecords)+1);
OutputRecord(records[(i % nrecords)]);}}}}
int SortMenu()//SortirovkaMenu
{ int choice = 0;
int loop = 1;
while(loop == 1)
{ system("cls");
for(int i=0;i<sortmenupuncts;++i)
{ if(i == choice)
printf("\n >%s< ", sortmenu[i]); //menu
else
printf("\n %s", sortmenu[i]); }
printf("\n\nНажмите <Enter> для выбора действия");
int c = getch();
switch(c)
{ case 72: --choice;break;
case 80: ++choice;break;
case 13: loop = 0;break;
default:break; }
if(choice < 0) choice = sortmenupuncts - 1;
if(choice >= sortmenupuncts) choice = 0; }
return choice;}
static record c;
void swap(record & a, record & b) //Функция обмена для сортировки
{ c = a;
a = b;
b = c;}
void SortBD()
{ if(!records)
{ system("cls");
printf("\nНет записи, нажмите любую клавишу");
getch();
} else
{ int c = SortMenu();
switch(c)
{ case 0:
{ for(int i=0;i<nrecords;++i)
for(int k=0;k<nrecords-1;++k)
if(strcmp(records[k].PIB, records[k+1].PIB) > 0)swap(records[k], records[k+1]); // меняет записи местами
break;}
case 1:
{ for(int i=0;i<nrecords;++i)
for(int k=0;k<nrecords-1;++k)
if(strcmp(records[k].Adresa, records[k+1].Adresa) > 0)swap(records[k], records[k+1]);
break;
} case 2:
{ for(int i=0;i<nrecords;++i)
for(int k=0;k<nrecords-1;++k)
if(strcmp(records[k].NomTel, records[k+1].NomTel) > 0)swap(records[k], records[k+1]);
break;}
case 3:
{ for(int i=0;i<nrecords;++i)
for(int k=0;k<nrecords-1;++k)
if(records[k].Tarif > records[k+1].Tarif)swap(records[k], records[k+1]);
break;
} case 4:
{ for(int i=0;i<nrecords;++i)
for(int k=0;k<nrecords-1;++k)
if(records[k].Vartist > records[k+1].Vartist)swap(records[k], records[k+1]);
break;}}}}
void ProsmotrBD()//ProsmotrBD
{ if(!records)
{ system("cls");
printf("\nНет записи, нажмите любую клавишу");
getch();}
else
{ int loop = 1;
int n = 0;
while(loop == 1)
{ system("cls");
OutputTable(n, 20);
printf("\nНажмите <Esc> для возврата в меню.");
int c = getch();
switch(c)
{ case 72: --n;break;
case 80: ++n;break;
case 27: loop = 0;break;
default:break;}
if(n < 0) n = 0;
if(n >= nrecords - 10) n = nrecords - 10;}}}
int ChooseRecord()//ViborZapisi
{ int choice = -1;
if(!records)
{ system("cls");
printf("\nНет записи нажмите любую клавишу");
getch();}
else
{ int loop = 1;
int n = 0;
while(loop == 1)
{ system("cls");
printf("N:| Имя и фамилия | Адрес | Номер телефона | Тариф |Стоимость\n");
printf("\n%d", n + 1);
OutputRecord(records[n]);
printf("\n\nНажмите <Esc> для возврата в меню, <Enter> для выбора записи.");
int c = getch();
switch(c)
{ case 72: ++n;break;
case 80: --n;break;
case 27: loop = 0;break;
case 13: loop = 0;choice = n;break;
default:break;}
if(n < 0) n = nrecords - 1;
if(n >= nrecords) n = 0;}}
return choice;}
void EditRecord()//RedaktirovanieZapisi
{ int c = ChooseRecord();
if(c>=0 && c<nrecords)
{ printf("\n\nВы уверены что хотите перезаписать запись?");
if(YesNoDialog() == 13)
{InputRecord(records[c]);}}}
void DeleteRecord()//Ydalenie zapisi
{ int c = ChooseRecord();
if(c>=0 && c<nrecords)
{ printf("\n\nВы уверены что хотите удалить эту запись?");
if(YesNoDialog() == 13)
{ --nrecords;
if(nrecords <= 0)
{ delete[] records;
records = 0;}// нач запись =0 и 1 2 3 4 5
else{ record * newrecords = new record[nrecords];
int k = 0;
for(int i=0;i<nrecords+1;++i)
{if(i!=c) { newrecords[k] = records[i];
++k;}}
delete[] records;
records = newrecords;}}}}
void CreateBD()//SozdanieBD
{ if(records)
{ system("cls");
printf("База данных не сохранена, хотите сохранить её?");
if(YesNoDialog() == 13) SaveBD();
delete[] records;}
do{ printf("Введите новую запись :\n");
AddRecord();
printf("\n\nХотите продолжить?");}
while(YesNoDialog() !=27);}
void SaveBD()//SoxranenieBD
{ if(!records)
{ system("cls");
printf("\nНичего не сохранить, нажмите любую клавишу.");
getch();
return;}
FILE * m_fp=fopen(FileDialog(), "wb");
if(m_fp)
{ //Header
fwrite("Baza_dann",1,9,m_fp);
//Number of records
fwrite(&nrecords, 1, sizeof(long int), m_fp);
record * r = records; //Records
for(int i=0;i<nrecords;++i)
{ fwrite(&(r->PIB), 1, STR_SIZE - 1, m_fp);
fwrite(&(r->Adresa), 1,STR_SIZE - 1, m_fp);
fwrite(&(r->NomTel), 1,STR_SIZE - 1, m_fp);
fwrite(&(r->Tarif), 1, sizeof(long int), m_fp);
fwrite(&(r->Vartist), 1, sizeof(long int), m_fp);
++r;}
fclose(m_fp);}
else
{ printf("\nНевозможно создать файл!");
getch();}}
void LoadBD()//ZagryzkaBD
{ if(records)
{ system("cls");
printf("База данных не сохранена, хотите сохранить ее?");
if(YesNoDialog() == 13) SaveBD();
delete[] records;}
FILE * m_fp=fopen(FileDialog(), "rb");
if(m_fp)
{ fseek(m_fp, 0, SEEK_SET);
char header[10];
fread(header,1,9,m_fp);
header[9] = 0;
char true_header[] = "Baza_dann";
if(memcmp(header,true_header,9) != 0)
{ printf("Ошибочный Формат файла!!!");
getch();
return; }//Number of records
fread(&nrecords, 1, sizeof(long int), m_fp);
if(nrecords<0) nrecords = 0;
records = new record[nrecords];
record * r = records;
for(int i=0;i<nrecords;++i)
{ fread(&(r->PIB), 1, STR_SIZE - 1, m_fp);
fread(&(r->Adresa), 1, STR_SIZE - 1, m_fp);
fread(&(r->NomTel), 1, STR_SIZE - 1, m_fp);
fread(&(r->Tarif), 1, sizeof(long int), m_fp);
fread(&(r->Vartist), 1, sizeof(long int), m_fp);
++r;}
fclose(m_fp);}
else
{ printf("\nФайл не найден");
getch();}}
int Get(int min, long max)
{ int i = 0;
fscanf(stdin, "%d", &i);
fflush(stdin);
while ((i==0)||(i>max)||(i<min))
{ printf("Неверный ввод, повторите попытку. (%d - %d): ", min,max);
fscanf(stdin, "%d", &i);
fflush(stdin); }
return i;} |