|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 莫问流年 于 2014-5-8 20:29 编辑
#include <stdio.h>
#include <string.h>
#define VOTERS 10
struct candidate
{
char name[30];
}cand[] = { { "刘德华" }, { "张国立" }, { "张三丰" }, { "孙悟空" } };
int max(int a, int b, int c, int d);
void main()
{
int i, a = 0, b = 0, c = 0, d = 0, e = 0;
char votes[30];
printf("*************欢迎进入良好公民评选投票系统**************\n");
printf("候选人有:刘德华,张国立,张三丰,孙悟空\n");
for (i = 1; i <= VOTERS; i++)
{
printf("第 %d 人投票,请写下你支持的候选人名字: ", i);
scanf_s("%s", votes);
if (!strcmp(votes, cand[0].name))
{
a++;
}
else if (!strcmp(votes, cand[1].name))
{
b++;
}
else if (!strcmp(votes, cand[2].name))
{
c++;
}
else if (!strcmp(votes, cand[3].name))
{
d++;
}
else
{
e++;
}
fflush(stdin);//votes[0] = 0;
}
printf("共%d人参与投票,其中%d人因投票有误视为弃权,投票的最终结果为:\n", VOTERS, e);
printf("刘德华同学的得票数为:\t%d\n", a);
printf("张国立同学的得票数为:\t%d\n", b);
printf("张三丰同学的得票数为:\t%d\n", c);
printf("孙悟空同学的得票数为:\t%d\n", d);
if (max(a, b, c, d) == a)
{
printf("\n本次投票的获胜者是:\t%s", cand[0].name);
}
else if (max(a, b, c, d) == b)
{
printf("\n本次投票的获胜者是:\t%s", cand[1].name);
}
else if (max(a, b, c, d) == c)
{
printf("\n本次投票的获胜者是:\t%s", cand[2].name);
}
else
{
printf("\n本次投票的获胜者是:\t%s", cand[3].name);
}
}
int max(int a, int b, int c, int d)
{
int max1, max2, max;
max1 = (a>b) ? a : b;
max2 = (c>d) ? c : d;
max = (max1>max2) ? max1 : max2;
return max;
}
|
|