帮忙修改一下程序,一道选举题,按要求改一下。谢谢啦!
某单位决定由全体员工选举总经理,技术总监,财务总监和销售经理。有20个候选人,编号分别是1.2.3.4......19.20,从中选举4人为总经理,技术总监,财务总监和销售经理。每个员工一张选票,每张选票最多可填写4人。请编程实现统计选票工作。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CANDIDATES 4
const char candi[4][10] ={"zhao","qian","sun","li"};//参选人名
const char jobs[4][29] ={"general manager","technical director",
"Financial Controller","marketing manager"};//职位名称
struct votepaper
{
char paper[5];
struct votepaper *next;
};
typedef struct votepaper vote;
void papercount(vote *head, int count[][5]);
vote *creatlist(int num);
int votecount(char *p, int i);
int main(void)
{
vote *head;
int num;
int count[CANDIDATES][5] = {0};//4*4统计表
printf("Enter the number of voter,num:\n");
scanf("%d", &num);//选民人数
head = creatlist(num);//建立链表
papercount(head, count);//统计选票
return 0;
}
vote *creatlist(int num)
{
vote *head, *pf, *pb;
int i;
head = (vote *) malloc(sizeof(vote));
head->next = NULL;
if(head == NULL)
{
printf("Failed to creat list;\n");
exit(1);
}
else
{ pf = head;
/*选民输入格式为对应人名下输入相应职位名称的
头一个字母的大写,对于不投给他票的下面画X, */
printf("Pls enter the characters:\n");
printf("such as:GTF, TFGM or XXG……\n");
printf("G: general manager T: technical director\n");
printf("F: Financial Controller M: marketing manager\n");
printf("X: means no choice of the man\n");
for(i=0; i< num; i++)
{
pb = (vote *) malloc(sizeof(vote));
printf("\n------------------------------------------\n");
printf("candidates:\nzhao\tqian\tsun\tli\n");
printf("------------------------------------------\n");
scanf("%s",pb->paper);
pf->next = pb;
pb->next = NULL;
pf = pb;
}
}
return head;
}
void papercount(vote *head, int count[][5])
{
int i,j,max;
int result;
head = head->next;//头结点为空,指向其下一个结点。
while(head != NULL)//每个结点(选民投的票)统计
{
for(i=0; i < CANDIDATES; i++ )
{
result = votecount(head->paper, i);//查表转换成对应的列数
count[i][result]++;//行为人名,列为职位名
}
head = head->next;
}
printf("\t");
printf("%c\t%c\t%c\t%c\n",'G','T','F','M');
for(i=0; i < 4; i++)
{
printf("%5s:\t", candi[i]);
for(j=0; j < CANDIDATES; j++)
printf("%d\t",count[i][j]);
printf("\n");
}
for(i=0; i < 4; i++) //输出统计结果,每列比较大小,每列中最大的为该职位的当选者。
{
max = 0;
for(j=0; j < CANDIDATES; j++)
{
if(count[i][j] >count[i][max])
max = j;
}
printf("Congratulations! %s is %s !\n", jobs[i], candi[max]);
}
}
int votecount(char *p, int i)
{
int count;
switch(p[i])
{
case 'G':count = 0;break;
case 'T':count = 1;break;
case 'F':count = 2;break;
case 'M':count = 3;break;
default: count = 4;
}
return count;
}