-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocGia.h
682 lines (652 loc) · 12.6 KB
/
DocGia.h
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
void SwapMaDG(int a[])
{
for(int i=1;i<MAXDG;i++)
{
a[i]=i;
}
int ramdom ;
for(int i=1;i<MAXDG/2;i++)
{
ramdom=1+rand()%(MAXDG-1);
if(ramdom!=i)
{
int temp=a[ramdom];
a[ramdom]=a[i];
a[i]=temp;}
}
}
void LoadMaDocGia()
{
fstream f;
f.open("MaDocGia.TXT", ios::in );
if(f.fail()|| f.eof())
{
f.open("MaDocGia.TXT", ios::out );
SwapMaDG(mathe);
for(int i=1;i<MAXDG;i++)
{
f<<mathe[i]<<" ";
}
ma_chua_dung=MAXDG-1;
f.close();
}
else{
while(!f.eof())
{
++ma_chua_dung;
f>>mathe[ma_chua_dung];
}
f.close();
}
}
int LayMaDG()
{
fstream f;
int maDG;
maDG=mathe[1];//lay phan tu dau tien
for(int i=1;i<ma_chua_dung;i++)// bo phan tu dau tien
{
mathe[i]=mathe[i+1];
}
f.open("MaDocGia.TXT", ios::out);
for(int i=1;i<ma_chua_dung;i++)//dua len file sau khi bo phan tu dau tien
{
f<<mathe[i]<<" ";
if(mathe[i+1]==0)break;
}
f.close();
return maDG;
}
void SaveDocGia(TREE t, ofstream &f, ofstream &f2)
{
if(t==NULL)
{
return;
}
else{
f<<t->data.ma_the<<",";
f<<t->data.ho<<",";
f<<t->data.ten<<",";
f<<t->data.phai<<",";
f<<t->data.trang_thai<<endl;
for (NODE_MUON_TRA* k = t->data.DS_MuonTra.First; k != NULL; k = k->right)
{
f2 << t->data.ma_the << "," << k->data.ma_sach << ",";
f2 << k->data.ngay_muon.Ngay << "/" << k->data.ngay_muon.Thang << "/" << k->data.ngay_muon.Nam << ",";
f2 << k->data.ngay_tra.Ngay << "/" << k->data.ngay_tra.Thang << "/" << k->data.ngay_tra.Nam << ",";
f2 << k->data.trang_thai << endl;
}
SaveDocGia(t->pLeft,f,f2);
SaveDocGia(t->pRight,f,f2);
}
}
void SaveToFile(TREE t)
{
ofstream f, f2;
f.open("DanhSachDocGia.TXT",ios::out);
f2.open("DanhSachMuonTra.TXT",ios::out);
SaveDocGia(t,f,f2);
f.close();
f2.close();
}
void ThemDocGiaVaoFile(TREE t)
{
ofstream f;
f.open("DanhSachDocGia.TXT", ios :: out | ios::app);
f<<t->data.ma_the<<",";
f<<t->data.ho<<",";
f<<t->data.ten<<",";
f<<t->data.phai<<",";
f<<t->data.trang_thai<<endl;
f.close();
}
void Load1DocGia(TREE &t, ifstream &f ,int mathe)
{
if(t==NULL)
{
NODE_DOCGIA *p= new NODE_DOCGIA;
p->data.ma_the=mathe;
f.ignore();
getline(f,p->data.ho,',');
getline(f,p->data.ten,',');
getline(f,p->data.phai,',');
f>>p->data.trang_thai;
f.ignore();
p->data.SoNgayQuaHan=0;
p->data.SoSachDangMuon=0;
p->data.DS_MuonTra.First=NULL;
p->data.DS_MuonTra.Last=NULL;
p->pLeft=NULL;
p->pRight=NULL;
t=p;
}
else{
if(t->data.ma_the > mathe)
{
Load1DocGia(t->pLeft,f,mathe);
}
else if(t->data.ma_the < mathe)
{
Load1DocGia(t->pRight,f,mathe);
}
}
}
void LoadDocGiaFromFile(TREE &t)
{
int lay_ma;
ifstream f;
f.open("DanhSachDocGia.TXT",ios::in);
if(f.fail()){
return;
}
while(!f.eof())
{
lay_ma=0;
f>>lay_ma;
if(lay_ma==0){
break;
}
++sl_docgia;
Load1DocGia(t ,f ,lay_ma);
}
f.close();
}
void ChuanHoaChu(string &str)
{
//khoang trang dau va cuoi
while(str[0]==' ')
{
str.erase(str.begin()+0);
}
while(str[str.length()-1]==' ')
{
str.erase(str.begin()+str.length()-1);
}
//khooang trang giua
for(int i=0;i<str.length();i++)
{
if(str[i]==' ' && str[i+1]==' ')
{
str.erase(str.begin()+i);
i--;
}
}
//viet hoa ky tu dau
if(str[0]>=97 && str[0]<=122)
{
str[0]-=32;
}
for(int i=1;i<str.length();i++)
{
if(str[i]==' ')
{
i++;
if(str[i]>=97 &&str[i]<=122)
{
str[i]-=32;
}
}
else{
if(str[i]>=65 &&str[i]<=90)
{
str[i]+=32;
}
}
}
}
string TrangThaiDocGia(int n)
{
if (n == 1)
return "Hoat Dong";
else
return "Khoa";
}
void KhoiTaoDG(TREE &t)
{
t=NULL;
}
string ChonGioiTinh(bool &Thoat,int toaX, int toaY)
{
XuatText("Nu",toaX+11,toaY,7);
XuatText("Nam",toaX,toaY,12);
ShowCur(1);
string gt="Nam";
char chon;
do{
chon=getch();
if (chon==0) chon = getch();
switch(chon){
case Right:if(gt=="Nam"){
XuatText("Nam",toaX,toaY,7);
XuatText("Nu",toaX+11,toaY,12);
gt="Nu";
}break;
case Left:if(gt=="Nu"){
XuatText("Nu",toaX+11,toaY,7);
XuatText("Nam",toaX,toaY,12);
gt="Nam";
} break;
case Up:{
gotoxy(45,toaY-4);
return gt;
}
case Down:{
gotoxy(45,toaY+4);
return gt;
}
case Enter:{
gotoxy(45,toaY+4);
return gt;
}
case Esc:{
Thoat=TRUE;
return gt;
}
}
}while(1);
}
void ThemDocGia(TREE &t, int x,bool &Exit)
{
if(t==NULL)
{
NODE_DOCGIA *p=new NODE_DOCGIA;
XuatNum(x,85,19,15);
int a[3]={19,23,27};
gotoxy(52,19);//cho con tro nhay toi toa do cua ham nhap ho
while(1){
//cout<<"Nhap ho: ";
if(wherey()==19){
Nhapho:
XuLiNhapDocGia(52, 19,p->data.ho,18,Exit);
if(Exit){
Exit=ThongBao("Du lieu se khong duoc luu","Ban co muon thoat ?",65,21);
if(Exit){
delete p;
return;
}
else goto Nhapho;
}
}
//cout<<"Nhap ten: ";
if(wherey()==23){
Nhapten:
XuLiNhapDocGia(52,23,p->data.ten,12,Exit);
if(Exit){
Exit=ThongBao("Du lieu se khong duoc luu","Ban co muon thoat ?",65,21);
if(Exit){
delete p;
return;
}
else goto Nhapten;
}
}
//cout<<"Nhap phai: ";
if(wherey()==27){
Chongioitinh:
p->data.phai=ChonGioiTinh(Exit,53,27);
if(Exit){
Exit=ThongBao("Du lieu se khong duoc luu","Ban co muon thoat ?",65,21);
if(Exit){
delete p;
return;
}
else goto Chongioitinh;
}
}
if(wherey()==31){
p->data.trang_thai=1;// cap nhap trang thai the
gotoxy(52,31);
cout<<TrangThaiDocGia(p->data.trang_thai);
ShowCur(0);
Exit=PhimDuyet(60,36);
break;
}
}
if(Exit){
Exit=!ThongBao("Da them thanh cong.","Ban co muon tiep tuc ?",65,21);
ChuanHoaChu(p->data.ho);
ChuanHoaChu(p->data.ten);
ChuanHoaChu(p->data.phai);
p->data.ma_the=LayMaDG();
p->data.SoSachDangMuon=0;
p->data.SoNgayQuaHan=0;
p->data.DS_MuonTra.First=NULL;
p->data.DS_MuonTra.Last=NULL;
p->pLeft=NULL;
p->pRight=NULL;
t=p;
sl_docgia++;
ThemDocGiaVaoFile(p);
}
else {
Exit=ThongBao("Du lieu se khong duoc luu","Ban co muon thoat ?",65,21);
if(Exit){
delete p;
return;
}
else goto Nhapho;
}
}
else{
if(t->data.ma_the > x){
ThemDocGia(t->pLeft, x,Exit);
}
if(t->data.ma_the < x){
ThemDocGia(t->pRight, x, Exit);
}
}
}
TREE TimDocGia(TREE t,int mathe)
{
if(t==NULL)
{
return t;
}
else{
if(t->data.ma_the> mathe)
{
TimDocGia(t->pLeft,mathe);
}
else if(t->data.ma_the < mathe)
{
TimDocGia(t->pRight,mathe);
}
else
{
return t;
}
}
}
void NodeThayThe(TREE &rp, TREE &p)// tim diem cuc trai cua p, swap t vs rp
{
if(p->pLeft!=NULL)
{
NodeThayThe(rp,p->pLeft);
}
else{
rp->data=p->data;
rp=p;
p=rp->pRight;
}
}
void XoaDocGia(TREE &t, int x, bool &Thoat)
{
if(t==NULL)
{
Thoat=!ThongBao("Khong tim thay thong tin","Ban co muon tiep tuc",55,21);
gotoxy(62,20);cout<<" ";
return;
}
else{
if(t->data.ma_the > x){
XoaDocGia(t->pLeft,x,Thoat);
}
else if(t->data.ma_the < x)
{
XoaDocGia(t->pRight,x,Thoat);
}
else{
GiaoDienXuat1Docgia(t->data.ma_the,t->data.ho,t->data.ten,t->data.phai,TrangThaiDocGia(t->data.trang_thai));
Thoat=PhimDuyet(60,36);
if(!Thoat)
{
Thoat=ThongBao("Huy xoa","Ban co muon thoat",55,21);
XoaHCN(11, 15, 123, 22);
return;
}
NODE_DOCGIA *rp=t;
if(t->pLeft==NULL) t=t->pRight;
else{
if(t->pRight==NULL) t=t->pLeft;
else {
NodeThayThe(rp,t->pRight);
}
}
delete rp;
sl_docgia--;
Thoat=!ThongBao("Da xoa","Ban co muon tiep tuc",55,21);
XoaHCN(11, 15, 123, 22);
}
}
}
void HieuChinhDocGia(TREE &t,int mathe,bool &Thoat)
{
NODE_DOCGIA *q;
q=TimDocGia(t, mathe);
if(q==NULL)
{
BaoLoi("Khong tim thay thong tin","Hay nhap lai",55,21);
KhungText("Ho:",47,21,21, 2, 14 );
KhungText("Ten:",46,25,15, 2, 14 );
return;
}
else{
NODE_DOCGIA *p=new NODE_DOCGIA;
p->data=q->data;
XuatText(p->data.ho,52, 21,15);
XuatText(p->data.ten,52, 25,15);
if(p->data.phai=="Nam"){
XuatText(p->data.phai,53,29,12);
}
else{
XuatText(p->data.phai,64,29,12);
}
XuatText(TrangThaiDocGia(p->data.trang_thai),52,33,70);
gotoxy(52,21);
while(!Thoat)
{
if(wherey()==21)
{
XuLiNhapDocGia(52, 21,p->data.ho,18,Thoat);
if(Thoat){
Thoat=ThongBao("","Ban co muon thoat ?",55,21);
if(Thoat){
delete p;
return;
}
else GiaoDienHieuChinh(p->data.ho, p->data.ten);gotoxy(52,21);
}
}
//cout<<"Nhap ten: ";
if(wherey()==25)
{
XuLiNhapDocGia(52,25,p->data.ten,13,Thoat);
if(Thoat){
Thoat=ThongBao("","Ban co muon thoat ?",55,21);
if(Thoat){
delete p;
return;
}
else GiaoDienHieuChinh(p->data.ho, p->data.ten);gotoxy(52,25);
}
}
//cout<<"Nhap phai: ";
if(wherey()==29)
{
p->data.phai=ChonGioiTinh(Thoat,53,29);
if(Thoat){
Thoat=ThongBao("","Ban co muon thoat ?",55,21);
if(Thoat){
delete p;
return;
}
else GiaoDienHieuChinh(p->data.ho, p->data.ten);gotoxy(52,29);
}
}
if(wherey()==33)
{
ShowCur(0);
Thoat=PhimDuyet(60,36);
break;
}
}
if(Thoat){
Thoat=!ThongBao("Da hieu chinh thanh cong.","Ban co muon tiep tuc ?",55,21);
ChuanHoaChu(p->data.ho);
ChuanHoaChu(p->data.ten);
ChuanHoaChu(p->data.phai);
q->data=p->data;
p->data=q->data;
delete p;
SaveToFile(t);
}
else {
Thoat=ThongBao("Du lieu se khong duoc luu","Ban co muon thoat ?",55,21);
delete p;
}
}
}
void ChepDGVaoMang(TREE t, TheDocGia p[],int &k)
{
if(t==NULL){
return ;
}
else{
ChepDGVaoMang(t->pLeft,p,k);
p[k]=t->data;
k++;
ChepDGVaoMang(t->pRight,p,k);
}
}
void SapXepTheoHoTen(TheDocGia p[],int &n)
{
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
//int kt1=strcmp(p[i].ten.c_str(),p[j].ten.c_str());
if(p[i].ten>p[j].ten){
TheDocGia tam=p[i];
p[i]=p[j];
p[j]=tam;
}
else if(p[i].ten==p[j].ten){
// int kt2=strcmp(p[i].ho.c_str(),p[j].ho.c_str());
if(p[i].ho>p[j].ho){
TheDocGia tam=p[i];
p[i]=p[j];
p[j]=tam;
}
}
}
}
}
void ChuyenDanhSachDocGiaQuaHanSangMang(TREE t, TheDocGia a[] , int &i)
{
if (t == NULL)
{
return;
}
else
{
ChuyenDanhSachDocGiaQuaHanSangMang(t->pLeft, a , i );
if(t->data.SoNgayQuaHan > 0)
{
a[i++] = t->data;
}
ChuyenDanhSachDocGiaQuaHanSangMang(t->pRight, a , i );
}
}
void SapXepDanhSachDocGiaQuaHanGiamDan(TREE t, TheDocGia a[],int &n)
{
n = 0;
ChuyenDanhSachDocGiaQuaHanSangMang(t,a,n);
TheDocGia tam;
for(int i = 0 ; i < n-1 ; i++)
{
for(int j = i+1 ; j < n ; j++)
{
if (a[i].SoNgayQuaHan < a[j].SoNgayQuaHan)
{
tam = a[i];
a[i] = a[j];
a[j] = tam;
}
}
}
}
void XuatDocGiaRaPage(TREE t, bool &Thoat,int &luachon)
{
int i=0, stt=0,temp;
int soPage=1;
int n=sl_docgia;
TheDocGia *p=new TheDocGia[sl_docgia];
XoaHCN(13,13,120,25);
ChepDGVaoMang(t, p,i);
luachon=LuaChonXuatDG(luachon);
if(luachon==-1) {
Thoat=TRUE;
return;
}
else if(luachon==1)
{
SapXepTheoHoTen(p,sl_docgia);
}
else if(luachon==2)
{
SapXepDanhSachDocGiaQuaHanGiamDan(t, p,n);
}
//===LOAD GIAO DIEN NOI DUNG======
GiaoDienXuatPageDG(soPage,n);
i=18;
temp=stt;
for(temp;(temp<stt+7)&& temp<n;temp++)
{
XuatNum(p[temp].ma_the,16,i,16);
XuatText(p[temp].ho+' '+p[temp].ten,35,i,16);
XuatText(p[temp].phai,74,i,16);
XuatText(TrangThaiDocGia(p[temp].trang_thai),87,i,16);
XuatNum(p[temp].SoSachDangMuon,108,i,16);
XuatNum(p[temp].SoNgayQuaHan,124,i,16);
i=i+3;
}
char kytu;
do{
kytu=getch();
switch(kytu)
{
case 77:{if(stt+7<n){
++soPage;
LoadPageDG(soPage,n);
// GiaoDienXuatPageDG();
stt+=7;
i=18;
temp=stt;
for(temp;(temp<stt+7)&&temp<n;temp++)
{
XuatNum(p[temp].ma_the,16,i,16);
XuatText(p[temp].ho+' '+p[temp].ten,35,i,16);
XuatText(p[temp].phai,74,i,16);
XuatText(TrangThaiDocGia(p[temp].trang_thai),87,i,16);
XuatNum(p[temp].SoSachDangMuon,108,i,16);
XuatNum(p[temp].SoNgayQuaHan,124,i,16);
i=i+3;
}
}
break;
}
case 75:{if(stt>=7){
--soPage;
LoadPageDG(soPage,n);
// GiaoDienXuatPageDG();
stt=stt-7;
i=18;
temp=stt;
for(temp;(temp<stt+7)&&temp<n;temp++)
{
XuatNum(p[temp].ma_the,16,i,16);
XuatText(p[temp].ho+' '+p[temp].ten,35,i,16);
XuatText(p[temp].phai,74,i,16);
XuatText(TrangThaiDocGia(p[temp].trang_thai),87,i,16);
XuatNum(p[temp].SoSachDangMuon,108,i,16);
XuatNum(p[temp].SoNgayQuaHan,124,i,16);
i=i+3;
}
}
break;
}
case 27:{
delete[] p;
return ;
}
}
}while(1);
}