|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CNT 10 //宏定义,投票次数
/**希望大家指正...*/ {:1_1:}
#if(1)
/**统计候选人得票,设有三个候选人,每次输入一个候选人的名字,要求最后输出各个人的得票数和最高得票人*/
struct person
{
char name[20]; //名字
int count; //统计的次数
};
int main()
{
struct person persons[3]={{"杨伟",0},{"甘炜",0},{"裔楠",0}}; //定义一个结构体数组并初始化
int i,max=0;
char temp[20]; //临时保存输入的人名
printf("\t\t欢迎进入投票系统\n\n");
printf("\t候选人有:%s , %s , %s \n\n\n",persons[0].name,persons[1].name,persons[2].name);
for(i=0;i<CNT;i++)
{
printf("第%d位投票,请输入支持的候选人名字: ",i+1);
gets(temp);
if(strcmp(persons[0].name,temp)==0)
{
persons[0].count++;
}
else if(strcmp(persons[1].name,temp)==0)
{
persons[1].count++;
}
else if(strcmp(persons[2].name,temp)==0)
{
persons[2].count++;
}
}
printf("\n\n");
//打印所有人得票,求最高投票数
for(i=0;i<3;i++)
{
printf("\t%s 得票数是:%d\n\n",persons.name,persons.count);
//max保存最大值下标
if(persons.count>persons[max].count)
max=i;
}
//打印最大值
printf("\n\n\t此次投票胜利者是:%s\n\n",persons[max].name);
system("pause");
}
#endif;
|
|