鱼C论坛

 找回密码
 立即注册
查看: 878|回复: 0

[技术交流] 写的通讯录

[复制链接]
发表于 2020-5-9 13:51:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 1024
  5. struct Contact
  6. {
  7.         char name[40];
  8.         char phone[40];
  9.         struct Contact *next;
  10. };
  11. int count = 0;
  12. struct Contact *pool = NULL;
  13. void addPerson(struct Contact **pointer);
  14. void findPerson(struct Contact *pointer);
  15. void changePerson(struct Contact *pointer);
  16. void delPerson(struct Contact **pointer);
  17. void displayContact(struct Contact *pointer);
  18. void release(struct Contact *forward);
  19. void shutdown(struct Contact *pointer);


  20. void addPerson(struct Contact **pointer)
  21. {
  22.         struct Contact *p;
  23.         if(pool == NULL)
  24.         {
  25.                 p = (struct Contact *)malloc(sizeof(struct Contact));
  26.                 if (p==NULL)
  27.                 {
  28.                         printf("分配新空间失败,请重试。\n");
  29.                         exit(1);
  30.                 }

  31.         }
  32.         else
  33.         {
  34.                 p = pool;
  35.                 pool = pool -> next;
  36.                 count--;
  37.         }
  38.         printf("添加联系人:请输入姓名\n");
  39.         scanf("%s",p->name);
  40.         printf("添加联系人:请输入电话号码\n");
  41.         scanf("%s",p->phone);
  42.         if (*pointer == NULL)
  43.         {
  44.                 *pointer = p;
  45.                 p->next = NULL;
  46.         }
  47.         else
  48.         {
  49.                 struct Contact *temp = *pointer;
  50.                 *pointer = p;
  51.                 p->next = temp;
  52.         }
  53. }

  54. void findPerson(struct Contact *pointer)
  55. {
  56.         char input[40];
  57.         int i=0;
  58.         printf("请输入要查询的姓名\n");
  59.         scanf("%s",input);
  60.         while(pointer != NULL)
  61.         {

  62.                 if (!strcmp(input, pointer->name))
  63.                 {
  64.                         printf("%s的电话号码是%s\n",input, pointer->phone );
  65.                         i++;
  66.                 }
  67.                 pointer = pointer -> next;

  68.         }
  69.         if(i==0)
  70.         {
  71.                 printf("未找到联系人%s\n",input );
  72.         }

  73. }
  74. void changePerson(struct Contact *pointer)
  75. {
  76.         char input[40];
  77.         int i=0;
  78.         printf("请输入要更改联系方式的联系人姓名\n");
  79.         scanf("%s",input);
  80.         while(pointer != NULL)
  81.         {

  82.                 if (!strcmp(input, pointer->name))
  83.                 {
  84.                         printf("更改%s的电话号码为\n",input );
  85.                         scanf("%s",pointer->phone);
  86.                         i++;
  87.                 }
  88.                 pointer = pointer -> next;

  89.         }
  90.         if(i==0)
  91.         {
  92.                 printf("未找到联系人%s\n",input );
  93.         }

  94. }
  95. void delPerson(struct Contact **pointer)
  96. {
  97.         char input[40];
  98.         int i=0;
  99.         printf("请输入要删除的联系人姓名\n");
  100.         scanf("%s",input);
  101.         struct Contact *back = NULL;
  102.         struct Contact *forward = NULL;
  103.         if (!strcmp(input, (*pointer)->name))
  104.         {
  105.                 forward = *pointer;
  106.                 *pointer = (*pointer)->next;
  107.                 release(forward);
  108.                 i++;
  109.         }
  110.         else
  111.         {
  112.                 back = *pointer;
  113.                 forward = (*pointer)->next;

  114.                 while(forward != NULL)
  115.                 {

  116.                         if (!strcmp(input, (forward)->name))
  117.                         {
  118.                                 back->next = forward->next;       
  119.                                 release(forward);       
  120.                                 i++;
  121.                         }
  122.                         back = forward;
  123.                         forward = forward->next;

  124.                 }
  125.         }
  126.         if(i==0)
  127.         {
  128.                 printf("未找到联系人%s\n",input );
  129.         }

  130. }
  131. void release(struct Contact *forward)
  132. {
  133.         if (pool == NULL)
  134.         {
  135.                 pool = forward;
  136.                 forward->next = NULL;
  137.         }
  138.         else if(count < MAX)
  139.         {
  140.                 struct Contact *tempool = pool;
  141.                 pool = forward;
  142.                 forward->next = tempool->next;
  143.                 count++;
  144.         }
  145.         else
  146.         {
  147.                 free(forward);
  148.         }
  149. }
  150. void displayContact(struct Contact *pointer)
  151. {
  152.         struct Contact *p = pointer;
  153.         while(p != NULL)
  154.         {
  155.                 printf("%s : %s \n", p->name, p->phone );
  156.                 p = p->next;
  157.         }
  158. }
  159. void shutdown(struct Contact *pointer)
  160. {
  161.        
  162.         if(pointer != NULL)
  163.         {
  164.                 struct Contact *temp = pointer;
  165.                 free(pointer);
  166.                 pointer = temp->next;
  167.         }
  168.         if(pool != NULL)
  169.         {
  170.                 struct Contact *temp = pool;
  171.                 free(pool);
  172.                 pool = temp->next;
  173.         }


  174. }




  175. int main(void)
  176. {
  177.         struct Contact *pointer = NULL;
  178.         printf("===请输入命令代码==========\n");
  179.         printf("===1: 增加一个联系人======\n");
  180.         printf("===2: 删除一个联系人======\n");
  181.         printf("===3: 查看指定联系人======\n");
  182.         printf("===4: 显示所有联系人======\n");
  183.         printf("===5: 更改联系人联系方式===\n");
  184.         printf("===6: 退出===============\n");
  185.         while(1)
  186.         {
  187.                 int i = 1;
  188.                 scanf("%d",&i);
  189.                 switch(i)
  190.                 {
  191.                         case 1:
  192.                         addPerson(&pointer);
  193.                         break;
  194.                         case 2:
  195.                         delPerson(&pointer);
  196.                         break;
  197.                         case 3:
  198.                         findPerson(pointer);
  199.                         break;
  200.                         case 4:
  201.                         displayContact(pointer);
  202.                         break;
  203.                         case 5:
  204.                         changePerson(pointer);
  205.                         break;
  206.                         case 6:
  207.                         goto SHUTDOWM;
  208.                         break;
  209.                         default:
  210.                         printf("请输入正确的指令代码");
  211.                 }
  212.        
  213.         }
  214.         SHUTDOWM: shutdown(pointer);


  215.         return 0;
  216. }
