#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct person
{
char name[10];
int count;
};
int main()
{
struct person candidate[3] = { "zhang",0,"li",0,"wang",0 };
struct person* p = candidate;
printf("请输入支持的候选人姓名:\n");
for (int i = 0; i < 10; i++)
{
p = candidate;
char voteName[10]=" ";
scanf("%s", voteName);
int flag = 0;//判断是否为废票,flag = 0 则为废票
for (int j = 0; j < 3; j++,p++)
{
if (strcmp(voteName, p->name) == 0)
{
p->count++;
flag = 1;
}
}
if (flag == 0)
{
printf("您的输入不合法,此票作废\n");
}
}
p = candidate;
for (int i = 0; i < 3; i++,p++)
{
printf("%8s:%d\n", p->name, p->count);
}
//统计废票数
int vote = 10;
p = candidate;
for (int i = 0; i < 3; i++,p++) {
vote -= p->count;
}
printf(" 废票数:%d\n", vote);
return 0;
}
注意:指针p指向数组的位置要及时改