-
Notifications
You must be signed in to change notification settings - Fork 1
/
BK_ENCYCLOPEDIA.cpp
1284 lines (1213 loc) · 36.8 KB
/
BK_ENCYCLOPEDIA.cpp
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>
#include "myString.h"
#include<time.h>
#define MAX_FIELD 20
using namespace std;
int MAXWORDS, MAXTYPES;
struct word{
string name;
int typeCode;
string values[MAX_FIELD];
int valuesCount;
static int count;
};
struct type {
int typeCode;
string typeName;
string fieldName[MAX_FIELD];
int fieldcount;
static int count;
};
struct stringArr{
string lines[MAX_FIELD];
int count;
};
type *tabletype;
word *words;
int type::count = 0;
int word::count = 0;
void addType();
void addType(type);
void addWord();
void addWord(word);
int searchWordByName(string, int, int );
int searchTypeByName(string);
int searchTypeByCode(int , int, int);
void printWords();
void printWord(int );
void printTypes();
void printNameTypes();
void deleteType();
void deleteWord();
void editWord();
void editType();
void findWord();
stringArr divStringSemicolon();
void printWordName(int);
void printLogo(){
cout <<"\n\n";
cout << " __Tu dien Bach Khoa toan thu BKEncyclopedia__ " << endl;
cout << " | |" << endl;
cout << " ***********************************************" << endl;
}
void saveWords()
{
ofstream out("tu-dien-0.txt");
int i = 0,j = 0;
for( i= 0;i<word::count-1;i++)
{
out << words[i].name << endl<<words[i].typeCode<<endl;
for(int j = 0; j < words[i].valuesCount;j++)
{
out << words[i].values[j]<<endl;
}
}
i = word::count-1;
out << words[i].name << endl<<words[i].typeCode<<endl;
for(int j = 0; j < words[i].valuesCount-1;j++)
{
out << words[i].values[j]<<endl;
}
j = words[i].valuesCount-1;
out << words[i].values[j];
out.close();
}
void saveTypes()
{
ofstream out ("tu-dien.txt");
int i= 0, j = 0;
for( i =0; i < type::count-1;i++)
{
out<<tabletype[i].typeCode << endl<<tabletype[i].typeName<<endl;
for( j = 0; j < tabletype[i].fieldcount;j++)
{
out << tabletype[i].fieldName[j]<<"; ";
}
out << endl;
}
i = type::count-1;
out<<tabletype[i].typeCode << endl<<tabletype[i].typeName<<endl;
for( j = 0; j < tabletype[i].fieldcount-1;j++)
{
out << tabletype[i].fieldName[j]<<"; ";
}
j = tabletype[i].fieldcount-1;
out << tabletype[i].fieldName[j]<<"; ";
out.close();
}
void save()
{ system("cls");
fflush(stdin);
printLogo();
cout << "\n\n Chuc nang da chon: Luu du lieu vua duoc chinh sua. \n\n";
saveWords();
saveTypes();
cout <<"\n ------------------LUU THANH CONG----------------\n"<<endl;
getchar();
system("cls");
}
stringArr divStringSemicolon(string line){
stringArr res; res.count = 0;
string chuoi = "";
int result = 0;
while(result < line.length()) {
chuoi = "";
while(line[result]==' ' && result < line.length()) result++;
while(line[result]!=';' && result < line.length()) chuoi += line[result++];
result++;
if(chuoi.length() > 0) {
res.lines[res.count++] = chuoi;
}
}
return res;
}
// Nap cac the loai tu file dau vao
void loadTypes(){
ifstream fi("the-loai.txt");
string line;
while(!fi.eof()){
// tao bien newtype de luu the loai moi
type newtype;
getline(fi,line);
// gan ma the loai
newtype.typeCode = atoi(line.c_str());
getline(fi,line);
// gan ten the loai
newtype.typeName = line;
getline(fi,line);
// doc cac truong
stringArr res = divStringSemicolon(line);
// gan cac truong vao newtype
for(int i = 0; i < res.count;i++) {
newtype.fieldName[i] = res.lines[i];
}
// gan so truong
newtype.fieldcount = res.count;
//tim vi tri, dua newtype vao mang tabletype
addType(newtype);
}
//cout << type::count << "Count"<<endl;
fi.close();
}
void printTypes() {
cout <<"\n ----------------DANH SACH THE LOAI--------------\n"<<endl;
for(int i = 0; i < type::count;i++){
cout <<" Ma The Loai: " << tabletype[i].typeCode << endl;
cout <<" Ten The Loai: " << tabletype[i].typeName << endl;
cout <<" Cac Truong: " ;
for(int j = 0; j < tabletype[i].fieldcount;j++)
{
cout << tabletype[i].fieldName[j] << "; ";
}
cout << "\n ---------------";
cout << endl << endl;
}
}
void printNameTypes(){
cout <<"\n ----------------DANH SACH THE LOAI---------------\n"<<endl;
for(int i = 0; i < type::count; i++){
cout <<" "<< tabletype[i].typeCode << ": " << tabletype[i].typeName << endl;
}
cout <<"\n --------------------------------------------------\n"<<endl;
}
// tra ve so truong
int getFieldCount(int code)
{
int i = searchTypeByCode(code,0,type::count);
return tabletype[i].fieldcount;
}
void smartWords(){
MAXWORDS = MAXWORDS * 2;
word *temp = new word[MAXWORDS];
for(int i = 0; i < word::count; i++){
temp[i] = words[i];
}
words = temp;
}
//Them tu vao mang, sap xep theo ten
void addWord(word newword){
if(word::count == MAXWORDS - 1){
smartWords();
}
for(int i = 0; i <= word::count; i++){
if(newword.name < words[i].name || i == word::count){
for(int j = word::count; j > i; j--){
words[j] = words[j-1];
}
words[i] = newword;
break;
}
}
word::count ++;
}
//doc file word
void loadWords(){
ifstream fi("tu.txt");
string line;
while(!fi.eof()) {
word newword;
getline(fi,line); newword.name = line;
getline(fi,line); newword.typeCode = atoi(line.c_str());
int numFields = -1;
numFields = getFieldCount(newword.typeCode);
newword.valuesCount = numFields;
for(int i= 0;i < numFields;i++)
{
getline(fi,line); newword.values[i] = line;
}
addWord(newword);
}
fi.close();
}
//in ra tat ca cac tu
void printWords()
{ clock_t start, finish;
double time;
start = clock();
int code,k;
fflush(stdin);
printLogo();
cout << "\n\n Chuc nang da chon: In danh sach cac tu. \n\n";
cout <<"\n -----------------DANH SACH CAC TU---------------\n"<<endl;
for(int i = 0;i < word::count; i++)
{
cout <<"Tu " << i << ": " << words[i].name << endl <<"MA THE LOAI: " << words[i].typeCode <<endl;
code = words[i].typeCode;
k = searchTypeByCode(code,0,type::count-1);
for(int j= 0;j < words[i].valuesCount ;j++)
{
cout << tabletype[k].fieldName[j] <<": "<< words[i].values[j] <<endl;
}
cout << "--------------------\n\n";
}
finish = clock();
time = (float)(finish-start)/CLOCKS_PER_SEC;
cout << "\n -----------------------------------------------\n";
printf("\n Thao tac xong trong khoang %.10f giay.\n\n",time);
getchar();
system("cls");
}
//in ra mot tu o vi tri thu i
void printWord(int i)
{ int code = words[i].typeCode;
int k = searchTypeByCode(code,0,type::count-1);
cout << words[i].name << endl;
for(int j= 0;j < words[i].valuesCount ;j++)
{
cout << endl<<tabletype[k].fieldName[j]<<": " << words[i].values[j] <<endl;
}
}
void printWordName(int i) {
cout <<" " << words[i].name << endl;
}
string checkInput(string line){
int last = line.length();
while(1){
last--;
if(line[last] == 32) line = line.substr(0,last);
else break;
}
while(1){
if(line[0] == 32) line = line.substr(1,last);
else break;
}
return line;
}
bool isNear(string name1, string name2){
if(strstr(toLowerStr(name1).c_str(),toLowerStr(name2).c_str()) != NULL) return true;
else return false;
}
void findNearWord(string name){
cout << "\n Cac tu co lien quan: \n" << endl;
for(int i = 0; i < word::count; i++){
if(isNear(words[i].name,name)) printWordName(i);
}
}
stringArr divStringSemicolonSpace(string str){
stringArr temp;
temp.count = 0;
int len = str.length();
int j = 0, i = 0;
string sub;
for(int i = 0; i < len; i++){
if(str[i] == 32){
sub = str.substr(j,i-j);
j = i + 1;
temp.lines[temp.count] = sub;
temp.count++;
}
}
if(i == 0) i = len;
sub = str.substr(j,i-j);
temp.lines[temp.count] = sub;
temp.count++;
return temp;
}
// nhap vao 1 xau ky tu, tim cac tu trong xau ky tu nay trong cac truong cua tu, dua ra tu neu tu co chua 5% so luong cac tu trong xau input
bool isDesWord(stringArr temp,word w){
int totalword = temp.count;
int rightword = 0;
float percent = 0.0;
for(int j = 0; j < totalword; j++){
for(int i = 0; i < w.valuesCount; i++){
if(isNear(w.values[i],temp.lines[j])){
rightword++;
percent = (float) rightword/totalword;
if(percent >= 0.9) return true;
break;
}
}
}
return false;
}
//tim tu theo ten
void findWord(){
clock_t start, finish;
double time;
fflush(stdin);
do{ printLogo();
cout << "\n\n Chuc nang da chon: Tim tu. \n\n";
cout << " Nhap tu muon tim kiem hoac nhap 0 de thoat: ";
string name;
fflush(stdin);
getline(cin,name);
name = checkInput(name);
if(name == "0") {
system("cls");
break;
}
start=clock();
int result = searchWordByName(name,0,word::count);
finish = clock();
time = (float)(finish-start)/CLOCKS_PER_SEC;
if(result == -1){
while(1){
clock_t start1, finish1;
double time1;
system("cls");
printLogo();
cout << "\n\n Chuc nang da chon: Tim tu. \n\n";
cout << " Tu muon tim kiem: " << name << endl;
cout <<"\n ----------------KHONG TIM THAY---------------"<<endl<<endl;
cout << " Ban co the thu tim kiem tu gan dung hoac mo ta: \n";
cout << "\n 1.Tim kiem gan dung\n 2.Mo ta cac truong\n Nhap khac 1 va 2 de thoat\n Lua chon cua ban: ";
string line;
fflush(stdin);
getline(cin,line);
start1 = clock();
line = checkInput(line);
if(line == "1") {
findNearWord(name);
finish1 = clock();
time1 = (float)(finish1-start1)/CLOCKS_PER_SEC;
cout << "\n ----------------------------------------------";
printf("\n Thao tac xong trong khoang %.10f giay.",time1);
getchar();
}
if(line == "2") {
cout << "\n Nhap cac mo ta cua ban: ";
fflush(stdin);
getline(cin,line);
start1 = clock();
line = checkInput(line);
if(line == ""){
cout << "\n Loi! Ban khong nhap gi\n";
}
else{
stringArr temp = divStringSemicolonSpace(line);
int count = 0;
cout << "\n ------------------KET QUA--------------------";
for(int i = 0; i < word::count; i++){
if(isDesWord(temp,words[i])){
count++;
cout << "\nTu " << count << endl;
printWord(i);
}
}
}
finish1 = clock();
time1 = (float)(finish1-start1)/CLOCKS_PER_SEC;
fflush(stdin);
getchar();
}
if(line!="1"&&line!="2") break;
}
}
else{
cout <<"\n ------------------TIM THAY---------------------\n"<<endl;
printWord(result);
cout <<"\n -----------------------------------------------\n"<<endl;
}
printf("\n Thao tac xong trong khoang %.10f giay.",time);
getchar();
system("cls");
}while(1);
}
//ham nhap tu moi
void addWord(){
clock_t start, finish;
double time;
string line;
word newword;
fflush(stdin);
printLogo();
cout << "\n\n Chuc nang da chon: Them tu. \n";
printNameTypes();
bool flag = false;
while(1){
if ( flag == true){
if(line != "0" && atoi(line.c_str()) ==0) cout << "\n Vui long nhap so, khong nhap chu!\n";
else cout << "\n Ma the loai nay khong co trong danh sach!\n";
}
cout << " Nhap ma the loai hop le: ";
fflush(stdin);
getline(cin,line);
flag = true;
start = clock();
line = checkInput(line);
if(line != "0" && atoi(line.c_str()) == 0) continue;
else{
if(searchTypeByCode(atoi(line.c_str()),0,type::count-1) == -1 || line == "") continue;
else {
break;
}
}
}
newword.typeCode = atoi(line.c_str());
int result = searchTypeByCode(newword.typeCode,0,type::count-1);
finish = clock();
time = (float)(finish-start)/CLOCKS_PER_SEC;
cout << " Ban chon the loai " << tabletype[result].typeName << endl;
flag = false;
do{ if(flag==true && line!="") cout << " Tu nay da co trong danh sach."<<endl;
cout << " Nhap ten tu moi: ";
getline(cin,line);
start = clock();
line = checkInput(line);
flag=true;
if(searchWordByName(line,0,word::count-1) ==-1) break;
}while(line==""||searchWordByName(line,0,word::count-1)!=-1);
newword.name = line;
int soTruong = getFieldCount(newword.typeCode);
newword.valuesCount = soTruong;
finish = clock();
time = time + (float)(finish-start)/CLOCKS_PER_SEC;
for(int i = 0; i < soTruong;i++)
{
cout << " Nhap truong "<<tabletype[result].fieldName[i]<<": ";
getline(cin,line);
newword.values[i] = line;
}
start = clock();
addWord(newword);
finish = clock();
time = time+ (float)(finish-start)/CLOCKS_PER_SEC;
cout << " ----------------THEM THANH CONG---------------\n" << endl;
printf(" Them thanh cong trong khoang %.10f giay.",time);
fflush(stdin);
getchar();
system("cls");
}
void printNameWords(){
cout << " -----------------DANH SACH TU-----------------" << endl;
for(int i = 0; i < word::count; i++){
cout << " " << words[i].name << endl;
}
cout << "\n -----------------------------------------------\n" << endl;
}
void deleteWord(int i){
for(int j = i; j < word::count - 1; j++){
words[j] = words[j+1];
}
word::count--;
}
void deleteWord() {
clock_t start, finish;
double time;
system("cls");
fflush(stdin);
printLogo();
cout << "\n\n Chuc nang da chon: Xoa tu. \n\n";
printNameWords();
if(word::count==0) {
cout <<" ---------------DANH SACH TRONG-----------\n\n";
return;
}
while(1){
string line;
cout << "\n Nhap Tu muon xoa hoac nhap 0 de quay tro lai: ";
getline(cin,line);
start = clock();
line = checkInput(line);
if(line == "0") {
system("cls");
break;
}
int rs = searchWordByName(line,0,word::count-1);
if(rs != -1){
system("cls");
printLogo();
cout << "\n\n Chuc nang da chon: Xoa tu. \n\n";
printNameWords();
deleteWord(rs);
cout << " ---------------------DA XOA--------------------";
}
else {
system("cls");
printLogo();
cout << "\n\n Chuc nang da chon: Xoa tu. \n\n";
printNameWords();
cout << " -----------KHONG TIM THAY TU MUON XOA----------";
}
finish=clock();
time = (float)(finish-start)/CLOCKS_PER_SEC;
printf("\n Thao tac xong trong khoang %.10f giay.\n\n",time);
}
system("cls");
}
// Tra ve -1 neu khong tim thay
// tra ve vi tri thu i
// tim kiem nhi phan
int searchWordByName(string name, int first, int last)
{
if(first == last){
if(words[last].name == name) return last;
else return -1;
}
int mid = (first + last)/2;
if(name == words[mid].name) return mid;
if(name > words[mid].name) return searchWordByName(name,mid+1,last);
if(name < words[mid].name) return searchWordByName(name,first,mid);
}
int searchTypeByName(string name){
tabletype[type::count].typeName = name;
int i = 0;
while(name != tabletype[i].typeName){
i++;
}
if(i != type::count) return i;
else return -1;
}
int searchTypeByCode(int code, int first, int last)
{
if(first == last){
if(tabletype[last].typeCode == code) return last;
else return -1;
}
int mid = (first + last)/2;
if(code == tabletype[mid].typeCode) return mid;
if(code > tabletype[mid].typeCode) return searchTypeByCode(code,mid+1,last);
if(code < tabletype[mid].typeCode) return searchTypeByCode(code,first,mid);
}
void Swap(word &w1, word &w2){
word newword = w1;
w1 = w2;
w2 = newword;
}
void editNameWord(int &i){
clock_t start, finish;
double time;
string line;
bool flag = false;
while(1){
if( flag == true ){
if( line != "") cout << "\n Tu nay da co trong danh sach: ";
else cout << "\n Khong duoc de trong ten tu: ";
}
fflush(stdin);
cout << "\n Nhap ten tu hoac nhap 0 de thoat: ";
getline(cin,line);
flag = true;
start=clock();
line = checkInput(line);
if(line == "0") return;
if(line == "") continue;
int rs = searchWordByName(line,0,word::count-1);
if(rs != -1) continue;
words[i].name = line;
if (words[i-1].name > words[i].name || words[i].name > words[i+1].name){
while(words[i-1].name > words[i].name){
Swap(words[i-1],words[i]);
i--;
if(i == 0 || i == word::count) break;
}
while(words[i].name > words[i+1].name){
Swap(words[i],words[i+1]);
i++;
if(i == -1 || i == word::count) break;
}
if(i == 0 || i == word::count) break;
}
finish = clock();
time = (float)(finish-start)/CLOCKS_PER_SEC;
cout << "\n --------------DA CAP NHAT THANH CONG--------------\n";
printf("\n Thao tac xong trong khoang %.10f giay.\n\n",time);
getchar();
break;
}
}
void editTypeWord(int &i){
clock_t start, finish;
double time;
string line;
int code = words[i].typeCode;
int k = searchTypeByCode(code,0,type::count-1);
while(1){
fflush(stdin);
cout << "\n The loai hien tai: " << tabletype[k].typeName ;
cout << "\n Nhap ten the loai moi khac voi the loai truoc do: ";
getline(cin,line);
start = clock();
line = checkInput(line);
if(line == "") continue;
int rs1 = searchTypeByName(line);
if(rs1 == -1){
cout << " The loai khong ton tai!" << endl;
continue;
}
else{
cout << " Ma the loai da chon: " << tabletype[rs1].typeCode << endl;
words[i].typeCode = tabletype[rs1].typeCode;
words[i].valuesCount = tabletype[rs1].fieldcount;
finish = clock();
time = (float)(finish-start)/CLOCKS_PER_SEC;
for(int j = 0; j < tabletype[rs1].fieldcount; j++){
cout << " Nhap truong " << tabletype[rs1].fieldName[j] << ": ";
string line;
getline(cin,line);
start = clock();
line = checkInput(line);
words[i].values[j] = line;
finish = clock();
time = time + (float)(finish-start)/CLOCKS_PER_SEC;
}
break;
}
}
cout << "\n --------------DA CAP NHAT THANH CONG--------------\n";
printf("\n Thao tac xong trong khoang %.10f giay.\n\n",time);
getchar();
system("cls");
}
void editValuesWord(int &i){
clock_t start, finish;
double time;
start = clock();
//system("cls");
int rs = searchTypeByCode(words[i].typeCode,0,type::count);
cout << "\n Ma the loai cua tu : " << tabletype[rs].typeCode << endl;
finish = clock();
time = (float)(finish-start)/CLOCKS_PER_SEC;
for(int j = 0; j < tabletype[rs].fieldcount; j++){
system("cls");
printLogo();
cout << "\n\n Chuc nang da chon: Sua tu. \n\n";
cout << "\n ------------------TU DINH SUA-----------------\n\n";
printWord(i);
cout << "\n -----------------------------------------------\n\n";
cout << " Nhap truong " << tabletype[rs].fieldName[j] << ", hoac nhan ENTER de thoat: ";
string line;
getline(cin,line);
start = clock();
if(line == "") break;
line = checkInput(line);
words[i].values[j] = line;
finish = clock();
time = time + (float)(finish-start)/CLOCKS_PER_SEC;
}
system("cls");
printLogo();
cout << "\n\n Chuc nang da chon: Sua tu. \n\n";
cout << "\n ------------------TU DINH SUA-----------------\n\n";
printWord(i);
cout << "\n --------------DA CAP NHAT THANH CONG--------------\n";
printf("\n Thao tac xong trong khoang %.10f giay.\n\n",time);
getchar();
system("cls");
}
void editWord(int i){
while(1){
system("cls");
printLogo();
cout << "\n\n Chuc nang da chon: Sua tu. \n\n";
cout << "\n ------------------TU DINH SUA-----------------\n\n";
printWord(i);
cout << "\n -----------------------------------------------\n\n";
cout << "\n Chon chuc nang hoac nhap 0 de quay tro lai\n 1.Ten tu\n 2.The loai\n 3.Cac truong\n Nhap vao mot so 1-3 de chon cac chuc nang: ";
string line;
getline(cin,line);
line = checkInput(line);
if(line != "0" && line != "1" && line != "2" && line != "3" ){
system("cls");
cout << "\n Vui long nhap so hop le: ";
continue;
}
else if(line == "0") {
system("cls");
break;
}
else if(line == "1") {
editNameWord(i);
cout << "\n Chon chuc nang hoac nhap 0 de quay tro lai\n 1.Ten tu\n 2.The loai\n 3.Cac truong\n Nhap vao mot so 1-3 de chon cac chuc nang: ";
}
else if(line == "2") {
editTypeWord(i);
cout << "\n Chon chuc nang hoac nhap 0 de quay tro lai\n 1.Ten tu\n 2.The loai\n 3.Cac truong\n Nhap vao mot so 1-3 de chon cac chuc nang: ";
}
else if(line == "3") {
editValuesWord(i);
cout << "\n Chon chuc nang hoac nhap 0 de quay tro lai\n 1.Ten tu\n 2.The loai\n 3.Cac truong\n Nhap vao mot so 1-3 de chon cac chuc nang: ";
}
system("cls");
}
}
void editWord(){
fflush(stdin);
while(1){
system("cls");
printLogo();
cout << "\n\n Chuc nang da chon: Sua tu. \n\n";
if(word::count == 0) {
cout << "<------------------------DANH SACH TRONG---------------------->" << endl;
return;
}
string line;
fflush(stdin);
cout << " Nhap Tu muon cap nhat hoac 0 de quay lai: ";
getline(cin,line);
line = checkInput(line);
if(line == "0") break;
int delWordPos = searchWordByName(line,0,word::count);
if(delWordPos == -1){
cout << "\n\n ------------KHONG TIM THAY TU MUON SUA---------\n\n";
getchar();
continue;
}
cout << "\n ------------------TU DINH SUA-----------------\n\n";
printWord(delWordPos);
cout << "\n -----------------------------------------------\n\n";
editWord(delWordPos);
}
system("cls");
}
void smartTableTypes(){
MAXTYPES = MAXTYPES * 2;
type *temp = new type[MAXTYPES];
for(int i = 0; i < type::count; i++){
temp[i] = tabletype[i];
}
tabletype = temp;
}
void addType(type newtype) {
if(type::count == MAXTYPES - 1){
smartTableTypes();
}
for(int i = 0; i <= type::count; i++){
if(newtype.typeCode < tabletype[i].typeCode || i == type::count){
for(int j = type::count; j > i; j--){
tabletype[j] = tabletype[j-1];
}
tabletype[i] = newtype;
break;
}
}
type::count ++;
}
void addType(){
clock_t start, finish;
double time;
fflush(stdin);
printLogo();
cout << "\n\n Chuc nang da chon: Them the loai." << endl;
printTypes();
cout <<"\n -------------------------------------------------\n"<<endl;
type newtype;
string line;
bool flag = false;
while(1){
if( flag == true ){
if (line !="0" && atoi(line.c_str()) ==0) cout << "\n Khong duoc nhap chu, vui long nhap so!\n";
else cout << "\n Ma the loai nay da co trong danh sach!\n";
}
cout << " Nhap ma the loai chua co trong danh sach: ";
getline(cin,line);
flag = true;
line = checkInput(line);
int code = atoi(line.c_str());
start = clock();
int kiemtra = searchTypeByCode(code,0,type::count);
if(code > 0 && kiemtra == -1){
newtype.typeCode = code;
finish = clock();
break;
}
}
time = (float)(finish-start)/CLOCKS_PER_SEC;
do{ fflush(stdin);
cout << " Nhap ten the loai chua co trong danh sach: ";
getline(cin,line);
start = clock();
line = checkInput(line);
if(searchTypeByName(line)==-1) finish = clock();
} while(line == "" || searchTypeByName(line) != -1);
time = time + (float)(finish-start)/CLOCKS_PER_SEC;
newtype.typeName = line;
fflush(stdin);
cout << " Nhap cac truong (Phan cach bang dau ';'): ";
getline(cin,line);
start = clock();
line = checkInput(line);
stringArr res = divStringSemicolon(line);
for(int i = 0; i < res.count;i++) {
newtype.fieldName[i] = res.lines[i];
}
newtype.fieldcount = res.count;
addType(newtype);
finish = clock();
time = time + (float)(finish-start)/CLOCKS_PER_SEC;
cout <<"\n ----------------------DA THEM---------------------\n"<<endl;
printf("\n Thao tac xong trong khoang %.10f giay.",time);
fflush(stdin);
getchar();
system("cls");
}
void quickSortTypeCode(int L, int R) {
int t= tabletype[(L+R)/2].typeCode;
int i=L;
int j=R;
struct type temp;
do { while(tabletype[i].typeCode <t) i++;
while(tabletype[j].typeCode >t) j--;
if(i<=j) { temp = tabletype[i];
tabletype[i]=tabletype[j];
tabletype[j]=temp;
i++;
j--;
}
}while(i<j);
if(L<j) quickSortTypeCode(L,j);
if(i<R) quickSortTypeCode(i,R);
}
void printTypeName(int &i){
cout << "\n --------------------------------------------------\n\n";
cout <<" Ten the loai: " << tabletype[i].typeName;
cout <<"\n Ma the loai: " << tabletype[i].typeCode;
cout <<"\n Cac truong: ";
for(int j = 0; j < tabletype[i].fieldcount;j++)
{
cout << tabletype[i].fieldName[j] << "; ";
}
cout << "\n\n ---------------------------------------------------\n\n";
}
void editType(){
clock_t start, finish;
double time;
system("cls");
fflush(stdin);
printLogo();
cout << "\n\n Chuc nang da chon: Sua the loai. \n\n";
string line;
int code;
int code1;
bool flag = false;
printTypes();
do{ if(flag == true ) cout << "\n The loai nay khong ton tai! \n";
cout << " Nhap ten the loai muon sua: ";
getline(cin,line);
flag = true;
start = clock();
line = checkInput(line);
}while(searchTypeByName(line)==-1);
int result = searchTypeByName(line);
system("cls");
printLogo();
cout << "\n\n Chuc nang da chon: Sua the loai. \n\n";
printTypeName(result);
code = tabletype[result].typeCode;
finish = clock();
time = (float)(finish-start)/CLOCKS_PER_SEC;
if(result == -1)
{
cout << "\n ----------KHONG TIM THAY TEN THE LOAI----------\n\n";
getchar();
system("cls");
return;
}
cout << "\n Chon c/k? de sua hoac khong\n\n";
do {fflush(stdin);
cout << " Ban muon sua ten the loai: c/k? ";
getline(cin,line);
line = checkInput(line);
if(line == "c" || line == "k") break;
}while(1);
if(line == "c"){
do{
system("cls");
printLogo();
cout << "\n\n Chuc nang da chon: Sua the loai. \n\n";
printTypeName(result);
cout << " Nhap ten the loai chua co trong danh sach: ";
getline(cin,line);
start= clock();
line = checkInput(line);
}while(line == "" || searchTypeByName(line)!=-1);
tabletype[result].typeName = line;
finish = clock();
}
time = time + (float)(finish-start)/CLOCKS_PER_SEC;
system("cls");
printLogo();
cout << "\n\n Chuc nang da chon: Sua the loai. \n\n";
printTypeName(result);
do { fflush(stdin);
cout << " Ban muon sua ma the loai: c/k? ";
getline(cin,line);
line = checkInput(line);
if(line == "c" || line == "k") break;
}while(1);
if(line == "c") {
flag = false;
do{ system("cls");
printLogo();