鱼C论坛

 找回密码
 立即注册
查看: 2698|回复: 2

[已解决]小甲鱼的结构体课后题投票系统,票数计数问题

[复制链接]
发表于 2018-2-22 02:38:27 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define NUM 3    //3个候选者
  4. #define NO 5    //5个投票者

  5. struct candidate
  6. {
  7.         char name[10];
  8.         int score;
  9. }candidate[NUM]={{"小可爱", 0}, {"小甲鱼", 0}, {"路人甲", 0}};

  10. void main()
  11. {
  12.         int i, j, max;
  13.         char cand[10];
  14.         int k=NO;
  15.         printf("欢迎进入良好公民评选投票系统:\n\n");
  16.         printf("候选人有:");
  17.         for(i = 0;i < NUM; i++)
  18.         {
  19.                 printf("%s ",candidate[i].name);
  20.         }
  21.         printf("\n\n");
  22.        

  23.         for(i=0; i < k; i++)
  24.         {
  25.                 printf("第 %d 位投票,请写下支持的候选人的名字:", i+1);
  26.                 scanf("%s", &cand);

  27.                 if(strcmp( cand, candidate[i].name) ==0 )
  28.                 {
  29.                         candidate[i].score++;                       //这里candidate[i].score++;为什么不能增加票数呀
  30.                 }
  31.         }
  32.        
  33.         printf("\n\n");

  34.         for(i = 0;i < NUM; i++)
  35.         {
  36.                 printf("%s 同学得票数位:%d\n", candidate[i].name, candidate[i].score);
  37.         }
  38.         printf("\n");
  39.         printf("本次投票活动的胜利者是:");
  40.         max = candidate[0].score;
  41.         for(i = 0;i < NUM; i++)  //求票数的最大值
  42.         {
  43.                 if(max < candidate[i].score)
  44.                 {
  45.                         max = candidate[i].score;
  46.                 }
  47.         }
  48.         for(i = 0;i < NUM; i++)  //打印票数最大值的人的名字
  49.         {
  50.                 if(max == candidate[i].score)
  51.                 {
  52.                         printf("%s\n", candidate[i].name);
  53.                 }
  54.         }
  55. }


复制代码




360截图20180222023514093.jpg




请教各位,为什么票数一直不对,代码的中间那里candidate[i].score++;为什么不能计票数呀。



最佳答案
2018-2-22 05:12:24
  1. for(i = 0; i < k; i++)
  2.         {
  3.                 printf("第 %d 位投票,请写下支持的候选人的名字:", i + 1);
  4.                 scanf("%s", &cand);

  5.                 for(j = 0; j < NUM; ++j)
  6.                 {
  7.                         if(strcmp(cand, candidate[j].name) == 0)
  8.                         {
  9.                                 candidate[j].score++;                       //这里candidate[i].score++;为什么不能增加票数呀
  10.                         }
  11.                 }
  12.         }
复制代码





  1.         max = candidate[0].score;
  2.         for(i = 0;i < NUM; i++)  //求票数的最大值
  3.         {
  4.                 if(max < candidate[i].score)
  5.                 {
  6.                         max = candidate[i].score;
  7.                 }
  8.         }
  9.         for(i = 0;i < NUM; i++)  //打印票数最大值的人的名字
  10.         {
  11.                 if(max == candidate[i].score)
  12.                 {
  13.                         printf("%s\n", candidate[i].name);
  14.                 }
  15.         }
复制代码

