投票评选十佳运动员 12页

  • 31.50 KB
  • 2022-09-27 发布

投票评选十佳运动员

  • 12页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档由用户上传,淘文库整理发布,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,请立即联系网站客服。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细阅读内容确认后进行付费下载。
  4. 网站客服QQ:403074932
十佳运动员评选活动一、功能要求某市体委与电视台联合举办十佳运动员有奖评选活动,具体说明如下(1)体委组织有关人士评出了如表1所示的20个候选人名单运动员编号运动员姓名运动员编号运动员姓名(2)电视台在网上设立了投票站供市民投票,以便用计算机进行统计和核对。选票格式如表2所示选票编号0000001投票人姓名投票人地址拟选运动员编号选票号为7位数字,有效的运动员编号是01~20(3)计算机统计的具体任务是①统计出各候选人的得票数,并根据得票数排定名次,选出十佳人员②根据命中率选出10个获奖的参选者,并排定名次命中率=命中分+次序分命中分:选中十佳中的一个即得10分,选中n个得n╳10分(不考虑次序)次序分:选票中的第一个运动员与十佳中的第一名相符(简称选中第一名)得9分选中第二名得8分,……,选中第十名的0分(4)编写出完成以上统计任务的程序具体要求如下1\n候选人数据和选票数据应以文本文件的方式分别存放在两个文件中,选票中参选的地址可以不考虑2程序中,对选票数据要求采用结构体作数据结构3程序除能完成统计功能外,应具有核对选票数据的功能,并且每一功能的实现要用选择菜单的方式进行(使用简单的文本菜单),菜单包含以下几项a.统计b.核对选票c.退出4各个功能以及相对独立的任务要求编写成独立的函数,主函数只用于管理菜单和织调用个功能函数5统计结果除在屏幕显示外,还要求输出到文件中最好用上链表#include#include#include#include#include#include/*存储运动员信息的文件*/#defineDATA1"data1.txt"/*存储选票信息的文件*/#defineDATA2"data2.txt"typedefstructsportsman{intnumber;\ncharname[21];inttickets;structsportsman*next;}sportsman;typedefstructticket{charticknum[8];charvotername[21];charvoteraddr[51];intsportsman;intscores;structticket*next;}ticket;intSIZE1,SIZE2;sportsman*head1,*tail1;ticket*head2,*tail2;voidinit();voidcleanup();voidcreate();voidprocess();sportsman*sortlist1();ticket*sortlist2();voidshowdetail();voidshowtop10();voidclearlist();voidinit(){SIZE1=sizeof(sportsman);SIZE2=sizeof(ticket);head1=tail1=(sportsman*)malloc(SIZE1);head2=tail2=(ticket*)malloc(SIZE2);memset(head1,0,SIZE1);memset(head2,0,\nSIZE2);}voidcreate(){FILE*infile=fopen(DATA1,"r");if(infile==NULL){printf("无法打开文件1\n");cleanup();exit(1);}while(1){charnm[21];intnum;memset(nm,0,21);num=0;fscanf(infile,"%d",&num);if(num==0)break;sportsman*node=(sportsman*)malloc(SIZE1);node->number=num;node->tickets=0;fscanf(infile,"%s",node->name);tail1->next=node;tail1=node;}tail1->next=NULL;fclose(infile);infile=fopen(DATA2,"r");if(infile==NULL){printf("无法打开文件2\n");\ncleanup();exit(1);}while(1){charnum[8];memset(num,'\0',8);fscanf(infile,"%s",num);if(num[0]=='\0')break;ticket*node=(ticket*)malloc(SIZE2);memset(node,0,SIZE2);strcpy(node->ticknum,num);fscanf(infile,"%s%s%d",node->votername,node->voteraddr,&node->sportsman);tail2->next=node;tail2=node;}fclose(infile);}voidclearlist(){sportsman*q1,*p1;q1=p1=head1->next;ticket*q2,*p2;q2=p2=head2->next;while(p1!=NULL){q1=q1->next;free(p1);p1=q1;}\nwhile(p2!=NULL){q2=q2->next;free(p2);p2=q2;}tail1=head1;tail2=head2;head1->next=NULL;head2->next=NULL;}voidcleanup(){free(head1);free(head2);}sportsman*sortlist1(sportsman*head){sportsman*cursor,*first,*tail,*prev,*max;first=NULL;while(head!=NULL){for(cursor=max=head;cursor->next!=NULL;cursor=cursor->next){if(cursor->next->tickets>max->tickets){prev=cursor;max=cursor->next;}}if(first==NULL)first=tail=max;\nelsetail=tail->next=max;if(max==head)head=head->next;elseif(prev)prev->next=max->next;}if(first!=NULL){tail->next=NULL;tail1=tail;}returnfirst;}ticket*sortlist2(ticket*head){ticket*cursor,*first,*tail,*prev,*max;first=NULL;while(head!=NULL){for(cursor=max=head;cursor->next!=NULL;cursor=cursor->next){if(cursor->next->scores>max->scores){prev=cursor;max=cursor->next;}}if(first==NULL)first=tail=max;elsetail=tail->next=max;\nif(max==head)head=head->next;elseif(prev)prev->next=max->next;}if(first!=NULL){tail->next=NULL;tail2=tail;}returnfirst;}voidprocess(){if(head1->next!=NULL||head2->next!=NULL)clearlist();create();sportsman*p1=head1->next;ticket*p2;intscore=9;while(p1!=NULL){p2=head2->next;while(p2!=NULL){if(p2->sportsman==p1->number)++p1->tickets;p2=p2->next;\n}p1=p1->next;}head1->next=sortlist1(head1->next);p2=head2->next;while(p2!=NULL&&score>0){p1=head1->next;while(p1!=NULL){if(p2->sportsman==p1->number){p2->scores+=score;break;}p1=p1->next;}--score;p2=p2->next;}head2->next=sortlist2(head2->next);showtop10();}voidshowtop10(){printf("最佳前10名运动员名单:\n");sportsman*p1=head1->next;inti=0;ticket*p2=head2->next;while(p1!=NULL&&i<10)\n{printf("%d\t%s\t%d\n",i+1,p1->name,p1->tickets);p1=p1->next;++i;}printf("\n最佳前10名投票人名单:\n");i=0;while(p2!=NULL&&i<10){printf("%d\t%s\t%\n",i+1,p2->votername,p2->scores);p2=p2->next;++i;}}voidshowdetail(){printf("详细投票信息:\n");intpagesize=10;ticket*p1=head2->next;while(p1!=NULL){printf("选票编号:%s\n",p1->ticknum);printf("投票人姓名:%s\n",p1->votername);printf("投票人住址:%s\n",p1->voteraddr);intsportsman=p1->sportsman;printf("运动员编号:");if(sportsman<10)printf("0%d\n",sportsman);elseprintf("%d\n",sportsman);\nif(--pagesize==0){pagesize=10;printf("\n按任意键继续。。。");getch();}p1=p1->next;}}voidmain(){printf("欢迎使用简单投票系统\n");charchoice;init();while(1){printf("功能选择:\n");printf("a.统计\tb.核对选票\tc.退出\n");choice=getchar();while(getchar()!='\n');switch(choice){case'a':process();break;case'b':showdetail();break;case'c':cleanup();exit(0);}printf("\n按任意键继续。。。");\ngetchar();system("cls");}}

相关文档