鱼C论坛

 找回密码
 立即注册
查看: 2195|回复: 5

[已解决]我又来了。。————求助!

[复制链接]
发表于 2023-4-8 22:39:46 | 显示全部楼层 |阅读模式

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

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

x
今天学完单链表用单链表的方式做之前的题,但是还是遇到一个类似之前的问题!
录入完第一个人资料 录第二个人资料就会有卡住 有问题。。求助


  1. #include<stdio.h>
  2. #include <stdlib.h>

  3. struct Ym
  4. {
  5.         char name[8];
  6.         int age;
  7.         char one_time[11];
  8.         char two_time[11];
  9.         struct Ym *next;

  10. };

  11. getInput(struct Ym *ym)
  12. {
  13.        
  14.         char ch;
  15.         printf("请输入姓名:");
  16.         scanf("%s",ym->name);
  17.         printf("请输入年龄:");
  18.         scanf("%d",ym->age);
  19.         while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  20.     printf("请问是否接种过疫苗(Y/N):");


  21.         
  22.         if((ch = getchar()) == 'Y')
  23.         {
  24.                 printf("请输入第一针疫苗接种的日期(yyyy-mm-dd):");
  25.                 scanf("%s",ym->one_time);
  26.                
  27.         }
  28.         else
  29.         {
  30.                 ym->one_time[0] = '0';
  31.                 return ;
  32.         }
  33.         
  34.         while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  35.         printf("请问是否接种过第二针疫苗(Y/N):");
  36.                
  37.                  if((ch = getchar()) == 'Y')
  38.         {
  39.         
  40.                 printf("请输入第二针疫苗接种的日期(yyyy-mm-dd):");
  41.                 scanf("%s",ym->two_time);
  42.         }
  43.         else
  44.         ym->two_time[0] = '0';
  45.        
  46. }


  47. void addYm(struct Ym **library)
  48. {
  49.         struct Ym *ym, *temp;
  50.         ym = (struct Ym*)malloc(sizeof(struct Ym));
  51.         if(ym == NULL)
  52.         {
  53.                 printf("内存分配失败了!\n");
  54.                 exit(1);
  55.         }
  56.        
  57.         getInput(ym);
  58.        
  59.         if(*library != NULL)
  60.         {
  61.                 temp = *library;
  62.                 *library = ym;
  63.                 ym->next = temp;
  64.                
  65.         }
  66.         else
  67.         {
  68.                 *library = ym;
  69.                 ym->next = NULL;
  70.         }
  71.        
  72.        
  73. }

  74.        
  75. void printLibrary(struct Ym *library)
  76. {
  77.         struct Ym *ym;
  78. //        int count = 1;
  79.        
  80.         ym = library;
  81.         while(ym != NULL)
  82.         {
  83.                 printf("姓名:%s        ",ym->name);
  84.                 printf("年龄:%d",ym->age);
  85.                  if(ym->one_time[0] != '0')
  86.         printf("第一针疫苗接种日期:%s,",ym->one_time);
  87.         else
  88.         {
  89.             printf("未接种疫苗!");
  90.         }
  91.         if(ym->two_time[0] != '0')
  92.         printf("第二针疫苗接种日期:%s\n\n",ym->two_time);
  93.         else
  94.         printf("未接种第二针疫苗!\n\n");
  95.         
  96.                 ym = ym->next;
  97.                 //count++;
  98.         }
  99. }

  100. void releaseLibrary(struct Ym *library)
  101. {
  102.         struct Ym *temp;
  103.    
  104.         while (library != NULL)
  105.         {
  106.                 temp = library;
  107.                 library = library->next;
  108.                 free(temp);
  109.         }
  110. }




  111. int main(void)
  112. {
  113.         struct Ym *library = NULL;
  114.         char ch;
  115.        
  116.         while(1)
  117.         {
  118.                 printf("请问是否需要录入疫苗信息(Y/N):");
  119.                 do
  120.                 {
  121.                         ch = getchar();
  122.                 }
  123.                 while(ch != 'Y' && ch != 'N');

  124.                 if(ch == 'Y')
  125.                 {
  126.                         addYm(&library); //如果用library是指向空间的地址 用&library是library指针本身的地址
  127.                        
  128.        
  129.                 }
  130.                 else
  131.                 {
  132.                         break;
  133.                 }
  134.         }

  135.         printf("请问是否需要打印疫苗信息(Y/N):");
  136.         do
  137.         {
  138.                 ch = getchar();
  139.         }
  140.         while(ch != 'Y' && ch != 'N');
  141.        
  142.         if(ch == 'Y')
  143.         {
  144.                 printLibrary(library);
  145.         }
  146.         releaseLibrary(library);
  147.        
  148.         return 0;
  149. }
