题目描述
在很多的电视大赛中,通常当选手表演结束后,现场观众通过手中的按键对参赛选手进行投票,然后对选手获得的票数进行统计,从高到低进行降序排列,从而自动产生冠军、亚军和季军。现在要求编写一程序模拟实现上述系统的功能。
(1) 在本例中首先输入参赛选手的人数(范围为1-9个);
(2) 将选手的编号和姓名依次存入顺序表单元中;
(3) 观众通过按键进行投票,按“1”为1号选手投票,按“2”为2号选手投票,以此类推,已按零作为投票结束标志;
(4) 投票结束后进行排序,然后为每个选手计算名次,得票相同的名次也相同。
(5) 在主函数中调用菜单函数调试程序。
存储类型的定义
参赛选手信息存储类型的定义:
typedef struct node{
char name[8]; //选手姓名
int num; //选手编号
int score; //选手得分
int tax; //选手名次
}Node;
(请使用滚轮下翻)
#include <stdlib.h>
#include <string.h>
typedef struct Score
{
char AthleteName[20];
char SchoolName[20];
int SchoolCode;
int Rank;
char Achievement[10];
} Score;
typedef struct Match
{
int MatchCode;
char MatchName[10];
int Top;
Score *RankList;//比赛项目里面包含着分数(储存结构层面)
} Match, *Event;
typedef struct Record
{
int MatchCode;
char MatchName[10];
int Rank;
char Achievement[10];
char AthleteName[20];
int GetScore;
} Record;
typedef struct School
{
char SchoolName[20];
int num;
int ptr;
Record *RecordList;
} School, *SchoolSheet;
typedef struct Team
{
int SchoolCode;
char SchoolName[20];
int ManTotal;
int WomanTotal;
int TeamTotal;
} Team, *All;
int n, m, w, t;
Event A;
SchoolSheet B;
All C;
int ScoreList[8] = {5, 3, 2, 7, 5, 3, 2, 1};
void Start()
{
printf("--------------------------
");
printf("欢迎使用运动会分数统计系统
");
printf("--------------------------
");
return;
}
void Function()
{
printf("-----功能选择-----
");
printf("1.初始化
");
printf("2.清零
");
printf("3.录入各项比赛成绩
");
printf("4.产生学校成绩单
");
printf("5.产生团体总分报表
");
printf("6.结果显示
");
printf("7.返回主界面
");
printf("0.退出系统
");
return;
}
void login()
{
printf("参加运动会的学校个数:");
scanf("%d", &n);
getchar();
printf("运动会的男子项目个数:");
scanf("%d", &m);
getchar();
printf("运动会的女子项目个数:");
scanf("%d", &w);
getchar();
A = (Event)malloc((w + m) * sizeof(Match));
B = (SchoolSheet)malloc(n * sizeof(School));
C = (All)malloc(n * sizeof(Team));
if (!A)
exit(1);
if (!B)
exit(1);
if (!C)
exit(1);
return;
}
void Clear()
{
for (int i = 0; i < n; i++)
{
(B + i)->num = 0;
(B + i)->ptr = 0;
(B + i)->RecordList = NULL;
(C + i)->SchoolCode = 0;
(C + i)->ManTotal = 0;
(C + i)->WomanTotal = 0;
(C + i)->TeamTotal = 0;
}
for (int j = 0; j < (m + w); j++)
{
(A + j)->Top = 0;
(A + j)->MatchCode = 0;
(A + j)->RankList = NULL;
}
return;
}
void Input()
{
int num = 0;
for (int i = 0; i < (m + w); i++)
{
(A + i)->MatchCode = i + 1;
printf("请输入取前几名(int),以及项目名称(char)");
scanf("%d %s", &(A + i)->Top, (A + i)->MatchName);
getchar();
num = (A + i)->Top;
(A + i)->RankList = (Score *)malloc(num * sizeof(Score));
Score *R = (A + i)->RankList;
for (int j = 0; j < num; j++)
{
(R + j)->Rank = j + 1;
printf("请输入运动员名字");
scanf("%s", (R + j)->AthleteName);
printf("请输入学校名称");
scanf("%s", (R + j)->SchoolName);
printf("请输入学校编号");
scanf("%d", &(R + j)->SchoolCode);
(B + (R + j)->SchoolCode - 1)->num++;
printf("请输入名次");
scanf("%s", (R + j)->Achievement);
}
}
return;
}
void SchoolScoreSheet()
{
for (int j = 0; j < n; j++)
{
int c = (B + j)->num;
(B + j)->RecordList = (Record *)malloc(c * sizeof(Record));
}
int num = 0;
for (int i = 0; i < (m + w); i++)
{
Score *R = (A + i)->RankList;
num = (A + i)->Top;
for (int j = 0; j < num; j++)
{
int k = 0, h = 0;
k = (R + j)->SchoolCode - 1;
strcpy((B + k)->SchoolName, (R + j)->SchoolName);
h = (B + k)->ptr;
Record *L = (B + k)->RecordList;
(L + h)->MatchCode = i + 1;
(L + h)->Rank = (R + j)->Rank;
strcpy((L + h)->MatchName, (A + i)->MatchName);
strcpy((L + h)->AthleteName, (R + j)->AthleteName);
strcpy((L + h)->Achievement, (R + j)->Achievement);
if ((A + i)->Top != 3)
(L + h)->GetScore = ScoreList[(R + j)->Rank + 2];
else
(L + h)->GetScore = ScoreList[(R + j)->Rank - 1];
(B + k)->ptr++;
}
}
return;
}
void TeamScoreSheet()
{
for (int i = 0; i < n; i++)
{
(C + i)->SchoolCode = i + 1;
strcpy((C + i)->SchoolName, (B + i)->SchoolName);
Record *L = (B + i)->RecordList;
for (int j = 0; j < (B + i)->num; j++)
{
if ((L + j)->MatchCode <= m)
(C + i)->ManTotal = (C + i)->ManTotal + (L + j)->GetScore;
else
(C + i)->WomanTotal = (C + i)->WomanTotal + (L + j)->GetScore;
}
(C + i)->TeamTotal = (C + i)->ManTotal + (C + i)->WomanTotal;
}
return;
}
void Show()
{
for (int i = 0; i < n; i++)
{
printf("
%s的成绩单:
", (B + i)->SchoolName);
printf("项目名 姓名 名次 得分
");
int k = (B + i)->num;
Record *L = (B + i)->RecordList;
for (int j = 0; j < k; j++)
{
printf("%s %s %d %d
", (L + j)->MatchName, (L + j)->AthleteName, (L + j)->Rank, (L + j)->GetScore);
}
}
printf("团体总分报表:
");
printf("校号 学校名称 男子团体总分 女子团体总分 团体总分
");
for (int i = 0; i < n; i++)
{
printf("%d %-20s%d %d %d
", (C + i)->SchoolCode, (C + i)->SchoolName, (C + i)->ManTotal, (C + i)->WomanTotal, (C + i)->TeamTotal);
}
return;
}
void logoff() //退出系统
{
printf("退出
");
exit(0);
}
void Home()
{
Function();
while (1)
{
printf("请输入选择的功能:");
scanf("%d", &t);
getchar();
switch (t)
{
case 1:
login();//初始化
break;
case 2:
Clear();//清零
break;
case 3:
Input();//录入各项比赛成绩
break;
case 4:
SchoolScoreSheet();//产生学校成绩单
break;
case 5:
TeamScoreSheet();//产生团体总分报表
break;
case 6:
Show();//结果显示
break;
case 7:
Home();//返回主界面
break;
case 0:
logoff();//退出系统
break;
}
}
return;
}
int main()
{
Start();//打印欢迎菜单
Home();//打印选项菜单
return 0;
}