运动会管理系统学术工程实践-数据结构-链表显示功能

目录

一、运行环境说明

二、主题题目要求

三、流程图

四、代码

 五、代码分析

六、界面实现

 七、总结


一、运行环境说明

操作系统:   Windows系列

编译软件:   Visual Studio Code

开发语言:   C语言

二、主题题目要求

输出全部运动员信息、比赛项目信息、参赛成绩信息。把运动员信息、比赛项目信息、参赛成绩信息全部显示出来。

运动员基本信息包括参赛编号、姓名、性别、年龄、所属学院、备注等信息。运动员基本信息存入文件“athlete.dat” 中。

比赛项目信息包括比赛项目编号、名称、项目性别、比赛时间、比赛地点、赛况等信息。比赛项目基本信息存入文件“match.dat” 中。

参赛成绩录入信息包括运动员编号、比赛项目编号、成绩、名次、是否参赛。将参赛成绩信息存入文件“score.dat” 中。

三、流程图

显示功能函数功能示意图

四、代码

 #include <stdio.h>
#include <stdlib.h>//exit
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PASSWORD_LENGTH 20
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
 
struct athleteData
{
	int athNum;//运动员编号 
	char athName[10];//姓名
	char athsex[10];//性别 
	int athage;//年龄
	char athcollege[10];//学院
	char athremark[10];//备注 
	
};
struct Linkathlete
{
	int athNum;//运动员编号 
	char athName[10];//姓名
	char athsex[10];//性别 
	int athage;//年龄
	char athcollege[10];//学院
	char athremark[10];//备注 
	struct Linkathlete *next;
};

struct matchData
{
	int matNum;//比赛项目编号
	char matName[10];//名称
	char matsex[10];//项目性别
	char mattime[10];//比赛时间
	char matplace[10];//比赛地点
	char matresult[10];//赛况 
};
struct Linkmatch
{
	int matNum;//比赛项目编号
	char matName[10];//名称
	char matsex[10];//项目性别
	char mattime[10];//比赛时间
	char matplace[10];//比赛地点
	char matresult[10];//赛况 
	struct Linkmatch *next;
};
struct scoreData
{
	int scoathlete;//运动员编号 
	int scomatch;//比赛项目编号
	char scocollege[10];//学院
	double scograde;//成绩 
	int scorank;//名次
};
struct Linkscore
{
	int scoathlete;//运动员编号 
	int scomatch;//比赛项目编号
	char scocollege[10];//学院
	double scograde;//成绩 
	int scorank;//名次
	struct Linkscore *next;
};
/*链表*/
struct Linkathlete *creadathleteList()
{
	FILE *ptr;
	struct  athleteData athlete={0,"","",0,"",""};
	struct Linkathlete *head,*p,*q;//创建结构体指针 
	if((ptr=fopen("athlete.dat","rb"))==NULL)
	{
		printf("不能打开文件 \n");
		exit(0);
	}

	head = NULL;//创建链表的常规操作,表示头结点开始为空结点
	p = NULL;//创建链表的常规操作,  当前读取结点
	q = NULL;//创建链表的常规操作,当前读取备份结点
	while(fread(&athlete,sizeof(athlete),1,ptr)!=0)	//从文件中读信息存储在athlete中 
	{
		p=(struct Linkathlete *)malloc(sizeof(struct Linkathlete));//
		p->athNum=athlete.athNum;
		strcpy(p->athName,athlete.athName);
		strcpy(p->athsex,athlete.athsex);
		p->athage=athlete.athage;
		strcpy(p->athcollege,athlete.athcollege);
		strcpy(p->athremark,athlete.athremark);
		p->next=NULL;
		if (head==NULL) //用于判断头结点是否为空 
		{
			head=p;//head指向头结点 
		}
		else
		{
			q->next=p;
		} 
		q=p;
	} 
	fclose(ptr);//关闭文件指针
	return head;//链表的头结点 
}


struct Linkmatch *creadmatchList()
{
	FILE *ptr;
	struct  matchData match={0,"","","","",""};
	struct Linkmatch *head,*p,*q;//创建结构体指针 
		if((ptr=fopen("match.dat","rb"))==NULL)
	{
		printf("不能打开文件 \n");
		exit(0);
	}
	head = NULL;//创建链表的常规操作,表示头结点开始为空结点
	p = NULL;//创建链表的常规操作,  当前读取结点
	q = NULL;//创建链表的常规操作,当前读取备份结点
	while(fread(&match,sizeof(match),1,ptr)!=0)	//从文件中读信息存储在score中 
	{
		p=(struct Linkmatch *)malloc(sizeof(struct Linkmatch));//申请内存空间
		p->matNum=match.matNum;
		strcpy(p->matName,match.matName);
		strcpy(p->matsex,match.matsex);
		strcpy(p->mattime,match.mattime);
		strcpy(p->matplace,match.matplace);
		strcpy(p->matresult,match.matresult);
		p->next=NULL;
		if (head==NULL) //用于判断头结点是否为空 
		{
			head=p;//head指向头结点 
		}
		else
		{
			q->next=p;
		} 
		q=p;
	}
	fclose(ptr);//关闭文件指针
	return head;//链表的头结点 
}

 struct Linkscore *creadscoreList()
{
	FILE *ptr;
	struct  scoreData score={0,0,"",0.0,0};
	struct Linkscore *head,*p,*q;//创建结构体指针 
		if((ptr=fopen("score.dat","rb"))==NULL)
	{
		printf("不能打开文件 \n");
		exit(0);
	}
	head = NULL;//创建链表的常规操作,表示头结点开始为空结点
	p = NULL;//创建链表的常规操作,  当前读取结点
	q = NULL;//创建链表的常规操作,当前读取备份结点
	while(fread(&score,sizeof(score),1,ptr)!=0)	//从文件中读信息存储在score中 
	{
		p=(struct Linkscore *)malloc(sizeof(struct Linkscore));//申请内存空间
		p->scoathlete=score.scoathlete;
		p->scomatch=score.scomatch;
		strcpy(p->scocollege,score.scocollege);
		p->scograde=score.scograde;
		p->scorank=score.scorank;
		p->next=NULL;
		if (head==NULL) //用于判断头结点是否为空 
		{
			head=p;//head指向头结点 
		}
		else
		{
			q->next=p;
		} 
		q=p;
	}
	fclose(ptr);//关闭文件指针
	return head;//链表的头结点 
}



/*显示信息*/ 
void displayathlete()
{
	int flag=0;//用于设置是否输出标题,0表示输出,1表示不输出 
		struct Linkathlete *head,*p;
	head = creadathleteList();
//	struct Linkathlete *p = head;
	p=head;
	if (p == NULL)
	{
		printf("无运动员信息\n");
		//return;
	}
	
	while(p != NULL)
	{
		if(flag==0)//第一次需要输出标题
		{
			printf("%-12s%-10s%-10s%-6s%-10s%-0s\n","运动员编号","姓名","性别","年龄","学院","备注"); 
		} 
		printf("%-12d%-10s%-10s%-6d%-10s%-10s\n",p->athNum,p->athName,p->athsex,p->athage,p->athcollege,p->athremark);
		flag=1;
		p = p->next;
	}
}


void displaymatch()
{
	int flag=0;//用于设置是否输出标题,0表示输出,1表示不输出 
	struct Linkmatch *head,*p,*q;
	head = creadmatchList();
//	struct Linkathlete *p = head;
	p=head;
	if (p == NULL)
	{
		printf("无比赛项目信息文件\n");
		//return;
	}
	
	while(p != NULL)
	{
		if(flag==0)//第一次需要输出标题
		{
				printf("%-12s%-14s%-10s%-10s%-10s%-0s\n","比赛项目编号","比赛项目名称","项目性别","比赛时间","比赛地点","赛况"); 
		} 
		printf("%-12d%-14s%-10s%-10s%-10s%-10s\n",p->matNum,p->matName,p->matsex,p->mattime,p->matplace,p->matresult);
		flag=1;
		p = p->next;
	}
}

void displayscore()
{
	int flag=0;//用于设置是否输出标题,0表示输出,1表示不输出 
	struct Linkscore *head,*p,*q;
	
	head = creadscoreList();
//	struct Linkathlete *p = head;
	p=head;
	if (p == NULL)
	{
		printf("无参赛成绩管理信息\n");
		//return;
	}
	
	while(p != NULL)
	{
		if(flag==0)//第一次需要输出标题
		{
			printf("%-12s%-14s%-10s%-13s%-10s\n","运动员编号","比赛项目编号","学院","成绩","名次"); 
		} 
		printf("%-12d%-14d%-10s%-13lf%-10d\n",p->scoathlete,p->scomatch,p->scocollege,p->scograde,p->scorank);
		flag=1;
		p = p->next;
	}
}


void displayinformation()
{

	displayathlete ();
	
	displaymatch ();
	
	displayscore (); 
} 

 五、代码分析

struct athleteData、struct matchData、struct scoreData定义运动员、比赛项目、参赛成绩的数据结构,后struct Linkathlete、struct Linkmatch、struct Linkscore定义运动员链表、比赛项目链表、参赛成绩链表的数据结构,以及下一个结点的指针。

函数creadathleteList()用于创建运动员链表,从文件"athlete.dat"中读取运动员数据,并将数据存储到链表中。

函数creadmatchList()用于创建比赛链表,从文件"match.dat"中读取比赛数据,并将数据存储到链表中。

函数creadscoreList()用于创建成绩链表,从文件"score.dat"中读取成绩数据,并将数据存储到链表中。

每个函数都会打开相应的文件,然后使用fread()函数从文件中读取数据,并将数据存储到相应的结构体指针中。然后根据链表的常规操作,将结点连接起来,最后返回链表的头结点。

displayathlete()、displaymatch()和displayscore(),以及一个主函数displayinformation()。这些函数用于显示运动员信息、比赛项目信息和成绩信息。

displayathlete()函数用于显示运动员信息。它首先创建一个运动员链表并将其赋值给头指针head,然后遍历链表,逐个输出运动员的编号、姓名、性别、年龄、学院和备注。

displaymatch()函数用于显示比赛项目信息。它首先创建一个比赛项目链表并将其赋值给头指针head,然后遍历链表,逐个输出比赛项目的编号、名称、项目性别、比赛时间、比赛地点和赛况。

displayscore()函数用于显示成绩信息。它首先创建一个成绩链表并将其赋值给头指针head,然后遍历链表,逐个输出运动员的编号、比赛项目的编号、学院、成绩和名次。

displayinformation()函数用于调用上述三个显示函数,以便一次性显示所有的运动员信息、比赛项目信息和成绩信息。

六、界面实现

 七、总结

本次实践,让我第一次亲自动手解决实际问题,对链表的显示功能有了更深的印象。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值