Advertisement
huutho_96

TuDienFull

May 18th, 2015
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 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. Show Dictionary");
  147. puts("\t6. Read Data on memory");
  148. puts("\t7. Exit");
  149. puts("\t\tInput Number: ");
  150. }
  151. void DelTree(TREE &Root)
  152. {
  153. if (Root)
  154. {
  155. DelTree(Root->pLeft);
  156. DelTree(Root->pRight);
  157. delete Root;
  158. }
  159. }
  160. void Run(TREE &Root)
  161. {
  162. int Num;
  163. TNode *p = NULL;
  164. char path[50];
  165. char Eng[30];
  166. do
  167. {
  168. Menu();
  169. Num = getch();
  170. switch (Num)
  171. {
  172. case '1':
  173. system("cls");
  174. puts("\t\t\tComplete!!!");
  175. ReadFile(Root);
  176. getch();
  177. break;
  178.  
  179. case '2':
  180. system("cls");
  181. fflush(stdin);
  182. puts("\t\t\tInput Path: ");
  183. gets(path);
  184. ReadFile(Root, path);
  185. puts("\t\t\tComplete!!!");
  186. getch();
  187. break;
  188.  
  189. case '3':
  190. if (Root == NULL) ReadFile(Root);
  191. system("cls");
  192. puts("\t\t\tInput: ");
  193. fflush(stdin);
  194. gets(Eng);
  195. p = searchNode(Root, Eng);
  196. if (p == NULL)
  197. {
  198. puts("Error");
  199. }
  200. else
  201. {
  202. XuatTu(p->Key);
  203. }
  204. getch();
  205. break;
  206.  
  207. case '4':
  208. system("cls");
  209. Write(Root);
  210. puts("\t\t\tComplete!!!");
  211. getch();
  212. break;
  213.  
  214. case '5':
  215. system("cls");
  216. LNR(Root);
  217. getch();
  218. break;
  219.  
  220. case '6':
  221. system("cls");
  222. DelTree(Root);
  223. ReadMemory(Root);
  224. puts("\t\t\tComplete!!!");
  225. getch();
  226. break;
  227. }
  228. } while (Num != '7');
  229. }
  230. void main()
  231. {
  232. TREE Root;
  233. CreateTree(Root);
  234. Run(Root);
  235. DelTree(Root);
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement