【C语⾔】统计候选⼈的得票数
编程统计候选⼈的得票数。设有3个候选⼈zhang、li、wang(候选⼈姓名不区分⼤⼩写),
10个选民,选民每次输⼊⼀个得票的候选⼈的名字,若选民输错候选⼈姓名,则按废票处理。
选民投票结束后程序⾃动显⽰各候选⼈的得票结果和废票信息。要求⽤结构体数组candidate
表⽰3个候选⼈的姓名和得票结果。
例如:
Input vote 1:li
Input vote 2:li
Input vote 3:Zhang
Input vote 4:wang
Input vote 5:zhang
Input vote 6:Wang
Input vote 7:Zhang
Input vote 8:wan
Input vote 9:li
Input vote 10:lii咸肉
Election results:
li:3
zhang:3
wang:2
Wrong election:2
输⼊格式:
"Input vote %d:"
"%s"
输出格式:
"Election results:\n"囚犯的意思
"%8s:%d\n"
"Wrong election:%d\n"
注意:
A. 上述输出格式中,"%8s:%d\n"中%8s表⽰输出候选⼈的姓名;%d表⽰输出相应候选⼈的的票数。
排序excel怎么设置
B. 另外,⽆论是否存在废票的情况,最终都需要输出"Wrong election:%d\n"。
C. 本题要求使⽤结构体。
#include<stdio.h>
#include<string.h>
typedef struct vote
{
char name[10];
int count;
} VOTE;
main()
{
VOTE vote[10]={{"li",0},{"zhang",0},{"wang",0},{"Zhang",0},{"Wang",0}};
int i;
char name[10];
for (i = 0; i < 10; i++)
{
printf("Input vote %d:", i + 1);
scanf("%s", name);
if (strcmp(vote[2].name, name) == 0 || strcmp(vote[4].name, name) == 0)
vote[2].count++;
el if (strcmp(vote[0].name, name) == 0)
vote[0].count++;
el if (strcmp(vote[3].name, name) == 0 || strcmp(vote[1].name, name) == 0)
vote[1].count++;
el
vote[3].count++;
}
陈述句怎么改printf("Election results:\n");
for (i = 0; i < 3; i++)
{
printf("%8s:%d\n", vote[i].name, vote[i].count);
}
printf("Wrong election:%d\n", vote[3].count);
}
⽅法⼀
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
struct Candidate
{
char name[10];
int count;
};
int main()
h壁纸
试用期三个月{
struct Candidate arr[3] = {
"li",0,"zhang",0,"wang",0
};
int i = 0, j = 0;
char s[10];
int wrong = 0;
int flag = 0;
for (i = 0; i < 10; i++)
{
flag = 0;
printf("Input vote %d:", i + 1);
scanf("%s", s);
for (j = 0; j < 3; j++)
最特别的爱
{
if (strcacmp(arr[j].name, s) == 0)
{
arr[j].count++;
flag = 1;
}
无孔不入}
if (flag == 0)
{
wrong++;
}
}
printf("Election results:\n");
for (i = 0; i < 3; i++)
{
printf("%8s:%d\n", arr[i].name, arr[i].count); }
printf("Wrong election:%d\n", wrong);
}
⽅法⼆