#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct XuanMin
{
char name[32];
int tickets;
};
int main()
{
struct XuanMin xm[3];
struct XuanMin max;
int i,j;
int len;
int total = 5;
char tmpName[32];
int mark;
int feipiao = 0;
//初始化选民信息
len = sizeof(xm)/sizeof(xm[0]);
for(i=0;i<len;i++)
{
xm[i].tickets = 0;
printf("请输入第%d个选民的名字:\n",i+1);
scanf("%s",xm[i].name);
}
//唱票环节
for(i = 0;i < total;i++)
{
mark = 0;
printf("请输入你投给谁:\n");
memset(tmpName,'\0',sizeof(tmpName));//每次清空一下
scanf("%s",tmpName);
for(j = 0;j < len;j++)
{
if(strcmp(tmpName,xm[j].name) == 0)
{
xm[j].tickets++;
mark = 1;
}
}
if(mark == 0)
{
printf("没有此候选人,作废!\n");
feipiao++;
}
}
//公布结果
max = xm[0];
for(i = 0;i < len;i++)
{
printf("名字:%s,票数:%d\n",xm[i].name,xm[i].tickets);
if(max.tickets < xm[i].tickets)
{
max = xm[i];
}
}
printf("本次投票总计%d票,有效票数为%d票,废票为%d票,选民%s以%d票当选。\n",total,total-feipiao,feipiao,max.name,max.tickets);
return 0;
}
结构体票选
最新推荐文章于 2024-06-03 14:07:40 发布