复制代码
最佳答案
2023-4-8 23:26:01
本帖最后由 isdkz 于 2023-4-8 23:31 编辑
a905448839 发表于 2023-4-8 23:20
是我这个修改后的代码能够录入多个人员的数据吗? 我尝试了很多次都不行。。。


暂且称你修改前的代码为第一个代码,修改后的为第二个代码,我给你解释以下只能输出一个数据的原因:

在第二个代码的 printLibrary 函数中,当第一个疫苗未接种时,程序会直接跳到下一个记录,这是因为使用了 break 语句。

实际上,应该使用 continue 语句,以便跳过当前记录的剩余部分并继续处理下一个记录。以下是修复后的第二个代码中的 printLibrary 函数的相关部分:

  1. else
  2. {
  3.     printf("未接种疫苗!");
  4.     ym = ym->next;
  5.     continue;
  6. }
复制代码


将 break 语句替换为 continue 语句后,第二个代码应该可以正确地输出所有已录入的信息。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-4-8 22:41:56 | 显示全部楼层
你好!我发现你在 getInput 函数中调用 scanf() 函数时,ym->age 需要传递一个整数指针,而不是整数本身。请将 scanf("%d",ym->age); 更改为 scanf("%d",&ym->age);。

下面是修改后的 getInput() 函数:


  1. void getInput(struct Ym *ym)
  2. {
  3.         char ch;
  4.         printf("请输入姓名:");
  5.         scanf("%s",ym->name);
  6.         printf("请输入年龄:");
  7.         scanf("%d",&ym->age); // 这里修改了
  8.         while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  9.         // ... 其他代码保持不变 ...
  10. }
复制代码


使用上述修改后的 getInput() 函数,程序应该可以正常运行并录入多个人的资料。如果还有其他问题,请随时告诉我!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-8 23:05:49 | 显示全部楼层
isdkz 发表于 2023-4-8 22:41
你好!我发现你在 getInput 函数中调用 scanf() 函数时,ym->age 需要传递一个整数指针,而不是整数本身。 ...

