-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDICTIONARY.h
538 lines (483 loc) · 17.2 KB
/
DICTIONARY.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
#include <iostream>
#include <fstream>
#include <windows.h>
#include <conio.h>
#include <string>
#include "Word.h"
#include "HashTable.h"
#include "Node.h"
#include "LinkedList.h"
#define CR_KTK 50
#ifndef DEF_DICTIONARY
#define DEF_DICTIONARY
using namespace std;
class DICTIONARY {
private:
HashTable TUDIEN;
public:
DICTIONARY(const char *fileName); // Ham khoi tao voi file dau vao
virtual ~DICTIONARY(); // Ham huy tu dien
void veGiaoDienChinh(string input); // Ham ve giao dien tim tu vung
void veGiaoDienChiTietTu(Word m); // Ham ve giao dien chi tiet tu vung
void giaoDienMoUngDung(); // Ham ve giao dien cho ung dung
void suaTu(Word &w); // Ham sua tu vung
void themTuMoi(); // Ham nhap tu vung
void xuLyTuDien(); // Xu ly tu dien
void timTuGoiY(Word *a, string input, int pos, int &size); // Ham tim tu goi y
void veLoiKhongTimThayTu(); // Ham tra ve giao dien khong tim thay tu
void veGoiY(int pos); // Ham ve giao dien khong tim thay tu
// interface graphics
void xoaManHinh(); // Xoa man hinh
void gotoxy(short x, short y); // Dat con tro tai toa do (x,y)
void kichThuocCuaSo(SHORT width, SHORT height); // Thay doi kich thuoc cua so
void anThanhCuon(BOOL Show); // Ham an thanh scroll bar
void voHieuHoaKichThuocCuaSo(); // Ham vo hieu hoa thay doi kich thuoc man hinh
void doiMau(short x); // Ham thay doi textcolor va backgroundcolor
void docFile(const char *fileName ); // Doc file
};
DICTIONARY::DICTIONARY(const char *fileName){
SetConsoleTitle(" English-Vietnamese Dictionary ");
this->docFile(fileName);
this->kichThuocCuaSo(51,44);
this->anThanhCuon(0);
this->voHieuHoaKichThuocCuaSo();
this->giaoDienMoUngDung();
this->xuLyTuDien();
}
void DICTIONARY::giaoDienMoUngDung(){
this->gotoxy(0,9);
this->doiMau(10);
// Ve logo ung dung
cout << " " << char(218);
for(int i = 1 ; i <= 27 ; i++ ) cout << char(196);
cout << char(191) << endl;
cout << " " << char(179) << " ------------------------- " << char(179)<< endl;
cout << " " << char(179) << " "; this->doiMau(12); cout << "#########"; this->doiMau(10); cout<< " --------------- " << char(179)<< endl;
cout << " " << char(179) << " "; this->doiMau(12); cout << "# "; this->doiMau(10); cout<< " --------------- " << char(179)<< endl;
cout << " " << char(179) << " "; this->doiMau(12); cout << "# "; this->doiMau(10); cout<< " --------------- " << char(179)<< endl;
cout << " " << char(179) << " "; this->doiMau(12); cout << "#########"; this->doiMau(10); cout<< " -"; this->doiMau(12); cout <<" # # "; this->doiMau(10); cout << char(179)<< endl;
cout << " " << char(179) << " "; this->doiMau(12); cout << "# "; this->doiMau(10); cout<< " --"; this->doiMau(12); cout <<" # ";this->doiMau(14); cout << char(3); this->doiMau(12); cout << " # "; this->doiMau(10); cout << char(179)<< endl;
cout << " " << char(179) << " "; this->doiMau(12); cout << "# "; this->doiMau(10); cout<< " ---"; this->doiMau(12); cout <<" # # "; this->doiMau(10); cout <<"- " << char(179)<< endl;
cout << " " << char(179) << " "; this->doiMau(12); cout << "#########"; this->doiMau(10); cout<< " ----"; this->doiMau(12); cout <<" # # "; this->doiMau(10); cout <<"-- " << char(179)<< endl;
cout << " " << char(179) << " ---------------"; this->doiMau(12); cout <<" # # "; this->doiMau(10); cout <<"--- " << char(179)<< endl;
cout << " " << char(179) << " ----------------"; this->doiMau(12); cout <<" # # "; this->doiMau(10); cout <<"---- " << char(179)<< endl;
cout << " " << char(179) << " -----------------"; this->doiMau(12); cout <<" # "; this->doiMau(10); cout <<"----- " << char(179)<< endl;
cout << " " << char(179) << " ------------------------- " << char(179)<< endl;
cout << " " << char(192);
for(int i = 1 ; i <= 27 ; i++ ) cout << char(196);
cout << char(217)<< endl << endl;
// Ve thong tin ung dung
this->doiMau(15);
cout << " ------ ENGLISH - VIETNAMESE DICTIONARY ------";
this->doiMau(14);
this->gotoxy(0,40);
cout << " from " << endl;
cout << " ITF - Inc" << endl;
Sleep(3500);
}
DICTIONARY::~DICTIONARY(){
this->TUDIEN.~HashTable();
}
void DICTIONARY::veGiaoDienChinh(string input) {
this->xoaManHinh();
// in ra cac thong tin ve de tai do an
this->doiMau(12);
cout<<endl;
cout<<" DAI HOC BACH KHOA - DAI HOC DA NANG " << endl;
cout<<" ------------------------------" << endl;
cout<<" KHOA CONG NGHE THONG TIN" << endl << endl << endl;
doiMau(10);
cout<<" DO AN PBL2 : CO SO LAP TRINH"<<endl;
cout<<" Giao vien huong dan: TS.Le Thi My Hanh"<<endl;
cout<<" Sinh vien thuc hien: +/ Ho Duc Hoang - 20T2 "<<endl;
cout<<" +/ Do Minh Quan - 20T2 "<<endl;
cout<<endl;
// in ra cac option tuy chon khi thao tac voi tu dien
this->doiMau(12);
cout << " " << "TU DIEN ANH - VIET" << endl;
this->doiMau(14);
cout << " " << char(254) << " Esc : Thoat chuong trinh" << endl;
cout << " " << char(254) << " Tab : Them tu moi" << endl;
cout << " " << char(254) << " Enter: Xem nghia cua tu" << endl;
this->doiMau(11);
// ve khung tim kiem
cout << char(218); for (int i = 0; i < CR_KTK; i++) cout << char(196); cout << char(191) << endl;
cout << char(179) << input; for (int i = 0; i < CR_KTK - input.size(); i++) cout << char(255); cout << char(179);
cout << endl << char(192); for (int i = 0; i < CR_KTK; i++) cout << char(196); cout << char(217) << endl;
// dat con tro tai vi tri thanh tim kiem
gotoxy(1 + input.size(), 9);
}
void DICTIONARY::veGiaoDienChiTietTu(Word m) {
this->xoaManHinh();
int dongHienTai = 0;
// in ra cac huong dan
this->doiMau(10);
cout << " " << char(254) << " Esc : Ve lai man hinh chinh" << endl;
cout << " " << char(254) << " Tab : Sua tu nay" << endl;
doiMau(14);
// in ra cac chi tiet cua tu
cout << m;
}
void DICTIONARY::veLoiKhongTimThayTu(){
this->xoaManHinh();
int dongHienTai = 0;
// in ra cac huong dan
this->doiMau(10);
cout << " " << char(254) << " Esc : Ve lai man hinh chinh" << endl;
cout << " " << char(254) << " Tab : Them tu moi" << endl;
this->doiMau(12);
cout << "\n " << char(19) << " Loi! Khong co tu nay" << endl;
}
void DICTIONARY::suaTu(Word &w) {
Word tmp = w;
// Qua trinh sua tu vung
while (1) {
string temp = "";
this->xoaManHinh();
char nhaplai;
// Chi tiet tu vung hien tai
this->doiMau(12);
cout << w;
this->doiMau(10);
cout << " " << char(3);
this->doiMau(14);
cout << " Sua tu : " << w.getWord() << endl;
// Sua loai tu
while (1){
this->doiMau(14);
cout << " " << char(16) << " Sua loai tu : ";
fflush(stdin);
getline(cin, temp);
if (temp == "") {
this->doiMau(12);
cout << " " << char(19) << "/ Ki tu khong hop le !! Ban co muon nhap lai khong (c/k) ? ";
cin >> nhaplai;
if (nhaplai == 'c' || nhaplai == 'C') continue;
}
if(temp != "") w.setType(temp);
break;
}
// Sua nghia tu
while (1){
this->doiMau(14);
cout << " " << char(16) << " Sua nghia : ";
fflush(stdin);
getline(cin, temp);
if (temp == "") {
this->doiMau(12);
cout << " " << char(19) << "/ Ki tu khong hop le !! Ban co muon nhap lai khong (c/k) ? ";
cin >> nhaplai;
if (nhaplai == 'c' || nhaplai == 'C') continue;
}
if(temp != "") w.setMean(temp);
break;
}
// Sua vi du
while (1){
this->doiMau(14);
cout << " " << char(16) << " Sua vi du : ";
fflush(stdin);
getline(cin, temp);
if (temp == "") {
this->doiMau(12);
cout << " " << char(19) << "/ Ki tu khong hop le !! Ban co muon nhap lai khong (c/k) ? ";
cin >> nhaplai;
if (nhaplai == 'c' || nhaplai == 'C') continue;
}
if(temp != "") w.setExample(temp);
break;
}
// Hien thi lai tu vung sau khi sua
this->xoaManHinh();
doiMau(10);
cout << w;
// Kiem tra chac chan chua
this->doiMau(14);
cout << " <!!!> Ban co chac chan thay doi khong (c/k) ? "; cin >> nhaplai;
if (nhaplai == 'c' || nhaplai == 'C') break;
else {
w = tmp;
continue;
}
}
// Cap nhat tu vung sau khi chinh sua
this->TUDIEN.update(w);
// In ra thanh thong bao Da Sua Thanh Cong
this->xoaManHinh();
this->gotoxy(0,20);
this->doiMau(14);
cout << " " << char(218); for (int i = 0; i < 23; i++) cout << char(196); cout << char(191) << endl;
cout << " " << char(179); this->doiMau(10); cout << " DA SUA THANH CONG ! "; this->doiMau(14); cout << char(179) << endl;
cout << " " << char(192); for (int i = 0; i < 23; i++) cout << char(196); cout << char(217) << endl;
Sleep(1500);
}
void DICTIONARY::themTuMoi() {
// Mo file
FILE *f;
f = fopen("words.txt", "a+");
Word w;
// Qua trinh them tu moi
while (1) {
string temp = "";
this->xoaManHinh();
char nhaplai;
this->doiMau(12);
cout << "\n " << char(3);
this->doiMau(10);
cout << " Them tu vung : " << endl << endl;
// Nhap tu
while (1){
this->doiMau(14);
cout << " " << char(16) << " Nhap tu vung : ";
fflush(stdin);
getline(cin, temp);
if (temp == "") {
this->doiMau(12);
cout << " " << char(19) << "/ Ki tu khong hop le !! Ban co muon nhap lai khong (c/k) ? ";
cin >> nhaplai;
if (nhaplai == 'c' || nhaplai == 'C') continue;
}
if (temp == "") w.setWord("null");
else w.setWord(temp);
break;
}
// Nhap loai Tu
while (1){
this->doiMau(14);
cout << " "<< char(16) << " Nhap loai tu : ";
fflush(stdin);
getline(cin, temp);
if (temp == "") {
this->doiMau(12);
cout << " " << char(19) << "/ Ki tu khong hop le !! Ban co muon nhap lai khong (c/k) ? ";
cin >> nhaplai;
if (nhaplai == 'c' || nhaplai == 'C') continue;
}
if (temp == "") w.setType("null");
else w.setType(temp);
break;
}
// Nhap Nghia Tu
while (1){
this->doiMau(14);
cout << " "<< char(16) << " Nhap nghia tu vung : ";
fflush(stdin);
getline(cin, temp);
if (temp == "") {
this->doiMau(12);
cout << " " << char(19) << "/ Ki tu khong hop le !! Ban co muon nhap lai khong (c/k) ? ";
cin >> nhaplai;
if (nhaplai == 'c' || nhaplai == 'C') continue;
}
if (temp == "") w.setMean("null");
else w.setMean(temp);
break;
}
// Nhap Vi Du
while (1){
this->doiMau(14);
cout << " "<< char(16) << " Nhap vi du tu vung : ";
fflush(stdin);
getline(cin, temp);
if (temp == "") {
this->doiMau(12);
cout << " " << char(19) << "/ Ki tu khong hop le !! Ban co muon nhap lai khong (c/k) ? ";
cin >> nhaplai;
if (nhaplai == 'c' || nhaplai == 'C') continue;
}
if (temp == "") w.setExample("null");
else w.setExample(temp);
break;
}
// Hien thi lai tu vung sau khi sua
doiMau(10);
this->xoaManHinh();
cout << w;
// Kiem tra chac chan chua
this->doiMau(14);
cout << " <!!!> Ban co chac chan them tu moi khong (c/k) ? "; cin >> nhaplai;
if (nhaplai == 'c' || nhaplai == 'C') break;
else continue;
}
// Chen tu moi vao bang bam
this->TUDIEN += w;
// In ra thanh thong bao Da Them Thanh Cong
this->xoaManHinh();
this->gotoxy(0,20);
this->doiMau(14);
cout << " " << char(218); for (int i = 0; i < 23; i++) cout << char(196); cout << char(191) << endl;
cout << " " << char(179); this->doiMau(10); cout <<" DA THEM THANH CONG ! "; this->doiMau(14); cout << char(179) << endl;
cout << " " << char(192); for (int i = 0; i < 23; i++) cout << char(196); cout << char(217) << endl;
Sleep(1500);
// Cap nhat vao file tu dien
string www = w.getWord()+"/"+w.getType()+"/"+w.getMean()+";/"+w.getExample()+";/";
const char *cstr = www.c_str();
fprintf(f,cstr);
fprintf(f, "\n");
fclose(f);
}
void DICTIONARY::docFile(const char* fileName) {
ifstream fi(fileName); // tim tap tin
string dong;
if (fi.is_open()) { // mo tap tin, kiem tra tap tin co ton tai hay khong
while (getline(fi, dong)) {
if (dong == "") {
continue;
}
Word w(dong);
this->TUDIEN += w; // Nap vao tu vung vao tu dien
}
fi.close(); // dong tap tin
}
}
void DICTIONARY::timTuGoiY(Word *a, string input, int pos, int &size){
// Hien thi toi da 10 goi y
size = 10;
this->TUDIEN.findByInput(a,input);
for(int i = 0; i<10 ;i++){
if(a[i].getWord() !=""){
cout<<" "<<a[i].getWord()<<endl;
} else {
size = i;
break;
}
}
}
void DICTIONARY::veGoiY(int pos){
this->gotoxy(20,pos+18);
this->doiMau(15);
cout<< char(17) << char(196) << char(196); // in ra mui ten co dang <-- tren moi goi y tu
}
void DICTIONARY::xuLyTuDien() {
Word currentWord;
string input = ""; // Khoi tao noi dung cua khung tim kiem
int keyCode = 0;
int pos = 0; // vi tri cua tu hien tai, vi tri thanh sang (highlight)
Word *tugoiy = new Word[10];
int goiy = 1; // so luong goi y tu tim duoc
string test = "";
while (true) {
this->veGiaoDienChinh(input); // ve toan bo giao dien
// Nhap tu vung
if(input != "") {
this->gotoxy(0,18);
this->timTuGoiY(tugoiy,input,pos+6,goiy);
this->veGoiY(pos);
}
// Nhap tuy chon (Enter/Tab/escape)
gotoxy(1 + input.size(), 16);
keyCode = getch(); // tam dung chuong trinh, nhan ky tu nhap vao
switch (keyCode) {
case 8: // BACKSPACE
// xoa ky tu cuoi
input = input.substr(0, input.size() - 1);
break;
case 9: // TAB
// Them tu moi
this->themTuMoi();
break;
case 13: // ENTER
// vao man hinh chi tiet tu
test = tugoiy[pos].getWord().substr(0, input.size());
if(tugoiy[pos].getWord() == ""|| input!=test) {
int k;
this->veLoiKhongTimThayTu();
do {
k = getch();
if (k == 9) { // TAB
// sua tu nay
this->themTuMoi();
input = ""; pos = 0;
break;
}
if (k == 27) { // ESCAPE
input = ""; // reset gia tri trong khung tim kiem
pos = 0;
break;
}
} while (true);
} else {
this->veGiaoDienChiTietTu(tugoiy[pos]);
int k;
do {
k = getch();
if (k == 8) { // BACKSPACE
// xoa tu nay
input = ""; pos = 0;
break;
}
if (k == 9) { // TAB
// sua tu nay
this->suaTu(tugoiy[pos]);
input = ""; pos = 0;
break;
}
if (k == 27) { // ESCAPE
// tro lai giao dien chinh
input = ""; pos = 0;
break;
}
} while (true);
}
break;
case 27: // ESCAPE
// Thoat chuong trinh , in loi cam on
this->doiMau(14);
this->gotoxy(3,21);
cout<<"----------------------------------------------";
this->gotoxy(2,22);
cout << " "; this->doiMau(12); cout << char(3) ; this->doiMau(14); cout << " Thanks for using our app! "; this->doiMau(12); cout << char(3) ; this->doiMau(14); cout << "\n Hope you had a great experience with this product";
this->gotoxy(3,24);
cout<<"----------------------------------------------";
this->gotoxy(2, 27);
this->doiMau(9);
exit(EXIT_SUCCESS); // thoat chuong trinh
break;
case 224: { // UP & DOWN
int key = getch();
if (key == 72) pos--; // UP
if (key == 80) { // DOWN
if(pos<goiy-1) pos++;
}
if (pos < 0) pos = 0;
break;
}
default: // cac ki tu trong bang chu cai
if (keyCode >= 97 && keyCode <= 122) { // 'a' <= keyCode <= 'z'
input += char(keyCode);
}
}
};
}
void DICTIONARY::xoaManHinh() {
system("cls");
}
void DICTIONARY::kichThuocCuaSo(SHORT width, SHORT height) {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT WindowSize;
WindowSize.Top = 0;
WindowSize.Left = 0;
WindowSize.Right = width;
WindowSize.Bottom = height;
SetConsoleWindowInfo(hStdout, 1, &WindowSize);
}
void DICTIONARY::anThanhCuon(BOOL Show) {
HWND hWnd = GetConsoleWindow();
ShowScrollBar(hWnd, SB_BOTH, Show);
}
void DICTIONARY::voHieuHoaKichThuocCuaSo() {
HWND hWnd = GetConsoleWindow();
SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & ~WS_SIZEBOX);
}
void DICTIONARY::doiMau(short x) {
HANDLE hConsoleColor;
hConsoleColor = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsoleColor, x);
}
void DICTIONARY::gotoxy(short x, short y) {
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = {x, y};
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}
#endif