鱼C论坛

 找回密码
 立即注册
查看: 2505|回复: 1

内存池那节的通讯录代码用vs打的有一堆bug,复制到dev上能运行,但是有的函数用不了

[复制链接]
发表于 2021-10-28 20:45:24 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. struct list
  5. {
  6.         char name[20];
  7.         char phone[20];
  8.         struct list* next;
  9. }*head = NULL;

  10. void addPerson(struct list** head);
  11. void getInput(struct list* person);
  12. void findPerson(struct list* head);
  13. void changePerson(struct list* head);
  14. void delPerson(struct list* head);
  15. void displayContacts(struct list* head);
  16. void releasePerson(struct list** head);

  17. void addPerson(struct list** head)
  18. {
  19.         struct list* person, * temp;
  20.         person = (struct list*)malloc(sizeof(struct list));
  21.         if (person == NULL)
  22.         {
  23.                 printf("分配内存失败。\n");
  24.                 exit(1);
  25.         }
  26.         getInput(person);

  27.         if (*head!=NULL)
  28.         {
  29.                 temp = *head;
  30.                 *head = person;
  31.                 person->next = NULL;
  32.         }
  33.         else
  34.         {
  35.                 *head = person;
  36.                 person->next = NULL;
  37.         }
  38. }

  39. void getInput(struct list* person)
  40. {
  41.         printf("请输入联系人姓名:\n");
  42.         scanf("%s", &person->name);
  43.         printf("请输入联系人电话。\n");
  44.         scanf("%s", &person->phone);
  45. }

  46. void findPerson(struct list* head)
  47. {
  48.         char ch[25];
  49.         printf("请输入联系人姓名:\n");
  50.         scanf("%s", ch);

  51.         while (strcmp(head->name,ch)&&head!=NULL)
  52.         {
  53.                 head = head->next;
  54.         }

  55.         if (head==NULL)
  56.         {
  57.                 printf("没有此联系人\n");
  58.         }
  59.         else
  60.         {
  61.                 printf("姓名:%s", head->name);
  62.                 printf("电话号码:%s", head->phone);
  63.         }
  64. }

  65. void changePerson(struct list*head)
  66. {
  67.         char ch[25];
  68.         int num = 1;
  69.         printf("请输入联系人姓名:\n");
  70.         scanf("%s", ch);

  71.         while (strcmp(head->name, ch) && head != NULL)
  72.         {
  73.                 head = head->next;
  74.         }

  75.         if (head == NULL)
  76.         {
  77.                 printf("没有此联系人\n");
  78.         }
  79.         else
  80.         {
  81.                 while (num)
  82.                 {
  83.                         printf("1.改写名字 2.改写姓名 0.退出\n");
  84.                         scanf("%d", &num);
  85.                         switch (num)
  86.                         {
  87.                         case 0:
  88.                                 printf("退出\n");
  89.                                 break;
  90.                         case 1:
  91.                                 printf("请输入姓名。\n");
  92.                                 scanf("%s", &head->name);
  93.                                 break;
  94.                         case 2:
  95.                                 printf("请输入电话。\n");
  96.                                 scanf("%s", head->phone);
  97.                                 break;
  98.                         default:
  99.                                 printf("输入错误");
  100.                                 break;
  101.                         }
  102.                 }
  103.         }
  104. }

  105. void delPerson(struct list* head)
  106. {
  107.         char ch[20];
  108.         printf("请输入姓名。\n");
  109.         scanf("%s", &ch);

  110.         struct list* previous, * current;
  111.         current = head;

  112.         while (strcmp(current->name,ch)&&current!=NULL)
  113.         {
  114.                 previous = current;
  115.                 current = current->next;
  116.         }

  117.         if (current == NULL)
  118.         {
  119.                 printf("没有此联系人\n");
  120.         }
  121.         else
  122.         {
  123.                 previous->next = current->next;
  124.                 free(current);
  125.         }
  126. }

  127. void displayContacts(struct list* head)
  128. {
  129.         while (head!=NULL)
  130.         {
  131.                 printf("姓名:%s  电话:%s\n",head->name,head->phone);
  132.                 head = head->next;
  133.         }
  134. }

  135. void releasePerson(struct list** head)
  136. {
  137.         struct list* temp;
  138.         while (*head!=NULL)
  139.         {
  140.                 temp = *head;
  141.                 *head = (*head)->next;
  142.                 free(temp);
  143.         }
  144. }

  145. int main(void)
  146. {
  147.         int num = 1;
  148.         while (num)
  149.         {
  150.                 printf("请输入(1-6):\n");
  151.                 printf("请输入命令:\n.....||1(添加新的联系人)  ||.....\n.....||2:(查找联系人)    ||.....\n.....||3:(更改联系人)    ||.....\n.....||4:(删除联系人)    ||.....\n.....||5:(查看所有联系人)||.....\n.....||6:(退出)          ||.....");
  152.                 scanf("%d", &num);

  153.                 switch (num)
  154.                 {
  155.                 case 1:
  156.                         addPerson(&head);
  157.                         break;
  158.                 case 2:
  159.                         findPerson(head);
  160.                         break;
  161.                 case 3:
  162.                         changePerson(head);
  163.                         break;
  164.                 case 4:
  165.                         delPerson(head);
  166.                         break;
  167.                 case 5:
  168.                         displayContacts(head);
  169.                         break;
  170.                 case 6:
  171.                         printf("提出系统.\n");
  172.                         releasePerson(&head);
  173.                         break;
  174.                 default:
  175.                         break;
  176.                 }
  177.         }


  178.         return 0;
  179. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-28 20:49:22 | 显示全部楼层
C:\Users\jiang\Desktop
屏幕截图 2021-10-28 204656.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-12 17:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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