搜索
查看: 1400|回复: 7
打印 上一主题 下一主题

分享下C语言通讯录,飞哥作品!!!!!

[复制链接]
跳转到指定楼层
楼主
发表于 2013-11-23 19:30:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5啊哈币
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. typedef struct{//定义链表数据类型
  5. char num[5]; //编号
  6. char name[8]; //姓名
  7. char sex[10]; //性别
  8. char tel[11]; //电话
  9. char address[100]; //地址
  10. }datatype;
  11. typedef struct node{//储存结构为链表
  12. datatype data;
  13. struct node * next;
  14. }listnode;
  15. typedef listnode * linklist;

  16. int menu_select( );//选择菜单
  17. linklist createlist(void);//创建链表
  18. void insertnode(linklist head,listnode *p);//添加数据
  19. listnode *listfind(linklist head);//数据查找返回找到的节点指针
  20. void delnode(linklist head);//数据删除
  21. void printlist(linklist head);//数据输出
  22. void main()
  23. {
  24. linklist head;//接受返回的链表
  25. listnode *p;//接受返回的节点
  26. for( ; ; ){
  27. switch(menu_select() )
  28. {
  29. case 1:
  30. printf(" ***********************************************\n");
  31. printf(" ************** 通讯录链表的建立 ***************\n");
  32. printf(" ***********************************************\n");
  33. head=createlist();
  34. break;
  35. case 2:
  36. printf(" ***********************************************\n");
  37. printf(" ************** 通讯录信息添加 *****************\n");
  38. printf(" ***********************************************\n");
  39. printf(" ** 编号(4) 姓名(8) 性别(10) 电话(11) 地址(30)**\n");
  40. printf(" ***********************************************\n");
  41. p=(listnode *)malloc(sizeof(listnode));
  42. scanf("%s%s%s%s%s",p->data.num,p->data.name,p->data.sex,p->data.tel,p->data.address);
  43. insertnode(head,p);
  44. break;
  45. case 3:
  46. printf(" ******************************\n");
  47. printf(" ******* 通讯录信息查询 *******\n");
  48. printf(" ******************************\n");
  49. p=listfind(head);
  50. if(p!=NULL){
  51. printf(" ** 编号 姓名 性别 电话 地址**\n");
  52. printf(" ------------------------------\n");
  53. printf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,p->data.sex,p->data.tel,p->data.address);
  54. printf(" ------------------------------\n");
  55. }
  56. else
  57. printf("没有你要找的人 ! \n");
  58. break;
  59. case 4:

  60. printf(" ***********************************\n");
  61. printf(" **********通讯信息的删除***********\n");
  62. printf(" ***********************************\n");
  63. delnode(head);
  64. break;
  65. case 5:
  66. printf(" ***********************************\n");
  67. printf(" **********通讯录链表的输出*********\n");
  68. printf(" ***********************************\n");
  69. printlist(head);
  70. break;
  71. case 0:
  72. printf("\t 谢谢您的使用! \n");
  73. return;
  74. }
  75. }
  76. }
  77. int menu_select()
  78. {
  79. int sn;
  80. printf(" \n");
  81. printf(" \n");
  82. printf(" \n");
  83. printf(" 通讯录管理系统 \n");
  84. printf(" \n");
  85. printf(" ======================\n");
  86. printf(" 1.通讯录链表的建立\n");
  87. printf(" 2.通讯者结点的插入\n");
  88. printf(" 3.通讯录链表的查询\n");
  89. printf(" 4.通讯者结点的删除\n");
  90. printf(" 5.通讯录链表的输出\n");
  91. printf(" 0.退出通讯录管理系统\n");
  92. printf(" ======================\n");
  93. printf( " 请选择操作0-5 \n\n");
  94. for(; ;)
  95. {
  96. scanf("%d",&sn);
  97. if(sn<0||sn>5)
  98. printf("\t输入错误\n");
  99. else
  100. break;
  101. }
  102. return sn;
  103. }
  104. linklist createlist(void)
  105. {
  106. linklist head=(listnode *)malloc(sizeof(listnode));//建立头节点
  107. listnode *p,*rear;//p为要创建的节点指针,rear为在链表上移动的指针
  108. int flag=1;
  109. rear=head;
  110. while(flag==1)
  111. {
  112. p=(listnode *)malloc(sizeof(listnode));
  113. printf(" 编号(5) 姓名(8) 性别(10) 电话(11) 地址(100) \n");
  114. printf(" --------------------------------------------\n");
  115. scanf("%s%s%s%s%s",p->data.num,p->data.name,p->data.sex ,p->data.tel,p->data.address);//数据输入
  116. rear->next=p;
  117. rear=p;
  118. printf(" 继续建表吗? (1.是/0.不):");
  119. scanf("%d",&flag);

  120. }
  121. if(flag==1)
  122. printf("创建成功!\n");
  123. rear->next=NULL;
  124. return head;
  125. }
  126. void insertnode(linklist head,listnode *p)//安编号有序的插如
  127. {
  128. listnode *p1,*p2;//链表上相邻的两个指针
  129. p1=head;
  130. p2=p1->next;
  131. while(p2!=NULL && strcmp(p2->data.num,p->data.num)<0)//查找要插入的位置为p1,p2间或者链表后面
  132. {
  133. p1=p2;
  134. p2=p2->next;
  135. }
  136. p1->next=p;
  137. p->next=p2;
  138. printf("插入成功!\n");
  139. }
  140. listnode * listfind(linklist head)
  141. {
  142. listnode * p;
  143. char num[5];
  144. char name[8];
  145. int xz;
  146. printf("--------------------\n");
  147. printf(" 1.编号查询\n");
  148. printf(" 2.姓名查询\n");
  149. printf("--------------------\n");
  150. printf("请选择: \n");
  151. p=head->next;
  152. scanf("%d",&xz);
  153. if (xz==1){
  154. printf(" 请输入要查找的编号: ");
  155. scanf("%s",num);
  156. while(p && strcmp(p->data.num,num)<0)
  157. p=p->next;
  158. if (p==NULL || strcmp(p->data.num,num)>0)
  159. p=NULL;
  160. }
  161. else
  162. if(xz=2){
  163. printf("请输入要查询者的姓名: ");
  164. scanf("%s",name);
  165. while(p && strcmp(p->data.name,name)!=0)
  166. p=p->next;
  167. }
  168. return p;
  169. }
  170. void delnode(linklist head)
  171. {
  172. int jx;
  173. listnode *p,*q;
  174. p=listfind(head);
  175. if(p==NULL){
  176. printf("没有要删除的通讯者!:\n");
  177. return;
  178. }
  179. printf ("真的要删除该结点吗?(1.是/0.不):");
  180. scanf("%d",&jx);
  181. if (jx==1){
  182. q=head;
  183. while(q!=NULL && q->next!=p)
  184. q=q->next;
  185. q->next=p->next;
  186. free(p);
  187. printf("通讯者已被删除!\n");
  188. }
  189. }
  190. void printlist(linklist head)
  191. {
  192. listnode * p;
  193. p=head->next;
  194. printf(" 编号 姓名 性别 电话 地址\n");
  195. printf(" -----------------------------\n");
  196. while(p!=NULL)
  197. {
  198. printf(" %s, %s, %s, %s, %s\n",p->data.num,p->data.name,p->data.sex,p->data.tel,p->data.address);
  199. printf(" ------------------------------\n");
  200. p=p->next;
  201. }

  202. }