修改后 为什么只录入了最后一个人的数据呢?

  1. #include<stdio.h>
  2. #include <stdlib.h>

  3. struct Ym
  4. {
  5.         char name[8];
  6.         int age;
  7.         char one_time[11];
  8.         char two_time[11];
  9.         struct Ym *next;

  10. };

  11. void getInput(struct Ym *ym)
  12. {
  13.        
  14.         char ch;
  15.         printf("请输入姓名:");
  16.         scanf("%s",ym->name);
  17.         printf("请输入年龄:");
  18.         scanf("%d",&ym->age);
  19.         while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  20.     printf("请问是否接种过疫苗(Y/N):");


  21.         
  22.         if((ch = getchar()) == 'Y')
  23.         {
  24.                 printf("请输入第一针疫苗接种的日期(yyyy-mm-dd):");
  25.                 scanf("%s",ym->one_time);
  26.                
  27.         }
  28.         else
  29.         {
  30.                 ym->one_time[0] = '0';
  31.                 printf("\n\n");
  32.                 return ;
  33.         }
  34.         
  35.         while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  36.         printf("请问是否接种过第二针疫苗(Y/N):");
  37.                
  38.                  if((ch = getchar()) == 'Y')
  39.         {
  40.         
  41.                 printf("请输入第二针疫苗接种的日期(yyyy-mm-dd):");
  42.                 scanf("%s",ym->two_time);
  43.         }
  44.         else
  45.         ym->two_time[0] = '0';
  46.         
  47.         //while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区
  48.                 printf("\n\n");
  49.        
  50. }


  51. void addYm(struct Ym **library)
  52. {
  53.         struct Ym *ym, *temp;
  54.         ym = (struct Ym*)malloc(sizeof(struct Ym));
  55.         if(ym == NULL)
  56.         {
  57.                 printf("内存分配失败了!\n");
  58.                 exit(1);
  59.         }
  60.        
  61.         getInput(ym);
  62.        
  63.         if(*library != NULL)
  64.         {
  65.                 temp = *library;
  66.                 *library = ym;
  67.                 ym->next = temp;
  68.                
  69.         }
  70.         else
  71.         {
  72.                 *library = ym;
  73.                 ym->next = NULL;
  74.         }
  75.        
  76.        
  77. }

  78.        
  79. void printLibrary(struct Ym *library)
  80. {
  81.         struct Ym *ym;
  82. //        int count = 1;
  83.        
  84.         ym = library;
  85.         while(ym != NULL)
  86.         {
  87.                 printf("姓名:%s        ",ym->name);
  88.                 printf("年龄:%d\n",ym->age);
  89.                
  90.                  if(ym->one_time[0] != '0')
  91.         printf("第一针疫苗接种日期:%s,        ",ym->one_time);
  92.         else
  93.         {
  94.             printf("未接种疫苗!");
  95.             ym = ym->next;
  96.             break;
  97.            
  98.         }
  99.       
  100.             if(ym->one_time[0] != '0' && ym->two_time[0] != '0')
  101.         printf("第二针疫苗接种日期:%s\n\n",ym->two_time);
  102.         else
  103.         {
  104.                         printf("未接种第二针疫苗!\n\n");
  105.          
  106.                 }
  107.                 ym = ym->next; //指向下一个结构体  就可以跳出循环了
  108.                         //count++;
  109.         }
  110. }

  111. void releaseLibrary(struct Ym *library)
  112. {
  113.         struct Ym *temp;
  114.    
  115.         while (library != NULL)
  116.         {
  117.                 temp = library;
  118.                 library = library->next;
  119.                 free(temp);
  120.         }
  121. }



  122. int main(void)
  123. {
  124.         struct Ym *library = NULL;
  125.         char ch;
  126.        
  127.         while(1)
  128.         {
  129.                 printf("请问是否需要录入疫苗信息(Y/N):");
  130.                 do
  131.                 {
  132.                         ch = getchar();
  133.                 }
  134.                 while(ch != 'Y' && ch != 'N');

  135.                 if(ch == 'Y')
  136.                 {
  137.                         addYm(&library); //如果用library是指向空间的地址 用&library是library指针本身的地址
  138.                        
  139.        
  140.                 }
  141.                 else
  142.                 {
  143.                         printf("\n");
  144.        
  145.                         break;
  146.                 }
  147.         }

  148.         printf("请问是否需要打印疫苗信息(Y/N):");
  149.         do
  150.         {
  151.                 ch = getchar();
  152.         }
  153.         while(ch != 'Y' && ch != 'N');
  154.        
  155.         if(ch == 'Y')
  156.         {
  157.                 printLibrary(library);
  158.         }
  159.         releaseLibrary(library);
  160.        
  161.         return 0;
  162. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-8 23:09:28 | 显示全部楼层
a905448839 发表于 2023-4-8 23:05
修改后 为什么只录入了最后一个人的数据呢?

录入的信息都有呀

截图20230408230854.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-8 23:25:23 | 显示全部楼层
isdkz 发表于 2023-4-8 23:09
录入的信息都有呀

是我这个修改后的代码能够录入多个人员的数据吗? 我尝试了很多次都不行。。。

  1. #include<stdio.h>
  2. #include <stdlib.h>

  3. struct Ym
  4. {
  5.         char name[8];
  6.         int age;
  7.         char one_time[11];
  8.         char two_time[11];
  9.         struct Ym *next;

  10. };

  11. void getInput(struct Ym *ym)
  12. {
  13.         
  14.         char ch;
  15.         printf("请输入姓名:");
  16.         scanf("%s",ym->name);
  17.         printf("请输入年龄:");
  18.         scanf("%d",&ym->age);
  19.         while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  20.     printf("请问是否接种过疫苗(Y/N):");


  21.         
  22.         if((ch = getchar()) == 'Y')
  23.         {
  24.                 printf("请输入第一针疫苗接种的日期(yyyy-mm-dd):");
  25.                 scanf("%s",ym->one_time);
  26.                
  27.         }
  28.         else
  29.         {
  30.                 ym->one_time[0] = '0';
  31.                 printf("\n\n");
  32.                 return ;
  33.         }
  34.         
  35.         while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  36.         printf("请问是否接种过第二针疫苗(Y/N):");
  37.                
  38.                  if((ch = getchar()) == 'Y')
  39.         {
  40.         
  41.                 printf("请输入第二针疫苗接种的日期(yyyy-mm-dd):");
  42.                 scanf("%s",ym->two_time);
  43.         }
  44.         else
  45.         ym->two_time[0] = '0';
  46.         
  47.         //while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区
  48.                 printf("\n\n");
  49.         
  50. }


  51. void addYm(struct Ym **library)
  52. {
  53.         struct Ym *ym, *temp;
  54.         ym = (struct Ym*)malloc(sizeof(struct Ym));
  55.         if(ym == NULL)
  56.         {
  57.                 printf("内存分配失败了!\n");
  58.                 exit(1);
  59.         }
  60.         
  61.         getInput(ym);
  62.         
  63.         if(*library != NULL) //可以分开看 * (*library)看成一个整体 这个代表**library指针指向空间的地址
  64.         {
  65.                 temp = *library;
  66.                 *library = ym;
  67.                 ym->next = temp;
  68.                
  69.         }
  70.         else
  71.         {
  72.                 *library = ym;
  73.                 ym->next = NULL;
  74.         }
  75.         
  76.         
  77. }

  78.         
  79. void printLibrary(struct Ym *library)
  80. {
  81.         struct Ym *ym;
  82. //        int count = 1;
  83.         
  84.         ym = library;
  85.         while(ym != NULL)
  86.         {
  87.                 printf("姓名:%s        ",ym->name);
  88.                 printf("年龄:%d\n",ym->age);
  89.                
  90.                  if(ym->one_time[0] != '0')
  91.         printf("第一针疫苗接种日期:%s,        ",ym->one_time);
  92.         else
  93.         {
  94.             printf("未接种疫苗!");
  95.             ym = ym->next;
  96.             break;
  97.            
  98.         }
  99.       
  100.             if(ym->one_time[0] != '0' && ym->two_time[0] != '0')
  101.         printf("第二针疫苗接种日期:%s\n\n",ym->two_time);
  102.         else
  103.         {
  104.                         printf("未接种第二针疫苗!\n\n");
  105.          
  106.                 }
  107.                 ym = ym->next; //指向下一个结构体  就可以跳出循环了
  108.                         //count++;
  109.         }
  110. }

  111. void releaseLibrary(struct Ym *library)
  112. {
  113.         struct Ym *temp;
  114.    
  115.         while (library != NULL)
  116.         {
  117.                 temp = library;
  118.                 library = library->next;
  119.                 free(temp);
  120.         }
  121. }



  122. int main(void)
  123. {
  124.         struct Ym *library = NULL;
  125.         char ch;
  126.         
  127.         while(1)
  128.         {
  129.                 printf("请问是否需要录入疫苗信息(Y/N):");
  130.                 do
  131.                 {
  132.                         ch = getchar();
  133.                 }
  134.                 while(ch != 'Y' && ch != 'N');

  135.                 if(ch == 'Y')
  136.                 {
  137.                         addYm(&library); //如果用library是指向空间的地址 用&library是library指针本身的地址
  138.                         
  139.         
  140.                 }
  141.                 else
  142.                 {
  143.                         printf("\n");
  144.         
  145.                         break;
  146.                 }
  147.         }

  148.         printf("请问是否需要打印疫苗信息(Y/N):");
  149.         do
  150.         {
  151.                 ch = getchar();
  152.         }
  153.         while(ch != 'Y' && ch != 'N');
  154.         
  155.         if(ch == 'Y')
  156.         {
  157.                 printLibrary(library);
  158.         }
  159.         releaseLibrary(library);
  160.         
  161.         return 0;
  162. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-8 23:26:01 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2023-4-8 23:31 编辑
a905448839 发表于 2023-4-8 23:20
是我这个修改后的代码能够录入多个人员的数据吗? 我尝试了很多次都不行。。。


暂且称你修改前的代码为第一个代码,修改后的为第二个代码,我给你解释以下只能输出一个数据的原因:

在第二个代码的 printLibrary 函数中,当第一个疫苗未接种时,程序会直接跳到下一个记录,这是因为使用了 break 语句。

实际上,应该使用 continue 语句,以便跳过当前记录的剩余部分并继续处理下一个记录。以下是修复后的第二个代码中的 printLibrary 函数的相关部分:

  1. else
  2. {
  3.     printf("未接种疫苗!");
  4.     ym = ym->next;
  5.     continue;
  6. }
复制代码


将 break 语句替换为 continue 语句后,第二个代码应该可以正确地输出所有已录入的信息。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 09:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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