用C语言字典实现词典功能 C语言字典词典

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAXWORD 25
  5. #define MAXMEAN 50
  6. struct record { //记录结构_读者
  7. char word[MAXWORD+1]; //key
  8. char mean[MAXMEAN+1];
  9. };
  10. struct lnode { //链表结点结构
  11. struct record data;
  12. struct lnode *next;
  13. };
  14. /* 函数声明 */
  15. void Add(struct lnode *list);
  16. void Search(struct lnode *list);
  17. void Edit(struct lnode *list);
  18. void Delete(struct lnode *list);
  19. void Display(struct lnode *list);
  20. struct lnode *SearchPrimarykey(struct lnode *list, char *key);
  21. void InsertList(struct lnode *list, struct lnode *n);
  22. void FreeList(struct lnode *list);
  23. void DisplayTableHead(void);
  24. void DisplayRecord(struct lnode *r);
  25. void DisplayMenu(void);
  26. /* 主程序 */
  27. int main(int argc, char *argv[])
  28. {
  29. struct lnode *dictionary;
  30. /* 功能选择,依次为:退出、添加、查找、删除、显示所有记录 */
  31. enum {EXIT, ADD, SEARCH, EDIT, DEL, DISP} function = DISP;
  32. /* 头结点 */
  33. dictionary = (struct lnode *)malloc(sizeof(struct lnode));
  34. if(dictionary != NULL) {
  35. dictionary->next = NULL; //初始化
  36. }
  37. while(function != EXIT) {
  38. DisplayMenu();
  39. scanf("%d",&function);
  40. while(function < EXIT || function > DISP) {
  41. scanf("%d",&function);
  42. }
  43. switch(function) {
  44. case ADD:
  45. Add(dictionary);
  46. break;
  47. case SEARCH:
  48. Search(dictionary);
  49. break;
  50. case EDIT:
  51. Edit(dictionary);
  52. break;
  53. case DEL:
  54. Delete(dictionary);
  55. break;
  56. case DISP:
  57. Display(dictionary);
  58. break;
  59. case EXIT:
  60. exit(0);
  61. break;
  62. default:
  63. printf("Input Error! Please input the right word.");
  64. break;
  65. }
  66. }
  67. FreeList(dictionary);
  68. }
  69. /* 添加 */
  70. void Add(struct lnode *list)
  71. {
  72. int i;
  73. struct record t;
  74. struct lnode *n, *r;
  75. /* 录入记录 */
  76. printf("Please input the word: ");
  77. getchar();
  78. gets(t.word);
  79. fflush(stdin);
  80. printf("Please input the meaning:");
  81. gets( t.mean);
  82. /* 判断记录是否已存在,若存在则显示记录,若不存在则添加记录 */
  83. if((r = SearchPrimarykey(list, t.word)) == NULL) {
  84. /* 申请lnode空间并初始化 */
  85. n = (struct lnode *)malloc(sizeof(struct lnode));
  86. if(n != NULL) {
  87. /* 复制记录 */
  88. strcpy((n->data).word,t.word);
  89. strcpy((n->data).mean, t.mean);
  90. /* 插入链表 */
  91. InsertList(list, n);
  92. }
  93. } else {
  94. printf("Record Existed!\n");
  95. DisplayTableHead();
  96. DisplayRecord(r);
  97. }
  98. }
  99. /* 修改 */
  100. void Edit(struct lnode *list)
  101. {
  102. struct record t;
  103. struct lnode *r, *p;
  104. char e[MAXWORD];
  105. p = list;
  106. printf("Please input the word you want to edit: ");
  107. getchar();
  108. gets(e);
  109. if((r = SearchPrimarykey(list, e)) != NULL) {
  110. fflush(stdin);
  111. printf("Please edit the word: ");
  112. gets(t.word);
  113. printf("Please edit the meaning:");
  114. gets(t.mean);
  115. /* 复制记录 */
  116. strcpy((r->data).word,t.word);
  117. strcpy((r->data).mean,t.mean);
  118. } else {
  119. printf("Record cann't find!\n");
  120. }
  121. }
  122. /* 查找 */
  123. void Search(struct lnode *list)
  124. {
  125. char e[MAXWORD];
  126. struct lnode *r;
  127. printf("Please input the word you want to search: ");
  128. getchar();
  129. gets(e);
  130. if((r = SearchPrimarykey(list, e)) != NULL) {
  131. DisplayTableHead();
  132. DisplayRecord(r);
  133. } else {
  134. printf("Cann't find the word.");
  135. }
  136. }
  137. /* 删除 */
  138. void Delete(struct lnode *list)
  139. {
  140. char e[MAXWORD];
  141. struct lnode *q, *p;
  142. q = list;
  143. p = list->next;
  144. printf("Please input the word you want to delete: ");
  145. getchar();
  146. gets(e);
  147. while(p != NULL) {
  148. if(strcmp((p->data).word, e) == 0) {
  149. q->next = p->next;
  150. free(p); /* 释放空间 */
  151. return ; /* 函数返回 */
  152. }
  153. q = p;
  154. p = p->next;
  155. }
  156. }
  157. /* 显示所有记录 */
  158. void Display(struct lnode *list)
  159. {
  160. int c = 0;
  161. struct lnode *p = list->next;
  162. printf("\n--------- ReaderMessage Display ---------\n");
  163. DisplayTableHead();
  164. while(p != NULL) {
  165. DisplayRecord(p);
  166. c++; /* 记录条数 */
  167. p = p->next;
  168. }
  169. printf("\n--------- Total: %d Record(s) ---------\n",c);
  170. }
  171. /* 按主键查找 */
  172. struct lnode *SearchPrimarykey(struct lnode *list, char *key)
  173. {
  174. struct lnode *p = list->next;
  175. while (p != NULL) {
  176. if(strcmp((p->data).word, key) == 0) {
  177. return p;
  178. }
  179. p = p->next;
  180. }
  181. return NULL;
  182. }
  183. /* 将记录按姓名字母升序插入链表 */
  184. void InsertList(struct lnode *list, struct lnode *n)
  185. {
  186. struct lnode *p = list;
  187. while (p->next != NULL && strcmp((p->next->data).word, (n->data).word) < 0)
  188. {
  189. p = p->next;
  190. }
  191. n->next = p->next;
  192. p->next = n;
  193. }
  194. /* 释放整个链表空间 */
  195. void FreeList(struct lnode *list)
  196. {
  197. struct lnode *p = list;
  198. while(p->next != NULL) {
  199. p = p->next;
  200. free(list);
  201. list = p;
  202. }
  203. free(p);
  204. }
  205. /* 显示表头 */
  206. void DisplayTableHead(void) {
  207. printf("%-10s %s\n","WORD","MEANING");
  208. }
  209. /* 显示一条记录 */
  210. void DisplayRecord(struct lnode *r)
  211. {
  212. printf("%-10s %s\n", (r->data).word, (r->data).mean);
  213. }
  214. /* 显示菜单 */
  215. void DisplayMenu(void)
  216. {
  217. printf("\n--------- ReaderMessage Menu ---------\n");
  218. printf("\n\t1.Add\n\t2.Search\n\t3.Edit\n\t4.Del\n\t5.Display\n\t0.Exit\n");
  219. printf("\nPlease select the function number(0-5):");
  220. }

发布了19 篇原创文章 · 获赞 3 · 访问量 2410
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览