鱼C论坛

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

[已解决]switch 语句,case不起作用,我还没输入指令,就开始执行下一个case的指令

[复制链接]
发表于 2021-1-9 13:23:43 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 猪猪虾 于 2021-1-9 13:30 编辑

主程序太长了,可以不用看,看最下面的main函数里面的switch就行,图片在最下方

  1. #include<winuser.inl>
  2. #include<stdio.h>
  3. #include <cstdlib>
  4. #include<string.h>
  5. void tail_add(struct People** head);

  6. struct People
  7. {
  8.         char name[20];
  9.         char phone[20];
  10.         struct People *next;
  11. };

  12. void getInput(struct People* people,int wether_change_phone = 0)
  13. {
  14.         printf("enter name:");
  15.         scanf_s("%s", people->name, 20);
  16.         if (wether_change_phone == 0)
  17.         {
  18.                 printf("enter phone:");
  19.                 scanf_s("%s", people->phone, 20);
  20.         }
  21.         else
  22.         {
  23.                 printf("enter new phone:");
  24.                 scanf_s("%s", people->phone, 20);
  25.         }
  26. }

  27. void printfInfo(struct People *head)
  28. {
  29.         struct People* people;
  30.         people = head;
  31.         printf("name: %s \n", people->name);
  32.         printf("phone:%s \n", people->phone);
  33. }

  34. void tail_add(struct People** head)
  35. {
  36.         struct People* people,*temp;
  37.         people = (struct People*)(malloc(sizeof(struct People)));  //动态申请结构体

  38.         if (people == NULL)
  39.         {
  40.                 printf("memory failed");
  41.                 exit(1);
  42.         }

  43.         getInput(people,0);

  44.         //找尾节点
  45.         if (*head == NULL)
  46.         {
  47.                 *head = people;
  48.                 people -> next = NULL;
  49.         }
  50.         else
  51.         {
  52.                 temp = *head;
  53.                 while (temp->next != NULL)
  54.                 {
  55.                         temp = temp->next;
  56.                 }
  57.                 temp->next = people;
  58.                 people -> next = NULL;
  59.         }

  60. }

  61. void find(struct People** head,char find_name[])
  62. {
  63.         struct People * people;
  64.         people = *head;
  65.         //判断书否为空链表,为空,直接结束
  66.         if (people == NULL)
  67.         {
  68.                 printf("no contact people!!!!\n");
  69.         }
  70.         else
  71.         {
  72.                 while (people != NULL)
  73.                 {
  74.                         if (!strcmp( people->name, find_name))
  75.                         {
  76.                                 printfInfo(people);
  77.                                 break;
  78.                         }       
  79.                         else
  80.                         {
  81.                                 people = people->next;
  82.                         }
  83.                 }
  84.         }
  85.         //while循环查找是否有符合条件的人或者到达链表的末端,跳出循环,
  86.         //找到相应的人,打印信息
  87. }

  88. void chang_info(struct People** head, char find_name[])
  89. {
  90.         struct People* people;
  91.         people = *head;

  92.         //遍历链表,找到姓名相同的联系人,修改其信息
  93.         if (people == NULL)
  94.         {
  95.                 printf("no contact people!!!!\n");
  96.         }
  97.         else
  98.         {
  99.                 while (people != NULL)
  100.                 {
  101.                         if (!strcmp(people->name, find_name))
  102.                         {
  103.                                 getInput(people, 1);
  104.                                 break;
  105.                         }
  106.                         else
  107.                         {
  108.                                 people = people->next;
  109.                         }
  110.                 }
  111.         }
  112. }

  113. void delete_info(struct People** head, char delete_name[])
  114. {
  115.         struct People* previous, * current;
  116.         //直接给previous赋值null,current赋值头节点,后面就不需要再用中间变量来存储之后,再进行交换地址了
  117.         //current此时就是一个空的结构体,头指针给他之后,他指向了链表的头节点
  118.         current = *head;
  119.         previous = NULL;

  120.         //找到删除值的位置的前后节点的位置
  121.         while (current != NULL && current->name != delete_name)
  122.         {
  123.                 previous = current;
  124.                 current = current->next;
  125.         }

  126.         //改变链表节点的指向,删除节点
  127.         if (current == NULL)  //空链表,直接将头指针指向他
  128.         {
  129.                 printf("not found anything\n");
  130.         }
  131.         else
  132.         {
  133.                 if (previous == NULL)  //链表只有一个节点
  134.                 {
  135.                         *head = current->next;
  136.                 }
  137.                 else
  138.                 {
  139.                         previous->next = current->next;//前面用的是current 来判断有没有找到要删除的节点的位置,所以删除就是currennt的节点指向下一个
  140.                 }
  141.                 free(current);
  142.         }

  143. }

  144. void show_all(struct People** head)
  145. {
  146.         struct People* people;
  147.         people = *head;
  148.         printf("all people in the phonebook is:\n");
  149.         while (people != NULL)
  150.         {
  151.                 printfInfo(people);
  152.                 people = people->next;
  153.         }
  154.         printf("\n");
  155.        
  156. }

  157. int main()
  158. {
  159.         printf("---欢迎使用通讯录管理程序--\n");
  160.         printf("---1:插入新的联系人-----\n");
  161.         printf("---2:查找已有的联系人---\n");
  162.         printf("---3更改已有的联系人----\n");
  163.         printf("---4:删除已有的联系人---\n");
  164.         printf("---5:显示当前通讯录----\n");
  165.         printf("---6:显退出当前通讯录---\n");
  166.         printf("\n");

  167.         int order;
  168.         char name[20];
  169.         struct People *phonebook = NULL;
  170.         do
  171.         {
  172.                 printf("请输入指令:\n");
  173.                 scanf_s("%d", &order);
  174.                 switch (order)
  175.                 {
  176.                         case 1:
  177.                                 tail_add(&phonebook);
  178.                         case 2:
  179.                                 printf("enter the name you want to find:\n");
  180.                                 scanf_s("%s",name,20);
  181.                                 find(&phonebook,name);
  182.                         case 3:
  183.                                 printf("enter the name you want to change:\n");
  184.                                 scanf_s("%s", name, 20);
  185.                                 chang_info(&phonebook, name);
  186.                         case 4:
  187.                                 printf("enter the name you want to delete:\n");
  188.                                 scanf_s("%s", name, 20);
  189.                                 delete_info(&phonebook, name);
  190.                         case 5:
  191.                                 show_all(&phonebook);
  192.                         case 6:
  193.                                 printf("退出界面!!1");
  194.                                 exit(1);
  195.                         default:
  196.                                 printf("wrong enter !!!!!!");
  197.                 }
  198.         } while (order != 6);

  199. }
