您的位置:首页 > 其它

项目实践:对候选人得票的统计程序

2016-06-30 19:06 369 查看

6.37对候选人得票的统计程序。

 一、问题描述:

      设有3个候选人zhou,he,lu,最终只能有1人当选为领导。今有10个人参加投票,从键盘先后输入这10个人所投的候选人的名字,名字写错,则选票作废。要求最后输出这3个候选人的得票结果。要求用结构体数组candidate表示3个候选人的姓名和得票结果。

二、代码实现:

#include<stdio.h>
#include<string.h>
struct candidate
{
char name[20];
int countn;
}a[3]={{"zhou",0},{"he",0},{"lu",0}};
//判断字符是否相同
void fcount()
{
int i;
char temp[20];
for(i=0;i<10;i++)
{
scanf("%s",temp);
if (strcmp(temp,a[0].name)==0)a[0].countn+=1;
if (strcmp(temp,a[1].name)==0)a[1].countn+=1;
if (strcmp(temp,a[2].name)==0)a[2].countn+=1;
//以上程序调用了strcmp函数
}

}
void print()
{
int i,j,t;

for(i=0;i<3;i++)
{
printf("%s :%d\n",a[i].name,a[i].countn);
}
}
int main()
{

printf("please input the name of candidate(zhou ,he ,lu):\n");
fcount();
printf("the vote result:\n");
print();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: