参加运动会的有n个学院,学校编号为1……n,比赛分成m个男子项目,和w个女子项目。项目编号为男子1……m,女子m+1……m+w。不同的项目取前八名积分,且前八名的积分分别为:9、7、6、5、4、3、2、1(m<=20,n<=20)。
功能要求:(1)可以输入各个项目的前八名的成绩;(2)能统计各学院的总分并排序;(3)可以按学院编号、学院总分、男女团体总分排序输出;(4)可以按学院编号查询学院某个项目的情况;可以按项目编号查询取得前八名的学院。(5)可以查找汇总某名选手参加的项目和获取的名次和积分。
界面要求:
有合理的提示,每个功能可以设立菜单,根据提示,可以完成相关的功能要求。
存储结构:
学生自己根据系统功能要求自己设计,要求运动会的相关数据要存储在数据文件中。(数据文件的数据读写方法等相关内容可以参考c语言程序设计的教材)请在最后的上交资料中指明使用的存储结构。
测试数据:
要求使用(1)全部合法数据;(2)整体非法数据;(3)局部非法数据。进行程序测试,以保证程序的稳定。测试数据及测试结果请在上交的资料中写明。
该代码使用Visual C++ 6.0 编译运行,如果使用其他版本可能输出scanf_s之类的错误
由于要求简单,没有删除等功能,没有设立尾指针
数据说明
数字 3 代表有三个学院
数字 2 代表有两个项目
字段 分别是 姓名,性别(0是女生,1是男生),学院,项目 ,名次 ,得分
运行截图:
下载地址
https://gitee.com/xiaoweidumpb/dscourese-design/tree/master/
头文件
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define N 10
typedef int ElemType;
typedef struct Node
{
ElemType score; //分数
ElemType rank; //排名
ElemType sex; //性别
char name[20]; //姓名
char college[20]; //学院
char item[20]; //项目
struct Node *next;
} LinkNode, *LinkList;
void Init(LinkList &Head);
int GetScore(int rank);
void Input(LinkList &Head, int &CollegeNum, int &ItemNum, char Colleges[][20], char Items[][20]);
void Create(LinkList &Head, int score, int rank, int sex, char name[], char college[], char item[]);
void Show(LinkList Head);
void Save(LinkList Head, char Colleges[][20], int CollegeNum, char Items[][20], int ItemNum);
void Read(LinkList Head, char Colleges[][20], int &CollegeNum, char Items[][20], int &ItemNum);
void Collegescore(LinkList Head, char Colleges[][20], int CollegeNum);
void SexScore(LinkList Head, char Colleges[][20], int CollegeNum);
void QueryCollege(LinkList Head);
void QueryName(LinkList Head);
void QueryItem(LinkList Head);
void Insert(LinkList &Head, int score, int rank, int sex, char name[], char college[], char item[]);
void Print(Node *p);
void Free(LinkList &Head);
main函数
#include "sports.h"
int main()
{
LinkList Head; //选手数据链表
Init(Head);
int CollegeNum=0; //学院数量
int ItemNum=0; //项目数量
char Colleges[N][20]={'\0'}; //学院
char Items[N][20]={'\0'}; //运动会项目
int select=0;
while (true)
{
printf("********************************************\n");
printf("[1]初始化程序 [2]读取数据 \n");
printf("[3]打印数据 [4]保存数据 \n");
printf("[5]统计学院的总分 [6]男女团体总分 \n");
printf("[7]查询学院项目 [8]查找选手 \n");
printf("[9]查询项目获奖 \n");
printf("[0]退出系统 \n");
printf("*******************************************\n");
printf("请选择: >\n");
scanf("%d", &select);
switch (select)
{
case 1:
{
Input(Head,CollegeNum,ItemNum,Colleges,Items);
break;
}
case 2:
Read(Head,Colleges,CollegeNum,Items,ItemNum);
Show(Head);
break;
case 3:
Show(Head);
break;
case 4:
Save(Head,Colleges,CollegeNum,Items,ItemNum);
break;
case 5:
Collegescore(Head,Colleges,CollegeNum);
break;
case 6:
SexScore(Head,Colleges,CollegeNum);
break;
case 7:
QueryCollege(Head);
break;
case 8:
QueryName(Head);
break;
case 9:
QueryItem(Head);
break;
case 0:
Free(Head);
return 0;
default:
printf("输入的数据有误,请重新输入\n");
break;
}
}
}
实现函数
#include "sports.h"
//初始化头指针
void Init(LinkList &Head)
{
Head = (LinkNode *)malloc(sizeof(LinkNode)); //申请头结点
Head->rank = 0;
Head->score = 0;
Head->sex = -1;
strcpy(Head->name, "");
strcpy(Head->college, "");
strcpy(Head->item, "");
Head->next = NULL;
}
//根据排名得到相应的分数
int GetScore(int rank)
{
if (rank == 1)
return 9;
else if (rank == 2)
return 7;
else if (rank == 3)
return 6;
else if (rank == 4)
return 5;
else if (rank == 5)
return 4;
else if (rank == 6)
return 3;
else if (rank == 7)
return 2;
else if (rank == 8)
return 1;
else
return 0;
}
//初始化程序,输入学院名,项目名和选手成绩
void Input(LinkList &Head, int &CollegeNum, int &ItemNum, char Colleges[][20], char Items[][20])
{
int i, r, flag = 0;
int score = 0, rank = 0, sex = 0; // 存储输入变量;
char name[20] = {'\0'}, college[20] = {'\0'}, item[20] = {'\0'}; // 存储输入变量存储;
int count = 1; // 计数器
printf("请输入学院个数 n<10 \n");
while (r = scanf("%d", &CollegeNum) != 1 || CollegeNum > 10 || CollegeNum < 0)
{
printf(" 您的输入有误,请重新输入! \n");
fflush(stdin); /*清空输入缓冲区,也可以使用rewind(stdin);*/
}
for (i = 0; i < CollegeNum; i++)
{
printf("请输入第%d学院名\n", i + 1);
scanf("%s", &Colleges[i]);
fflush(stdin);
}
printf("请输入项目个数 n<10 \n");
while (r = scanf("%d", &ItemNum) != 1 || ItemNum > 10 || ItemNum < 0)
{
printf(" 您的输入有误,请重新输入! \n");
fflush(stdin);
}
for (i = 0; i < ItemNum; i++)
{
printf("请输入第%d项目名\n", i + 1);
scanf("%s", &Items[i]);
}
while (true)
{
fflush(stdin);
printf("请输入第%d个选手数据\n", count);
printf("请输入姓名\n");
r = scanf("%s", &name);
if (r == 0)
{
printf(" 您的输入有误,请重新输入! \n");
break;
}
printf("请输入性别 0代表女性 1代表男性\n");
r = scanf("%d", &sex);
if (r == 0)
{
printf(" 您的输入有误,请重新输入! \n");
break;
}
printf("请输入学院\n");
r = scanf("%s", &college);
if (r == 0)
{
printf(" 您的输入有误,请重新输入! \n");
break;
}
printf("请输入项目\n");
r = scanf("%s", &item);
if (r == 0)
{
printf(" 您的输入有误,请重新输入! \n");
break;
}
printf("请输入名次 1-8\n");
r = scanf("%d", &rank);
if (r == 0 && rank < 0 && rank > 8)
{
printf(" 您的输入有误,请重新输入! \n");
break;
}
score = GetScore(rank);
Create(Head, score, rank, sex, name, college, item);
count++;
printf("0 结束输入数据 , 1 输入数据\n");
scanf("%d", &flag);
if (flag == 0)
break;
}
}
//创建链表
void Create(LinkList &Head, int score, int rank, int sex, char name[], char college[], char item[])
{
LinkNode *p = Head->next, *s;
//如果链表为空,则做初始化链表
if (p == NULL)
{
s = (LinkNode *)malloc(sizeof(LinkNode));
assert(s != NULL);
s->score = score;
s->rank = rank;
s->sex = sex;
strcpy(s->name, name);
strcpy(s->college, college);
strcpy(s->item, item);
s->next = NULL;
Head->next = s;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
s = (LinkNode *)malloc(sizeof(LinkNode));
assert(s != NULL);
s->score = score;
s->rank = rank;
s->sex = sex;
strcpy(s->name, name);
strcpy(s->college, college);
strcpy(s->item, item);
s->next = NULL;
p->next = s;
}
}
//打印所有的数据
void Show(LinkList Head)
{
int count = 1;
Node *p = Head->next;
while (p != NULL)
{
printf("打印第%d个成绩\n", count);
Print(p);
p = p->next;
count++;
}
}
//保存数据为.txt文件
void Save(LinkList Head, char Colleges[][20], int CollegesNum, char Items[][20], int ItemNum)
{
int i;
//保存选手数据
FILE *file;
file = fopen("sports_data.txt", "w");
if (file == NULL)
{
printf("文件创建失败请检查!\n");
exit(0);
}
Node *p = Head->next;
while (p != NULL)
{
fprintf(file, "%s\t", p->name);
fprintf(file, "%d\t", p->sex);
fprintf(file, "%s\t", p->college);
fprintf(file, "%s\t", p->item);
fprintf(file, "%d\t", p->rank);
fprintf(file, "%d\n", p->score);
p = p->next;
}
fclose(file);
file = NULL; //将指针置为NULL ,打开另一个文件
file = fopen("Colleges_Items.txt", "w");
if (file == NULL)
{
printf("文件创建失败请检查!\n");
exit(0);
}
//保存项目数和学院数
fprintf(file, "%d\t", CollegesNum);
fprintf(file, "%d\t", ItemNum);
//保存项目名字和学院名字
for (i = 0; i < CollegesNum; i++)
{
fprintf(file, "%s\t", Colleges[i]);
}
for (i = 0; i < ItemNum; i++)
{
fprintf(file, "%s\t", Items[i]);
}
fclose(file);
}
//读取txt文件中的数据
void Read(LinkList Head, char Colleges[][20], int &CollegesNum, char Items[][20], int &ItemNum)
{
if (Head->next != NULL)
{
printf("已读取数据!\n");
return;
}
FILE *file;
int i = 0;
file = fopen("sports_data.txt", "r");
if (file == NULL)
{
printf("文件读取失败请初始化!\n");
exit(0);
}
int score, rank, sex; // 作为输入变量存储;
char name[20], college[20], item[20]; // 作为输入变量存储;
//当指针不为结束符,一直++
//读入所有的选手成绩
while (!feof(file))
{
fscanf(file, "%s", &name);
fscanf(file, "%d\t", &sex);
fscanf(file, "%s\t", &college);
fscanf(file, "%s\t", &item);
fscanf(file, "%d\t", &rank);
fscanf(file, "%d\n", &score);
Create(Head, score, rank, sex, name, college, item);
}
fclose(file);
//读取学院名字和项目名字
file = NULL;
file = fopen("colleges_items.txt", "r");
if (file == NULL)
{
printf("文件读取失败请初始化!\n");
exit(0);
}
while (!feof(file))
{
fscanf(file, "%d\t", &CollegesNum);
fscanf(file, "%d\t", &ItemNum);
for (i = 0; i < CollegesNum; i++)
{
fscanf(file, "%s\t", &Colleges[i]);
}
for (i = 0; i < CollegesNum; i++)
{
fscanf(file, "%s\t", &Items[i]);
}
}
fclose(file);
}
//学院分数
void Collegescore(LinkList Head, char Colleges[][20], int CollegesNum)
{
int score[N] = {0}; //
int i;
LinkList list;
Node *p = NULL;
//计算每个学院的分数
Init(list);
for (i = 0; i < CollegesNum; i++)
{
p = Head->next;
while (p != NULL)
{
if (strcmp(Colleges[i], p->college) == 0)
{
score[i] += p->score;
}
p = p->next;
}
}
//按分数大小建立一个链表
for (i = 0; i < CollegesNum; i++)
{
Insert(list, score[i], 0, 0, "", Colleges[i], "");
}
//打印学院成绩
p = NULL;
p = list->next;
while (p != NULL)
{
printf("%s学院得分为%8d\n", p->college, p->score);
p = p->next;
}
Free(list);
}
//释放链表
void Free(LinkList &Head)
{
Node *p = NULL, *q = NULL;
p = Head->next;
//链表为空直接返回
if (p == NULL)
{
free(Head);
return;
}
while (p != NULL)
{
q = p;
p = p->next;
free(q);
}
free(Head);
}
//按男女统计得分
void SexScore(LinkList Head, char Colleges[][20], int CollegesNum)
{
LinkList man_score, woman_score;
Init(man_score);
Init(woman_score);
int i;
for (i = 0; i < CollegesNum; i++)
{
int a[N] = {0};
Node *p = Head->next;
LinkList man, womon;
Init(man);
Init(womon);
while (p != NULL)
{ //统计某学院男生,女生成绩
if (strcmp(Colleges[i], p->college) == 0 && p->sex == 0)
{
Insert(womon, p->score, p->rank, p->sex, p->name, p->college, p->item);
}
if (strcmp(Colleges[i], p->college) == 0 && p->sex == 1)
{
Insert(man, p->score, p->rank, p->sex, p->name, p->college, p->item);
}
p = p->next;
}
p = man->next;
while (p != NULL)
{
a[i] += p->score;
p = p->next;
}
Insert(man_score, a[i], 0, 0, "", Colleges[i], "");
p = NULL;
a[i] = 0;
p = womon->next;
while (p != NULL)
{
a[i] += p->score;
p = p->next;
}
Insert(woman_score, a[i], 0, 0, "", Colleges[i], "");
Free(man);
Free(womon);
}
Node *q;
q = man_score->next;
printf("学院男生团体的前八名为\n");
while (q != NULL)
{
printf("%s男生团体得分为: %d\n", q->college, q->score);
q = q->next;
}
q = NULL;
q = woman_score->next;
printf("学院女生团体的前八名为\n");
while (q != NULL)
{
printf("%s女生团体得分为: %d\n", q->college, q->score);
q = q->next;
}
Free(man_score);
Free(woman_score);
}
//查询某学院某个项目获奖情况
void QueryCollege(LinkList Head)
{
char item[20], college[20];
printf("请输入您要查询的学院名:\n");
scanf("%s", &college);
printf("请输入您要查询的项目名:\n");
scanf("%s", &item);
Node *p = Head->next;
while (p != NULL)
{
if (strcmp(college, p->college) == 0 && strcmp(item, p->item) == 0)
{
Print(p);
}
p = p->next;
}
}
//查询选手成绩
void QueryName(LinkList Head)
{
char name[20];
printf("请输入您要查询的选手姓名:\n");
scanf("%s", &name);
Node *p = Head->next;
while (p != NULL)
{
if (strcmp(name, p->name) == 0)
{
Print(p);
}
p = p->next;
}
}
// 按大小顺序插入建立一个链表
void Insert(LinkList &Head, int score, int rank, int sex, char name[], char college[], char item[])
{
LinkNode *p = Head->next, *s = NULL;
LinkNode *q = Head;
//如果链表为空,则做初始化链表
if (p == NULL)
{
s = (LinkNode *)malloc(sizeof(LinkNode));
assert(s != NULL);
s->score = score;
s->rank = rank;
s->sex = sex;
strcpy(s->name, name);
strcpy(s->college, college);
strcpy(s->item, item);
s->next = NULL;
Head->next = s;
}
else
{
while (p != NULL && score <= p->score)
{
q = p;
p = p->next;
}
s = (LinkNode *)malloc(sizeof(LinkNode));
assert(s != NULL);
s->score = score;
s->rank = rank;
s->sex = sex;
strcpy(s->name, name);
strcpy(s->college, college);
strcpy(s->item, item);
s->next = p;
q->next = s;
}
}
//查询项目得分情况
void QueryItem(LinkList Head)
{
char item[20];
LinkList t;
Init(t);
printf("请输入您要查询的项目名:\n");
scanf("%s", &item);
Node *p = Head->next;
while (p != NULL)
{
if (strcmp(item, p->item) == 0)
{
Insert(t, p->score, p->rank, p->sex, p->name, p->college, p->item);
}
p = p->next;
}
p = t->next;
printf("%s项目前八名为\n", item);
while (p != NULL)
{
Print(p);
p = p->next;
}
}
//打印指针内的数据
void Print(Node *p)
{
printf("\n\n\n\n");
printf("*************************\n");
printf("姓名为 ");
printf("%s\n", p->name);
printf("性别为 ");
printf("%d\n", p->sex);
printf("学院为 ");
printf("%s\n", p->college);
printf("项目为 ");
printf("%s\n", p->item);
printf("名次为 ");
printf("%d\n", p->rank);
printf("分数为 ");
printf("%d\n", p->score);
}