用双链表实现的学生成绩管理系统
题目要求:
链表A,每个节点存放一个新的链表B1,B2,B3,B4,B5的头结点。
场景:一个年级,相当链表A,该年级5个班,每个班5个人,相当于链表B1–B5,做一个学生成绩管理系统
学生成绩有语文 数学 英语
功能: 录入成绩 找最三科总分的最高分 最低分 算出平均分
#include<stdlib.h>
#include<stdio.h>
#include <string.h>
struct student
{
char * name;
int xuehao;
float English;
float Chinese;
float math;
float aver;
float sum;
struct student * next;
};//定义存放学生信息的链表
struct class
{
int classnum;
struct student * head;//存放每个班第一个学生信息链表的头节点
struct class * next;
};//定义存放每个班信息的链表
用尾插法插入学生信息链表节点
struct student * insertstudent(struct student *head,struct student *new, int i)//这里的int i在第一次调用时为0
{
head=(struct student *)malloc(sizeof(struct student));
struct student*p=head;
if(i==0){
head=new;
return head;
}//当i=0时即第一次调用此函数时确保插入了头节点
while(p->next!=NULL){
p=p->next;
}
p->next=new;
return head;
}
输入学生信息构建学生信息链表
struct student *buildstudent(struct student *head,int num,int totalstudent)
{
int i;
int stnum=totalstudent;
struct student * new;
char * getname;
for(i=0;i<totalstudent;i++){
new=(struct student*)malloc(sizeof(struct student));
new->name=(char *)malloc(128);//学生信息链表中的char * name 开辟空间
getname=(char*)malloc(128);//对getname开辟空间,否则会出现段错误
printf("input %d class %d student name:\n",num,i+1);
scanf("%s",getname);
new->name=getname;
printf("input %d class %d student xuehao\n",num,i+1);
scanf("%d",&(new->xuehao));
printf("input %d class %d student Engilsh score\n",num,i+1);
scanf("%f",&(new->English));
printf("input %d class %d student Chinese score\n",num,i+1);
scanf("%f",&(new->Chinese));
printf("input %d class %d student math score\n",num,i+1);
scanf("%f",&(new->math));
stnum--;
new->sum=new->English+new->Chinese+new->math;
new->aver=new->sum/3;
head=insertstudent(head,new,i);//第一次调用时i=0
}
return head;
}
尾插法插入班级链表节点
struct class *insertclass(struct class* classhead,struct class*new,int i)
{
struct class*p=classhead;
if(i==0){
classhead=new;
return classhead;
}
while(p->next!=NULL){
p=p->next;
}
p->next=new;
return classhead;
}
构建班级链表的节点
struct class * buildclass(struct class *classhead,int totalclass,int totalstudent)
{
struct class * new=NULL;
struct student *stuhead;
int i;
int num=totalclass;
for(i=0;i<totalclass;i++){
new=(struct class*)malloc(sizeof(struct class));
new->classnum=i+1;
stuhead=buildstudent(stuhead,i+1,totalstudent);
new->head=stuhead;
classhead=insertclass(classhead,new,i);
num--;
}
return classhead;
}
定义一个打印学生信息的函数
void printmessage(struct class *classhead,int totalclass,int totalstudent)
{ int i;
int j;
struct class *p=classhead;
struct student *sp=NULL;
for(i=0;i<totalclass;i++){
sp=p->head;
for(j=0;j<totalstudent;j++){
printf("%d class %d student message:\n",p->classnum,j+1);
printf("******************************************************\n");
printf("%d class %d student name %s\n",p->classnum,j+1,sp->name);
printf("%d class %s xuehao %d\n",p->classnum,sp->name,sp->xuehao);
printf("%d class %s Chinese %2.f\n",p->classnum,sp->name,sp->Chinese);
printf("%d class %s Engilsh %2.f\n",p->classnum,sp->name,sp->English);
printf("%d class %s math %2.f\n",p->classnum,sp->name,sp->math);
printf("%d class %s sum %2.f\n",p->classnum,sp->name,sp->sum);
printf("%d class %s average %2.f\n",p->classnum,sp->name,sp->aver);
printf("******************************************************\n");
sp=sp->next;
}
p=p->next;
}
}
定义找最三科总分的最高分 最低分 算出平均分的函数
float getMax(struct class*classhead,int totalclass ,int totalstudent)
{
struct class *p=classhead;
struct student * sp;
int i,j;
float maxsum=p->head->sum;
for(i=0;i<totalclass;i++){
sp=p->head;
for(j=0;j<totalstudent;j++){
if(maxsum<sp->sum){
maxsum=sp->sum;
}
sp=sp->next;
}
p=p->next;
}
return maxsum;
}
float getMin(struct class*classhead,int totalclass ,int totalstudent)
{
struct class *p=classhead;
struct student * sp;
int i,j;
float minsum=p->head->sum;
for(i=0;i<totalclass;i++){
sp=p->head;
for(j=0;j<totalstudent;j++){
if(minsum>sp->sum){
minsum=sp->sum;
}
sp=sp->next;
}
p=p->next;
}
return minsum;
}
float getsumaver(struct class * classhead,int totalclass,int totalstudent)
{
struct class *p=classhead;
struct student *sp;
int i,j;
float sumaver;
float total=0;
for(i=0;i<totalclass;i++){
sp=p->head;
for(j=0;j<totalstudent;j++){
total=total+sp->sum;
sp=sp->next;
}
p=p->next;
}
printf("%f total\n",total);
sumaver=total/(totalclass*totalstudent);
printf("The sumaverage is %f\n",sumaver);
return sumaver;
}
int main()
{
int nclass,nstu;
struct class *chead;
float maxsum,minsum;
chead=(struct class*)malloc(sizeof(struct class));
printf("input total classes\n");
scanf("%d",&nclass);
printf("input total student\n");
scanf("%d",&nstu);
chead=buildclass(chead,nclass,nstu);
printmessage(chead,nclass,nstu);
maxsum=getMax(chead,nclass,nstu);
minsum=getMin(chead,nclass,nstu);
printf("The max sum is %2.f\n",maxsum);
printf("The min sum is %2.f\n",minsum);
getsumaver(chead,nclass,nstu);
return 0;
}