复制代码
最佳答案
2021-1-9 13:35:24
本帖最后由 jackz007 于 2021-1-9 13:38 编辑

        只要程序不退出,每一个 case 语句块的最后一条语句必须是 break,否则的话,就会从进入点开始,顺序执行完遇到的每一条语句,而不会在意这些语句是否属于本 case。
  1.                 switch (order)
  2.                 {
  3.                         case 1:
  4.                                 tail_add(&phonebook);
  5.                                 break               ;
  6.                         case 2:
  7.                                 printf("enter the name you want to find:\n");
  8.                                 scanf_s("%s",name,20);
  9.                                 find(&phonebook,name);
  10.                                 break                ;
  11.                         case 3:
  12.                                 printf("enter the name you want to change:\n");
  13.                                 scanf_s("%s", name, 20);
  14.                                 chang_info(&phonebook, name);
  15.                                 break                       ;
  16.                         case 4:
  17.                                 printf("enter the name you want to delete:\n");
  18.                                 scanf_s("%s", name, 20);
  19.                                 delete_info(&phonebook, name);
  20.                                 break                        ;
  21.                         case 5:
  22.                                 show_all(&phonebook);
  23.                                 break               ;
  24.                         case 6:
  25.                                 printf("退出界面!!1");
  26.                                 exit(1);
  27.                         default:
  28.                                 printf("wrong enter !!!!!!");
  29.                                 break                       ;
  30.                 }
复制代码
1.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-9 13:35:24 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-1-9 13:38 编辑

        只要程序不退出,每一个 case 语句块的最后一条语句必须是 break,否则的话,就会从进入点开始,顺序执行完遇到的每一条语句,而不会在意这些语句是否属于本 case。
  1.                 switch (order)
  2.                 {
  3.                         case 1:
  4.                                 tail_add(&phonebook);
  5.                                 break               ;
  6.                         case 2:
  7.                                 printf("enter the name you want to find:\n");
  8.                                 scanf_s("%s",name,20);
  9.                                 find(&phonebook,name);
  10.                                 break                ;
  11.                         case 3:
  12.                                 printf("enter the name you want to change:\n");
  13.                                 scanf_s("%s", name, 20);
  14.                                 chang_info(&phonebook, name);
  15.                                 break                       ;
  16.                         case 4:
  17.                                 printf("enter the name you want to delete:\n");
  18.                                 scanf_s("%s", name, 20);
  19.                                 delete_info(&phonebook, name);
  20.                                 break                        ;
  21.                         case 5:
  22.                                 show_all(&phonebook);
  23.                                 break               ;
  24.                         case 6:
  25.                                 printf("退出界面!!1");
  26.                                 exit(1);
  27.                         default:
  28.                                 printf("wrong enter !!!!!!");
  29.                                 break                       ;
  30.                 }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-3 04:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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