这里两次循环,没有必要

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define NUM 3    //3个候选者
  4. #define NO 5    //5个投票者

  5. struct candidate
  6. {
  7.         char name[10];
  8.         int score;
  9. }candidate[NUM] = {{"小可爱", 0}, {"小甲鱼", 0}, {"路人甲", 0}};

  10. int main(void)
  11. {
  12.         int i, j;
  13.         char cand[10];
  14.         int k = NO;
  15.         printf("欢迎进入良好公民评选投票系统:\n\n");
  16.         printf("候选人有:");
  17.         for(i = 0; i < NUM; i++)
  18.         {
  19.                 printf("%s ", candidate[i].name);
  20.         }
  21.         printf("\n\n");


  22.         for(i = 0; i < k; i++)
  23.         {
  24.                 printf("第 %d 位投票,请写下支持的候选人的名字:", i + 1);
  25.                 scanf("%s", &cand);

  26.                 for(j = 0; j < NUM; ++j)
  27.                 {
  28.                         if(strcmp(cand, candidate[j].name) == 0)
  29.                         {
  30.                                 candidate[j].score++;                       //这里candidate[i].score++;为什么不能增加票数呀
  31.                         }
  32.                 }
  33.         }

  34.         printf("\n\n");

  35.         for(i = 0; i < NUM; i++)
  36.         {
  37.                 printf("%s 同学得票数位:%d\n", candidate[i].name, candidate[i].score);
  38.         }
  39.         printf("\n");
  40.         printf("本次投票活动的胜利者是:");
  41.        
  42.        
  43.         struct candidate *max = &candidate[0];
  44.         for(i = 1; i < NUM; i++)        // 求票数的最大值
  45.         {
  46.                 if(max->score < candidate[i].score)
  47.                 {
  48.                         max = &candidate[i];
  49.                 }
  50.         }
  51.        
  52.         printf("%s\n", max->name);        // 打印票数最大值的人的名字

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

使用道具 举报

发表于 2018-2-22 05:12:24 | 显示全部楼层    本楼为最佳答案   
  1. for(i = 0; i < k; i++)
  2.         {
  3.                 printf("第 %d 位投票,请写下支持的候选人的名字:", i + 1);
  4.                 scanf("%s", &cand);

  5.                 for(j = 0; j < NUM; ++j)
  6.                 {
  7.                         if(strcmp(cand, candidate[j].name) == 0)
  8.                         {
  9.                                 candidate[j].score++;                       //这里candidate[i].score++;为什么不能增加票数呀
  10.                         }
  11.                 }
  12.         }
复制代码





  1.         max = candidate[0].score;
  2.         for(i = 0;i < NUM; i++)  //求票数的最大值
  3.         {
  4.                 if(max < candidate[i].score)
  5.                 {
  6.                         max = candidate[i].score;
  7.                 }
  8.         }
  9.         for(i = 0;i < NUM; i++)  //打印票数最大值的人的名字
  10.         {
  11.                 if(max == candidate[i].score)
  12.                 {
  13.                         printf("%s\n", candidate[i].name);
  14.                 }
  15.         }
复制代码

这里两次循环,没有必要

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define NUM 3    //3个候选者
  4. #define NO 5    //5个投票者

  5. struct candidate
  6. {
  7.         char name[10];
  8.         int score;
  9. }candidate[NUM] = {{"小可爱", 0}, {"小甲鱼", 0}, {"路人甲", 0}};

  10. int main(void)
  11. {
  12.         int i, j;
  13.         char cand[10];
  14.         int k = NO;
  15.         printf("欢迎进入良好公民评选投票系统:\n\n");
  16.         printf("候选人有:");
  17.         for(i = 0; i < NUM; i++)
  18.         {
  19.                 printf("%s ", candidate[i].name);
  20.         }
  21.         printf("\n\n");


  22.         for(i = 0; i < k; i++)
  23.         {
  24.                 printf("第 %d 位投票,请写下支持的候选人的名字:", i + 1);
  25.                 scanf("%s", &cand);

  26.                 for(j = 0; j < NUM; ++j)
  27.                 {
  28.                         if(strcmp(cand, candidate[j].name) == 0)
  29.                         {
  30.                                 candidate[j].score++;                       //这里candidate[i].score++;为什么不能增加票数呀
  31.                         }
  32.                 }
  33.         }

  34.         printf("\n\n");

  35.         for(i = 0; i < NUM; i++)
  36.         {
  37.                 printf("%s 同学得票数位:%d\n", candidate[i].name, candidate[i].score);
  38.         }
  39.         printf("\n");
  40.         printf("本次投票活动的胜利者是:");
  41.        
  42.        
  43.         struct candidate *max = &candidate[0];
  44.         for(i = 1; i < NUM; i++)        // 求票数的最大值
  45.         {
  46.                 if(max->score < candidate[i].score)
  47.                 {
  48.                         max = &candidate[i];
  49.                 }
  50.         }
  51.        
  52.         printf("%s\n", max->name);        // 打印票数最大值的人的名字

  53.         return 0;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-22 17:21:03 | 显示全部楼层
人造人 发表于 2018-2-22 05:12
这里两次循环,没有必要

好 感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 09:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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