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
| #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string.h>
#include <fstream>
using namespace std;
void Choice();
int c=0; //Глобальная переменная с - для количества элементов структуры
int k=0; //Глобальная переменная к - для номера удаляемого элемента
struct TelAbon //Структура для абонента
{
struct FIO
{
char surname[50]; //Фамилия
char name[50]; //Имя
char second_name[50]; //Отчество
};
FIO fio;
char adress[100]; //Адрес абонента
int year; //Год установки телефона
int number; //Номер телефона
};
class Telephon //Создаю свой класс
{
public:
Telephon(); //Конструктор
void LoadFile(); //Функция для считывания информации из файла
void Dobavit(); //Функция для добавления экземпляра структуры
void Ydalit(); //Функция для удаления экземпляра структуры
void Naity(); //Функция для поиска по адресу/фамилии
void Analiz(); //Функция печатает количество телефонов,установленных в определенном году
void SaveFile();
TelAbon mas[5000];
};
Telephon tel; //Экземпляр класса
Telephon::Telephon() //Инициализация конструктора
{
}
void Telephon::LoadFile()
{
FILE *f = fopen("telephon.txt", "r");
if((f=fopen("telephon.txt","r"))==NULL)
printf("Cannot open file\n");
char buff[150],*p;
int i=0;
while(!feof(f))
{
fgets (buff, sizeof(buff), f);
p = strtok (buff," ");
strcpy (mas[i].fio.surname, p);
p = strtok (NULL, " ");
strcpy (mas[i].fio.name, p);
p = strtok (NULL, "\n\0");
strcpy(mas[i].fio.second_name, p);
fgets (buff, 100, f);
p = strtok (buff, "\n");
strcpy (mas[i].adress, p);
fgets(buff, sizeof(buff),f);
mas[i].year = atoi(buff);
fgets(buff,sizeof(buff),f);
mas[i].number = atoi(buff);
fgets(buff,sizeof(buff),f);
i++;
}
c=i;
fclose(f);
remove("telephon.txt");
}
void Telephon :: Dobavit()
{
FILE *f;
TelAbon tmp;
f = fopen ("telephon.txt", "w");
cout<<"Enter telephon subscriber surname, name and second name"<<endl;
cin.getline(tmp.fio.surname,sizeof(tmp.fio.surname),32);
cin.getline(tmp.fio.name,sizeof(tmp.fio.name),32);
cin.getline(tmp.fio.second_name,sizeof(tmp.fio.second_name));
cout<<"Enter telephon subscriber adress:"<<endl;
cin.getline(tmp.adress,sizeof(tmp.adress));
cout<<"Enter year of telephon installation"<<endl;
cin>>tmp.year;
cout<<"Enter telephon number:"<<endl;
cin>>tmp.number;
fprintf(f,"%s %s %s\n",tmp.fio.surname,tmp.fio.name,tmp.fio.second_name);
fprintf(f,"%s\n",tmp.adress);
fprintf(f,"%d\n",tmp.year);
fprintf(f,"%d\n",tmp.number);
fprintf(f,"\n");
fclose(f);
}
void Telephon :: Ydalit()
{
char sname[50], nm[50], snm[50];
cout<<"Enter telephon subscriber surname, name and second name, who you want to remove"<<endl;
cin.getline(sname, sizeof(sname), 32);
cin.getline(nm, sizeof(nm), 32);
cin.getline(snm, sizeof(snm));
for(int i=0; i<c; i++)
{
char s = strcmp (sname, mas[i].fio.surname);
char n = strcmp (nm, mas[i].fio.name);
char sn = strcmp (snm, mas[i].fio.second_name);
if (s==0 && n==0 && sn==0)
k=i;
}
}
void Telephon::Naity()
{
char surname[50];
char adress[50];
cout<<"Enter telephon subscriber surname"<<endl;
cin.getline(surname,sizeof(surname),32);
cout<<"Enter telephon subscriber adress"<<endl;
cin.getline(adress,sizeof(adress));
if(sizeof(adress)==0)
for(int i=0; i<c; i++)
{
char s = strcmp(surname, mas[i].fio.surname);
if(s==0)
{
printf("%s %s %s\n", mas[i].fio.surname, mas[i].fio.name, mas[i].fio.second_name);
printf("%d\n", mas[i].number);
}
else
cout<<" Telephon subscriber isn't find"<<endl;
}
else
for(int i=0; i<c; i++)
{
char a = strcmp(adress, mas[i].adress);
char b = strcmp(surname, mas[i].fio.surname);
if(a==0 && b==0)
{
printf("%s %s %s\n", mas[i].fio.surname, mas[i].fio.name, mas[i].fio.second_name);
printf("%s\n", mas[i].adress);
printf("%d\n", mas[i].number);
}
else
if(a!=0 && b==0)
{
printf("%s %s %s\n", mas[i].fio.surname, mas[i].fio.name, mas[i].fio.second_name);
printf("%d\n", mas[i].number);
}
else
cout<<" Telephon subscriber isn't find"<<endl;
}
}
void Telephon::Analiz()
{
int year,count=0;
cout<<"Enter year of telephon installation"<<endl; //Вводится год установки телефона
cin>>year;
for(int i=0;i<c;i++) //Выполняется, пока не достигнет конца файла
{
if(year==mas[i].year) //Если найден искомый год - инкремент переменной с
count++;
}
cout<<count<<" Telephon installed in "<<year<<" year"<<endl; //Вывод на экран количество телефонов, установленных в нужном году
}
int main()
{
tel.LoadFile();
Choice();
tel.SaveFile();
return 0;
}
void Choice()
{
int control; //Переменная для реакции на клавиатуру
cout<<"Choose menu position"<<endl; //Выбрать позицию из меню
cout<<"\nMENU:\n1.Add telephone subscriber \n2.Find telephone subscriber \n3.Remove telephone subscriber \n4. Analysis \n5. Exit\n"<<endl;
cin>>control;
for(;;)
{
if(control==1)
tel.Dobavit();
else
if(control==2)
tel.Naity();
else
if(control==3)
tel.Ydalit();
if(control==4)
tel.Analiz();
if (control==5)break;
else
if(control==0 || control<9 && control>5)
cout<<"You press incorrect button. Please repeat"<<endl; //Нажата неверная клавиша, повторить
cout<<"Choose menu position"<<endl;
cin>>control;
}
}
void Telephon::SaveFile()
{
FILE *f;
f = fopen("telephon.txt", "a");
for(int i=0; i<c; i++)
{
if(i==k) continue;
fprintf(f,"%s %s %s\n",mas[i].fio.surname,mas[i].fio.name,mas[i].fio.second_name);
fprintf(f,"%s\n",mas[i].adress);
fprintf(f,"%d\n",mas[i].year);
fprintf(f,"%d\n",mas[i].number);
fprintf(f,"\n");
}
fclose(f);
} |