题目:
候选人计票程序
设计一个进行候选人的选票程序,假设又三位候选人,在屏幕上输入要选择的候选人的姓名,又5次投票机会,最后输出每个人的的票结果
源代码:
#include <stdio.h>
#include <string.h>
struct candidate
{
char name[20];
int votes;
};
int main()
{
struct candidate cand[3] =
{
{"yu",0},{"tian",0},{"cai",0}
};
int total_votes=0;
char votes_name[20];
for(;total_votes < 5;total_votes++)
{
scanf("%s",votes_name);
if(strcmp(votes_name,cand[0].name) == 0) cand[0].votes++;
if(strcmp(votes_name,cand[1].name) == 0) cand[1].votes++;
if(strcmp(votes_name,cand[2].name) == 0) cand[2].votes++;
}
for(int i = 0;i < 3;i++) printf("%s 票数: %d\n",cand[i].name,cand[i].votes);
return 0;
}
演示效果:
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