投票评选十佳运动员

更新时间:2024-04-02 06:28:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

十佳运动员评选活动

一、功能要求

某市体委与电视台联合举办十佳运动员有奖评选活动,具体说明如下

(1)体委组织有关人士评出了如表1所示的20个候选人名单

运动员编号 运动员姓名 运动员编号 运动员姓名

(2)电视台在网上设立了投票站供市民投票,以便用计算机进行统计和核对。选票格式如表2所示

选票编号 0000001 投票人姓名 投票人地址 拟选运动员编号

选票号为7位数字,有效的运动员编号是01~20

(3)计算机统计的具体任务是

① 统计出各候选人的得票数,并根据得票数排定名次,选出十佳人员

② 根据命中率选出10个获奖的参选者,并排定名次

命中率=命中分+次序分

命中分:选中十佳中的一个即得10分,选中n个得n╳10分(不考虑次序)

次序分:选票中的第一个运动员与十佳中的第一名相符(简称选中第一名)得9分

选中第二名得8分,……,选中第十名的0分

(4)编写出完成以上统计任务的程序

具体要求如下

1 候选人数据和选票数据应以文本文件的方式分别存放在两个文件中,选票中参选

的地址可以不考虑

2程序中,对选票数据要求采用结构体作数据结构

3 程序除能完成统计功能外,应具有核对选票数据的功能,并且每一功能的实现要

用选择菜单的方式进行(使用简单的文本菜单),菜单包含以下几项

a. 统计

b. 核对选票

c. 退出

4 各个功能以及相对独立的任务要求编写成独立的函数,主函数只用于管理菜单和

织调用个功能函数

5统计结果除在屏幕显示外,还要求输出到文件中 最好用上链表

#include #include #include #include #include #include

/* 存储运动员信息的文件 */ #define DATA1 \ /* 存储选票信息的文件 */ #define DATA2 \

typedef struct sportsman {

int number;

char name[21]; int tickets;

struct sportsman *next; } sportsman;

typedef struct ticket {

char ticknum[8]; char votername[21]; char voteraddr[51]; int sportsman; int scores; struct ticket *next; } ticket;

int SIZE1, SIZE2;

sportsman *head1, *tail1; ticket *head2, *tail2;

void init(); void cleanup(); void create(); void process(); sportsman *sortlist1(); ticket *sortlist2(); void showdetail(); void showtop10(); void clearlist();

void init() {

SIZE1 = sizeof(sportsman);

SIZE2 = sizeof(ticket);

head1 = tail1 = (sportsman *)malloc(SIZE1); head2 = tail2 = (ticket *)malloc(SIZE2); memset(head1, 0, SIZE1); memset(head2, 0, SIZE2); }

void create() {

FILE *infile = fopen(DATA1, \

if (infile == NULL)

{

printf(\无法打开文件1\\n\ cleanup(); exit(1); } while (1) {

char nm[21]; int num;

memset(nm, 0, 21); num = 0;

fscanf(infile, \

if (num == 0) break;

sportsman *node = (sportsman *)malloc(SIZE1); node->number = num; node->tickets = 0;

fscanf(infile, \ tail1->next = node; tail1 = node; }

tail1->next = NULL; fclose(infile);

infile = fopen(DATA2, \

if (infile == NULL) {

printf(\无法打开文件2\\n\

cleanup(); exit(1); } while (1) {

char num[8];

memset(num, '\\0', 8); fscanf(infile, \

if (num[0] == '\\0') break;

ticket *node = (ticket *)malloc(SIZE2); memset(node, 0, SIZE2);

strcpy(node->ticknum, num);

fscanf(infile, \&node->sportsman); tail2->next = node; tail2 = node; }

fclose(infile); }

void clearlist() {

sportsman *q1, *p1; q1 = p1 = head1->next; ticket *q2, *p2;

q2 = p2 = head2->next;

while (p1 != NULL) {

q1 = q1->next; free(p1); p1 = q1; }

while (p2 != NULL) {

q2 = q2->next; free(p2); p2 = q2; }

tail1 = head1; tail2 = head2; head1->next = NULL; head2->next = NULL; }

void cleanup() {

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;

else

tail = tail->next = max;

if (max == head) head = head->next; else if (prev)

prev->next = max->next; }

if (first != NULL) {

tail->next = NULL; tail1 = tail; }

return first; }

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; else

tail = tail->next = max;

if (max == head) head = head->next; else if (prev)

prev->next = max->next; }

if (first != NULL) {

tail->next = NULL; tail2 = tail; }

return first; }

void process() {

if (head1->next != NULL || head2->next != NULL) clearlist();

create();

sportsman *p1 = head1->next; ticket *p2; int score = 9;

while (p1 != NULL) {

p2 = head2->next;

while (p2 != NULL)

{

if (p2->sportsman == p1->number) ++p1->tickets;

p2 = p2->next; }

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(); }

void showtop10()

{

printf(\最佳前10名运动员名单:\\n\ sportsman *p1 = head1->next; int i = 0;

ticket *p2 = head2->next;

while (p1 != NULL && i < 10)

{

printf(\ p1 = p1->next; ++i; }

printf(\最佳前10名投票人名单:\\n\ i = 0;

while (p2 != NULL && i < 10) {

printf(\ p2 = p2->next; ++i; } }

void showdetail() {

printf(\详细投票信息:\\n\ int pagesize = 10;

ticket *p1 = head2->next;

while (p1 != NULL) {

printf(\选票编号: %s\\n\ printf(\投票人姓名:%s\\n\ printf(\投票人住址:%s\\n\

int sportsman = p1->sportsman; printf(\运动员编号:\

if (sportsman < 10)

printf(\ else

printf(\

if (--pagesize == 0)

{

pagesize = 10;

printf(\按任意键继续。。。\ getch(); }

p1 = p1->next; } }

void main()

{

printf(\欢迎使用简单投票系统\\n\ char choice; init();

while (1)

{

printf(\功能选择:\\n\

printf(\统计\\tb. 核对选票\\tc. 退出\\n\ choice = getchar(); while (getchar() != '\\n');

switch (choice) {

case 'a': process(); break; case 'b':

showdetail(); break; case 'c': cleanup(); exit(0);

}

printf(\按任意键继续。。。\

getchar(); system(\ } }

本文来源:https://www.bwwdw.com/article/fehr.html

Top