中易网

c语言 选票

答案:3  悬赏:10  
解决时间 2021-02-12 10:08
c语言 选票
最佳答案
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>

#define DATA1 "data1.txt"

#define DATA2 "data2.txt"

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, "r");

if (infile == NULL)
{
printf("无法打开文件1\n");
cleanup();
exit(1);
}

while (1)
{
char nm[21];
int num;
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");
cleanup();
exit(1);
}

while (1)
{
char num[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);
}

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("%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;
}
}

void showdetail()
{
printf("详细投票信息:\n");
int pagesize = 10;
ticket *p1 = head2->next;

while (p1 != NULL)
{
printf("选票编号: %s\n", p1->ticknum);
printf("投票人姓名:%s\n", p1->votername);
printf("投票人住址:%s\n", p1->voteraddr);

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

if (sportsman < 10)
printf("0%d\n", sportsman);
else
printf("%d\n", sportsman);

if (--pagesize == 0)
{
pagesize = 10;
printf("\n按任意键继续。。。");
getch();
}

p1 = p1->next;
}
}

void main()
{
printf("欢迎使用简单投票系统\n");
char choice;
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按任意键继续。。。");
getchar();
system("cls");
}
}
全部回答
大侠出马,药到病除!
void countrs(void) {  int i,j; int count; for(i=0;i<10;i++) yy[i]=0; for(i=0;i<100;i++) { count=0; for(j=0;j<10;j++) if(xx[i][j]=='1') count++; if(count<=5) for(j=0;j<10;j++) if(xx[i][j]=='1') yy[j]++; } }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
在平面直角坐标系中,已知点A(-4,0),B(0,2),
七福平价商场地址有知道的么?有点事想过去
西安哪个学校有社会工作专业的研究生
1912年民国建国之后有几个省?
急!!!我家这么刷墙,面积怎么算???
主角性格冷漠孤独一心向道偏向魔的玄幻小说
羊圈子镇农垦佳苑和泰馨城小区地址有知道的么
win7旗舰版32位怎样安装sql2000
转过呼啦圈后肠子疼了一星期了,怎么回事?
TL494电压闭环反馈到哪个管脚?
完美国际妖精平民都带啥BB?
函数f(x)=ax2+1,x≥0(a+2)eax,x<0为R的
请问如果一台交换机不支持VLAN该怎么划分VLAN
灵狐者把工作辞了以后,这天她又想到了新工作.
家里给买车的钱,被我昨晚一夜输掉了,我该怎
推荐资讯
径山古道怎么去啊,有知道地址的么
广渠门内大街/崇外大街(路口)我想知道这个在
遵义羊肉粉我想知道这个在什么地方
同一网段 ping不通
羊血成块,水和血的比例
喷油嘴故障喷油量过大会导致什么爆震吗
DNF征战者90异界套哪套好 90版本征战者异界套
盼盼起个什么近音或者好听的女英文名呢
婴之坊这个地址在什么地方,我要处理点事
一个男生明明很污(他好朋友跟我说的),为什
wifi能上但电脑上不了!用诊断网络说是网卡驱
西樵镇二社区卫生服务站这个地址在什么地方,
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?