复制代码

测试:
===请输入命令代码==========
===1: 增加一个联系人======
===2: 删除一个联系人======
===3: 查看指定联系人======
===4: 显示所有联系人======
===5: 更改联系人联系方式===
===6: 退出===============
1
添加联系人:请输入姓名
爱新觉罗努尔哈赤
添加联系人:请输入电话号码
13567852435
1
添加联系人:请输入姓名
嬴政
添加联系人:请输入电话号码
1898723645
1
添加联系人:请输入姓名
朱元璋
添加联系人:请输入电话号码
18876245603
1
添加联系人:请输入姓名
赵匡胤
添加联系人:请输入电话号码
17856730987
4
赵匡胤 : 17856730987
朱元璋 : 18876245603
嬴政 : 1898723645
爱新觉罗努尔哈赤 : 13567852435
5
请输入要更改联系方式的联系人姓名
嬴政
更改嬴政的电话号码为
01078698763
5
请输入要更改联系方式的联系人姓名
赵匡胤
更改赵匡胤的电话号码为
02178657943
4
赵匡胤 : 02178657943
朱元璋 : 18876245603
嬴政 : 01078698763
爱新觉罗努尔哈赤 : 13567852435
2
请输入要删除的联系人姓名
嬴政
4
赵匡胤 : 02178657943
朱元璋 : 18876245603
爱新觉罗努尔哈赤 : 13567852435
5
请输入要更改联系方式的联系人姓名
李世民
未找到联系人李世民
6
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[进程已完成]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-30 21:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表