Advertisement
huutho_96

TuDien

May 18th, 2015
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <Windows.h>
  5. typedef struct tagWord
  6. {
  7. char Eng[30];
  8. char Vie[100];
  9. }Word;
  10. typedef struct tagTNode
  11. {
  12. Word Key;
  13. struct tagTNode *pLeft;
  14. struct tagTNode *pRight;
  15. }TNode;
  16. typedef TNode *TREE;
  17. void CreateTree(TREE &T)
  18. {
  19. T = NULL;
  20. }
  21. TNode *CreateTNode(Word x)
  22. {
  23. TNode *p;
  24. p = new TNode;
  25. if (p != NULL)
  26. {
  27. p->Key = x;
  28. p->pLeft = NULL;
  29. p->pRight = NULL;
  30. }
  31. return p;
  32. }
  33. int insertNode(TREE &T, Word X)
  34. {
  35. int t;
  36. if (T)
  37. {
  38. t = strcmp(T->Key.Eng, X.Eng);
  39. if (t == 0)
  40. {
  41. strcpy(T->Key.Vie, X.Vie);
  42. return 0;
  43. }
  44. if (t == -1) return insertNode(T->pLeft, X);
  45. else return insertNode(T->pRight, X);
  46. }
  47. T = new TNode;
  48. if (T == NULL) return -1;
  49. T->Key = X;
  50. T->pLeft = T->pRight = NULL;
  51. return 1;
  52. }
  53. TNode * searchNode(TREE Root, char *x)
  54. {
  55. TNode *p = Root;
  56. int t;
  57. while (p != NULL)
  58. {
  59. t = strcmp(x, p->Key.Eng);
  60. if (t == 0) return p;
  61. else
  62. if (t == 1) p = p->pLeft;
  63. else p = p->pRight;
  64. }
  65. return NULL;
  66. }
  67. void XuatTu(Word W)
  68. {
  69. printf("%s\t:%s\n", W.Eng, W.Vie);
  70. }
  71. void LNR(TREE T)
  72. {
  73. if (T)
  74. {
  75. LNR(T->pLeft);
  76. XuatTu(T->Key);
  77. LNR(T->pRight);
  78. }
  79. }
  80. void WriteData(TREE T, FILE *f)
  81. {
  82. if (T != NULL)
  83. {
  84. WriteData(T->pLeft, f);
  85. fwrite((char *)&T->Key, sizeof(T->Key), 1, f);
  86. fflush(f);
  87. WriteData(T->pRight, f);
  88. }
  89. }
  90. void Write(TREE T)
  91. {
  92. FILE *f;
  93. f = fopen("D:\data.txt", "wb");
  94. if (f == NULL) return;
  95. WriteData(T, f);
  96. fflush(f);
  97. fclose(f);
  98. }
  99. void ReadMemory(TREE &T)
  100. {
  101. FILE *f;
  102. f = fopen("D:\data.txt", "rb");
  103. Word W;
  104. while (!feof(f))
  105. {
  106. fread((char *)&W, sizeof(W), 1, f);
  107. insertNode(T, W);
  108. }
  109. fclose(f);
  110. }
  111. void ReadFile(TREE &Root, char path[] = "D:/TuDienAV.txt")
  112. {
  113. FILE *f;
  114. f = fopen(path, "rt");
  115. char text[100];
  116. int i, j, len;
  117. Word w;
  118. while (!feof(f))
  119. {
  120. fgets(text, 99, f);
  121. len = strlen(text);
  122. for (i = 0; i < len; i++)
  123. {
  124. w.Eng[i] = text[i];
  125. if (text[i] == '#')
  126. {
  127. w.Eng[i] = '\0';
  128. break;
  129. }
  130. }
  131. for (j = 0, i = i + 1; i <= len; j++, i++)
  132. {
  133. w.Vie[j] = text[i];
  134. }
  135. insertNode(Root, w);
  136. }
  137. }
  138. void Menu()
  139. {
  140. system("cls");
  141. puts("TU DIEN ANH VIET");
  142. puts("\t1. Read File");
  143. puts("\t2. Update");
  144. puts("\t3. Search");
  145. puts("\t4. Write Data");
  146. puts("\t5. Exit");
  147. puts("\t\tInput Number: ");
  148. }
  149. void Run(TREE &Root)
  150. {
  151. int Num;
  152. TNode *p = NULL;
  153. char path[50];
  154. char Eng[30];
  155. do
  156. {
  157. Menu();
  158. Num = getch();
  159. switch (Num)
  160. {
  161. case '1':
  162. system("cls");
  163. puts("\t\t\tComplete!!!");
  164. ReadFile(Root);
  165. getch();
  166. break;
  167.  
  168. case '2':
  169. system("cls");
  170. fflush(stdin);
  171. puts("\t\t\tInput Path: ");
  172. gets(path);
  173. ReadFile(Root, path);
  174. puts("\t\t\tComplete!!!");
  175. getch();
  176. break;
  177.  
  178. case '3':
  179. system("cls");
  180. puts("\t\t\tInput: ");
  181. fflush(stdin);
  182. gets(Eng);
  183. p = searchNode(Root, Eng);
  184. if (p == NULL)
  185. {
  186. puts("Error");
  187. }
  188. else
  189. {
  190. XuatTu(p->Key);
  191. }
  192. getch();
  193. break;
  194.  
  195. case '4':
  196. system("cls");
  197. Write(Root);
  198. puts("\t\t\tComplete!!!");
  199. getch();
  200. break;
  201. }
  202. } while (Num != '5');
  203. }
  204. void main()
  205. {
  206. TREE Root;
  207. CreateTree(Root);
  208. Run(Root);
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement