编程统计候选人得票数,假设有3个候选人,名字分别为Li,Zhang和Fun。使用结构体存储每一个候选人的名字和得票数。记录每一张选票的得票人名,输出每个候选人最终的得票数。
#include <stdio.h>#include <stdlib.h>#include <string.h>#define N 10#define M 3typedef struct candidate{char name[N];int votes;int Invalid_ticket;}CAND;void Vote(int n,CAND arr[]);int main(){CAND candidate[M] = {{"zhang",0,0},{"li",0,0 },{"wang",0,0 }};int n;printf("please input the number of voters: \n");scanf("%d",&n);printf("the number of voters is %d \n",n);Vote(n,candidate);return 0;}Vote(int n,CAND arr[]){int i,j;char name[10] ;for(i = 0; i <= n ;i++){gets(name);for(j = 0; j< 3;j++){if(strcmp(name , arr[j].name)==0)arr[j].votes++;}}for(j = 0;j<3;j++){printf("%s: %d \n",arr[j].name,arr[j].votes);}// printf("%s: %d",arr[0].name,arr[1].votes);}