c语言 选票
答案:3 悬赏:10
解决时间 2021-02-12 10:08
- 提问者网友:巴黎塔下许过得承诺
- 2021-02-11 09:27
c语言 选票
最佳答案
- 二级知识专家网友:输掉的尊严
- 2021-02-11 10:11
#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");
}
}
#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");
}
}
全部回答
- 1楼网友:不服输就别哭
- 2021-02-11 11:54
大侠出马,药到病除!
- 2楼网友:努力只為明天
- 2021-02-11 10:56
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]++;
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