#include #include #include #include typedef struct tagWord { char Eng[30]; char Vie[100]; }Word; typedef struct tagTNode { Word Key; struct tagTNode *pLeft; struct tagTNode *pRight; }TNode; typedef TNode *TREE; void CreateTree(TREE &T) { T = NULL; } TNode *CreateTNode(Word x) { TNode *p; p = new TNode; if (p != NULL) { p->Key = x; p->pLeft = NULL; p->pRight = NULL; } return p; } int insertNode(TREE &T, Word X) { int t; if (T) { t = strcmp(T->Key.Eng, X.Eng); if (t == 0) { strcpy(T->Key.Vie, X.Vie); return 0; } if (t == -1) return insertNode(T->pLeft, X); else return insertNode(T->pRight, X); } T = new TNode; if (T == NULL) return -1; T->Key = X; T->pLeft = T->pRight = NULL; return 1; } TNode * searchNode(TREE Root, char *x) { TNode *p = Root; int t; while (p != NULL) { t = strcmp(x, p->Key.Eng); if (t == 0) return p; else if (t == 1) p = p->pLeft; else p = p->pRight; } return NULL; } void XuatTu(Word W) { printf("%s\t:%s\n", W.Eng, W.Vie); } void LNR(TREE T) { if (T) { LNR(T->pLeft); XuatTu(T->Key); LNR(T->pRight); } } void WriteData(TREE T, FILE *f) { if (T != NULL) { WriteData(T->pLeft, f); fwrite((char *)&T->Key, sizeof(T->Key), 1, f); fflush(f); WriteData(T->pRight, f); } } void Write(TREE T) { FILE *f; f = fopen("D:\data.txt", "wb"); if (f == NULL) return; WriteData(T, f); fflush(f); fclose(f); } void ReadMemory(TREE &T) { FILE *f; f = fopen("D:\data.txt", "rb"); Word W; while (!feof(f)) { fread((char *)&W, sizeof(W), 1, f); insertNode(T, W); } fclose(f); } void ReadFile(TREE &Root, char path[] = "D:/TuDienAV.txt") { FILE *f; f = fopen(path, "rt"); char text[100]; int i, j, len; Word w; while (!feof(f)) { fgets(text, 99, f); len = strlen(text); for (i = 0; i < len; i++) { w.Eng[i] = text[i]; if (text[i] == '#') { w.Eng[i] = '\0'; break; } } for (j = 0, i = i + 1; i <= len; j++, i++) { w.Vie[j] = text[i]; } insertNode(Root, w); } } void Menu() { system("cls"); puts("TU DIEN ANH VIET"); puts("\t1. Read File"); puts("\t2. Update"); puts("\t3. Search"); puts("\t4. Write Data"); puts("\t5. Exit"); puts("\t\tInput Number: "); } void Run(TREE &Root) { int Num; TNode *p = NULL; char path[50]; char Eng[30]; do { Menu(); Num = getch(); switch (Num) { case '1': system("cls"); puts("\t\t\tComplete!!!"); ReadFile(Root); getch(); break; case '2': system("cls"); fflush(stdin); puts("\t\t\tInput Path: "); gets(path); ReadFile(Root, path); puts("\t\t\tComplete!!!"); getch(); break; case '3': system("cls"); puts("\t\t\tInput: "); fflush(stdin); gets(Eng); p = searchNode(Root, Eng); if (p == NULL) { puts("Error"); } else { XuatTu(p->Key); } getch(); break; case '4': system("cls"); Write(Root); puts("\t\t\tComplete!!!"); getch(); break; } } while (Num != '5'); } void main() { TREE Root; CreateTree(Root); Run(Root); }