题目要求:链表A,每个节点存放一个新的链表B1,B2,B3,B4,B5的头结点。
场景:一个年级,相当链表A
该年级5个班,每个班5个人,相当于链表B1–B5
做一个学生成绩管理系统
学生成绩有语文 数学 英语
功能: 录入成绩 找最三科总分的最高分 最低分 算出平均分
创建学生和班级结构体:
struct Student//创建学生结构体
{
int xuehao;
int chinese;
int math;
int english;
double sum;
double avge;
struct Student *next;
};
struct Class//创建班级结构体
{
struct Student *stuhead;//学生链表头
int classnum;
struct Class *next;
};
创建学生链表:
struct Student* insertStuMesage(struct Student *head,struct Student *new)//头插法插入
{
if(head == NULL)
{
head = new;
}
else
{
new->next = head;
head = new;
}
return head;
}
struct Student* creatStudent(struct Student *head,int stunum,int clanum)//动态创建学生链表
{
int i;
int num = stunum;
double sum;
double avge;
struct Student *new;
for(i=0;i<stunum;i++)
{
new = (struct Student*)malloc(sizeof(struct Student));
printf("请输入%d班第%d个学生的学号;\n",clanum,num);
scanf("%d",&(new->xuehao));
printf("请输入第%d个学生的语文成绩;\n",num);
scanf("%d",&(new->chinese));
printf("请输入第%d个学生的数学成绩;\n",num);
scanf("%d",&(new->math));
printf("请输入第%d个学生的英语成绩;\n",num);
scanf("%d",&(new->english));
sum = new->chinese+new->math+new->english;
new->sum =sum;
avge = sum/3;
new->avge = avge;
num--;
head = insertStuMesage(head,new);
}
return head;
}
创建班级链表:
struct Class* insertClaMesage(struct Class *head,struct Class *new)//头插法
{
if(head == NULL)
{
head = new;
}
else
{
new->next = head;
head = new;
}
return head;
}
struct Class* creatClass(struct Class *head,int clanum,int stunum)//动态创建班级链表
{
struct Class *new = NULL;
struct Student *stuHead;
int num = clanum;
int i;
for(i=0;i<clanum;i++)
{
new = (struct Class*)malloc(sizeof(struct Class));
new->classnum = num;
stuHead = creatStudent(stuHead,stunum,num);
new->stuhead = stuHead;
num--;
head = insertClaMesage(head,new);
}
return head;
}
打印成绩:
void printMessage(struct Class *head,int clanum,int stunum)//打印输入的成绩(这步可以没有,并不影响程序的运行)
{
struct Class *p1 = head;
struct Student *p2 =NULL;
int k = stunum;
int i=0;
while(i < clanum)
{
p2 = p1->stuhead;
while(p2 != NULL && k>0)
{
printf("%d班第%d个学生的学号:%d\n",p1->classnum,k,p2->xuehao);
printf("语文成绩为:%d\n",p2->chinese);
printf("数学成绩为:%d\n",p2->math);
printf("英语成绩为:%d\n",p2->english);
printf("总分为:%lf\n",p2->sum);
printf("平均分为为:%lf\n",p2->avge);
p2 = p2->next;
k--;
}
k = stunum;
p1= p1->next;
i++;
}
}
最高成绩,最小成绩,平均值:
int getMax(struct Class *head,int clanum,int stunum)//获取学生总分最大值
{
struct Class *p1 = head;
struct Student *p2=(struct Student *)malloc(sizeof(struct Student));
p2 = p1->stuhead;
int i=0;
int j=0;
int max=0;
while(i < clanum)
{
while(j < stunum*clanum)
{
if(max < p2->sum)
{
max = p2->sum;
}
p2 = p2->next;
j++;
}
p1 = p1->next;
i++;
}
printf("**************************\n");
printf("最高总分为:%d\n",max);
printf("**************************\n");
}
int getMin(struct Class *head,int clanum,int stunum)//获取学生总分最小值
{
struct Class *p1 = head;
struct Student *p2=(struct Student *)malloc(sizeof(struct Student));
int i=0;
int j=0;
int min=10000;
p2 = p1->stuhead;
while(i < clanum)
{
while(j < stunum*clanum)
{
if(min > p2->sum)
{
min = p2->sum;
}
p2 = p2->next;
j++;
}
p1 = p1->next;
i++;
}
printf("**************************\n");
printf("最低总分为:%d\n",min);
printf("**************************\n");
}
float getAvge(struct Class *head,int clanum,int stunum)//获取学生总分平均值
{
struct Class *p1 = head;
struct Student *p2=(struct Student *)malloc(sizeof(struct Student));
int i=0;
int j=0;
double sum=0;
double avge;
p2 = p1->stuhead;
while(i < clanum)
{
while(j < stunum*clanum)
{
sum = sum+p2->sum;
p2 = p2->next;
j++;
}
p1 = p1->next;
i++;
}
avge = sum/(stunum*clanum);
printf("**************************\n");
printf("年级平均分为:%lf\n",avge);
printf("**************************\n");
}
main函数调用:
int main()
{
printf("##########################\n");
printf("欢迎使用学生成绩管理系统\n");
printf("#########################\n");
int a ;//班级个数
int b ;//学生个数
int max=0;
struct Class *head;
printf("请输入班级个数\n");
scanf("%d",&a);
printf("请输入学生个数\n");
scanf("%d",&b);
head = creatClass(head,a,b);
printMessage(head,a,b);
getMax(head,a,b);
getMin(head,a,b);
getAvge(head,a,b);
system("pause");
return 0;
}