复制代码

沙发
发表于 2013-11-23 20:25:43 | 只看该作者
每次关闭后数据就丢失了,推荐做成文件读写(FILE)
板凳
 楼主| 发表于 2013-11-23 20:31:32 | 只看该作者
4399APPLE 发表于 2013-11-23 20:25
每次关闭后数据就丢失了,推荐做成文件读写(FILE)

以后回头慢慢看了
地板
 楼主| 发表于 2013-11-23 20:32:02 | 只看该作者
TTTTT 发表于 2013-11-23 20:31
以后回头慢慢看了

还有这个不是我 是我飞哥。
5#
发表于 2013-11-23 20:35:00 | 只看该作者
TTTTT 发表于 2013-11-23 20:32
还有这个不是我 是我飞哥。

啥意思                     
6#
 楼主| 发表于 2013-11-23 21:09:29 | 只看该作者
4399APPLE 发表于 2013-11-23 20:35
啥意思

朋友写的代码 发来分享一下
7#
发表于 2013-11-24 09:37:37 | 只看该作者
本帖最后由 4399APPLE 于 2013-11-30 16:32 编辑
TTTTT 发表于 2013-11-23 21:09
朋友写的代码 发来分享一下
代码太乱了……
8#
 楼主| 发表于 2013-11-30 16:43:17 | 只看该作者
4399APPLE 发表于 2013-11-24 09:37
代码太乱了……

难得有备注 我发现很多人不喜欢备注 这样如何让人调试啊 看见就会头晕
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表