include iostream include string include assert using namespace std con

  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
#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;
const int size=1000;
struct dataRow;
enum e_sex {FEMALE, MALE};
const int DBG_INOUT=1;
const int DBG_DB=2;
const int DEBUG=DBG_DB;
struct dataRow{
char* name;
char* surname;
char* birthday;
e_sex sex;
char* classname;
int end_year;
dataRow(char* a_name, char* a_surname, char* a_birthday, e_sex a_sex, char* a_classname, int a_end_year){
name = a_name;
surname = a_surname;
birthday = a_birthday;
sex = a_sex;
classname = a_classname;
end_year = a_end_year;
}
};
int surnamecmp(const void* a, const void* b){
cout<<static_cast<const dataRow*>(a)->surname<<" COMPARE "<<static_cast<const dataRow*>(b)->surname<<" \n";
return strcmp(static_cast<const dataRow*>(a)->surname,
static_cast<const dataRow*>(b)->surname);
}
class InOut{
private:
enum e_filemode {NO,READ, WRITE};
char* filename;
e_filemode filemode;
bool is_open;
FILE* file;
private:
void OpenFile(e_filemode a_filemode){
const char* rights=(a_filemode==READ?"rb":"wr");
if(!is_open){
file=fopen(filename,rights);
DEBUG&DBG_INOUT && cout<<"open "<<filename<<" for "<<rights<<"\n";
is_open=true;
filemode=a_filemode;
}
else if(is_open && filemode!=a_filemode){
fclose(file);
file=fopen(filename,rights);
filemode=a_filemode;
DEBUG&DBG_INOUT && cout<<"reopen "<<filename<<" for "<<rights<<"\n";
}
}
void writeBuffer(void* buff, int size){
OpenFile(WRITE);
DEBUG&DBG_INOUT && cout<<"Write "<<size<<" bytes\n";
fwrite(buff, size, 1, file);
}
void readBuffer(void* buffer, int size){
OpenFile(READ);
DEBUG&DBG_INOUT && cout<<"Read "<<size<<" bytes\n";
fread(buffer, size, 1, file);
}
public:
InOut(){
is_open=false;
filemode =NO;
}
~InOut(){
if(is_open)
fclose(file);
}
void SetFilename(char* fn){
filename=fn;
}
void writeInt(int a){
writeBuffer(&a, sizeof(int));
}
void writeDouble(double a){
writeBuffer(&a, sizeof(double));
}
void writeString(char* string){
writeInt((int)strlen(string));
writeBuffer(string, strlen(string));
}
int readInt(){
int res;
readBuffer(&res, sizeof(int));
return res;
}
double readDouble(){
double res;
readBuffer(&res, sizeof(double));
return res;
}
char* readString(){
int size = readInt();
char* res = new char[size + 1];
readBuffer(res, size);
res[size] = 0;
return res;
}
};
class DataBase: private InOut{
private:
int row_num;
dataRow** rows;
public:
DataBase(char* fn){
SetFilename(fn);
rows = new dataRow*[size];
row_num = 0;
}
void AddRow(dataRow* a_row){
rows[row_num]=a_row;
row_num++;
}
int GetNumRow(){
return row_num;
}
dataRow GetRow(int id){
return *rows[id];
}
void Save(){
DEBUG&DBG_DB && cout<<"Saving...";
writeInt(row_num);
char* name;
char* surname;
char* birthday;
e_sex sex;
char* classname;
int end_year;
for (int i=0; i<row_num; i++){
writeString (rows[i]->name);
writeString (rows[i]->surname);
writeString (rows[i]->birthday);
writeInt (rows[i]->sex);
writeString (rows[i]->classname);
writeInt (rows[i]->end_year);
DEBUG&DBG_DB && cout<<".";
}
DEBUG&DBG_DB && cout<<"\nSaved "<<row_num<<" rows!\n";
}
void Load(){
DEBUG&DBG_DB && cout<<"Loading";
row_num=0;
int j=readInt();
for (int i=0; i<j; i++){
DEBUG&DBG_DB && cout<<".";
char* name = readString();
char* surname = readString();
char* birthday = readString();
e_sex sex = (readInt()==1?MALE:FEMALE);
char* classname = readString();
int end_year = readInt();
AddRow(new dataRow(name, surname, birthday, sex, classname, end_year));
}
DEBUG&DBG_DB && cout<<"\nLoaded "<<row_num<<" rows!\n";
}
void PrintHeader(){
cout<<"Имя\tФамилия\tДата Рождения\tПол\tКласс\tВыпуск\n";
}
void PrintRow(int id){
cout<<rows[id]->name<<"\t"<<
rows[id]->surname<<"\t"<<
rows[id]->birthday<<"\t"<<
rows[id]->sex<<"\t"<<
rows[id]->classname<<"\t"<<
rows[id]->end_year<<"\n";
}
void PrintAll(){
PrintHeader();
for(int i=0;i<row_num;i++)
PrintRow(i);
}
void Select(){
//dataRow** new_rows=rows;
qsort(rows, 2, sizeof(dataRow), surnamecmp);
cout<<"1214";
}
};
void test(){
DataBase db("base");
db.AddRow(new dataRow("Геральд","Сдуко","06.07.1997",MALE,"4В",2014));
db.AddRow(new dataRow("Юманита","Лифина","11.05.1997",FEMALE,"4В",2014));
db.AddRow(new dataRow("Павлина","Дхокшвили","02.11.1998",FEMALE,"3А",2015));
db.AddRow(new dataRow("Вячеслав","Сшeин","04.04.1990",MALE,"11А",2007));
db.AddRow(new dataRow("Агей","Снуeшели","01.11.2000",MALE,"1Б",2017));
db.AddRow(new dataRow("Ефимия","Подоко","24.10.1996",FEMALE,"5В",2013));
db.AddRow(new dataRow("Раймонда","Втодева","23.04.2000",FEMALE,"1В",2017));
db.AddRow(new dataRow("Авдотья","Eфиушвили","17.06.1992",FEMALE,"9Б",2009));
db.AddRow(new dataRow("Евдокия","Пирфшели","14.01.1995",FEMALE,"6В",2012));
db.AddRow(new dataRow("Геронтий","Бжишели","05.10.1999",MALE,"2А",2016));
db.AddRow(new dataRow("Анфия","Фнугшели","16.04.1995",FEMALE,"6В",2012));
db.AddRow(new dataRow("Раиса","Яоноич","19.03.1990",FEMALE,"11Б",2007));
db.AddRow(new dataRow("Ванда","Гавева","05.01.1991",FEMALE,"10Б",2008));
db.AddRow(new dataRow("Горислава","Eфяова","22.09.1994",FEMALE,"7В",2011));
db.AddRow(new dataRow("Инна","Бгувтшвили","19.04.1994",FEMALE,"7Б",2011));
db.AddRow(new dataRow("Любава","Чироова","03.04.1996",FEMALE,"5В",2013));
db.AddRow(new dataRow("Домника","Глапина","23.08.1997",FEMALE,"4Б",2014));
db.AddRow(new dataRow("Павла","Eнокшели","26.07.1997",FEMALE,"4А",2014));
db.AddRow(new dataRow("Трофим","Ксяснко","21.09.1992",MALE,"9В",2009));
db.AddRow(new dataRow("Вальтер","Урабов","28.10.1999",MALE,"2Б",2016));
db.AddRow(new dataRow("Серафима","Сжакаич","18.12.1990",FEMALE,"11Б",2007));
db.AddRow(new dataRow("Ермолай","Авалев","26.02.2000",MALE,"1Б",2017));
db.AddRow(new dataRow("Викторина","Жолчова","27.12.1999",FEMALE,"2Б",2016));
db.AddRow(new dataRow("Эльза","Кмяeева","30.06.1993",FEMALE,"8А",2010));
db.AddRow(new dataRow("Нелли","Мрeкова","17.04.1990",FEMALE,"11В",2007));
db.AddRow(new dataRow("Рустам","Жясдко","16.08.1998",MALE,"3А",2015));
db.AddRow(new dataRow("Дар","Шкучигев","22.08.1996",MALE,"5А",2013));
db.AddRow(new dataRow("Святогор","Ядачшели","30.10.1998",MALE,"3В",2015));
db.AddRow(new dataRow("Евлампий","Шлиплов","15.08.1997",MALE,"4Б",2014));
db.AddRow(new dataRow("Савелий","Хгeин","22.02.1992",MALE,"9Б",2009));
db.AddRow(new dataRow("Киприан","Укошоуев","27.01.1996",MALE,"5В",2013));
db.AddRow(new dataRow("Шамиль","Оутсев","24.04.1997",MALE,"4Б",2014));
db.AddRow(new dataRow("Потап","Ишeмпшвили","13.02.1999",MALE,"2Б",2016));
db.AddRow(new dataRow("Иоанна","Нживова","20.05.2000",FEMALE,"1В",2017));
db.AddRow(new dataRow("Римма","Ягусич","12.11.1996",FEMALE,"5В",2013));
db.AddRow(new dataRow("Леонила","Фбуфшвили","20.01.1994",FEMALE,"7Б",2011));
db.AddRow(new dataRow("Ираклия","Уфашели","26.03.1993",FEMALE,"8А",2010));
db.AddRow(new dataRow("Дональт","Рякко","21.11.1992",MALE,"9А",2009));
db.AddRow(new dataRow("Мадлен","Гхоева","30.05.1998",FEMALE,"3В",2015));
db.AddRow(new dataRow("Марсен","Вхашели","19.10.2000",MALE,"1В",2017));
db.AddRow(new dataRow("Иннокентий","Ндяич","14.10.1995",MALE,"6В",2012));
db.AddRow(new dataRow("Федора","Мфипчко","02.02.2000",FEMALE,"1А",2017));
db.AddRow(new dataRow("Агния","Врeмко","23.02.1994",FEMALE,"7В",2011));
db.AddRow(new dataRow("Эльвира","Гвагжич","17.01.1995",FEMALE,"6Б",2012));
db.AddRow(new dataRow("Искра","Тголeдко","13.01.1998",FEMALE,"3В",2015));
db.AddRow(new dataRow("Болеслава","Ябиич","22.08.2000",FEMALE,"1Б",2017));
db.AddRow(new dataRow("Роза","Eдичушвили","01.11.1996",FEMALE,"5Б",2013));
db.AddRow(new dataRow("Анвар","Чохпшели","21.06.1991",MALE,"10А",2008));
db.AddRow(new dataRow("Авелина","Погяина","28.02.1993",FEMALE,"8Б",2010));
db.Save();
//db.Load();
db.Select();
db.PrintAll();
}
int main()
{
test();
scanf(" ") ;
return 0;
}