#include <stdio.h>
#include <stdlib.h>
//该成绩管理系统由双链表嵌套完成,可完成m个班级,每个班级n个学生的成绩管理,输出学生信息,输出最高分、最低分、平均分,m、n由输入获取
struct Class //班级结构体
{
char *Class;
struct Class *next;
struct Student *Stuhead;
};
struct Student //学生结构体
{
char *name;
int Chinese;
int Math;
int English;
int Sum;
struct Student *next;
};
struct Class *Chead = NULL; //定义全局变量,避免太多传参而出错
struct Student *Shead = NULL;
void printStudentMes() //输出学生信息
{
struct Class *p1;
p1 = Chead;
struct Student *p2 = NULL;
while(p1!=NULL){ //嵌套
p2 = p1->Stuhead;
while(p2!=NULL){
printf("%s班的%s语文成绩为%d分\n",p1->Class,p2->name,p2->Chinese);
printf("%s班的%s数学成绩为%d分\n",p1->Class,p2->name,p2->Math);
printf("%s班的%s英语成绩为%d分\n",p1->Class,p2->name,p2->English);
printf("%s班的%s总成绩为%d分\n",p1->Class,p2->name,p2->Sum);
putchar('\n');
p2 = p2->next;
}
p1 = p1->next;
}
}
struct Student* AddStudentNode(struct Student *head,struct Student *new) //尾插法创建学生链表
{
struct Student *p = head;
if(head==NULL){
head = new;
return head;
}
while(p->next!=NULL){
p=p->next;
}
p->next = new;
return head;
}
struct Student *ScanfStudent(struct Student *head,int Students) //输入学生信息
{
int j;
for(j=0;j<Students;j++){
struct Student *new = (struct Student*)malloc(sizeof(struct Student));
new -> name = (struct Student *)malloc(sizeof(struct Student));
new -> next = NULL;
printf("请输入学生姓名:\n");
scanf("%s",new->name);
printf("请输入学生语文成绩:\n");
scanf("%d",&(new->Chinese));
printf("请输入学生数学成绩:\n");
scanf("%d",&(new->Math));
printf("请输入学生英语成绩:\n");
scanf("%d",&(new->English));
new->Sum = new->Chinese + new->Math + new->English;
head = AddStudentNode(head,new);
}
return head;
}
struct Class* AddClassNode(struct Student *head,struct Student *new) //增加班级节点
{
struct Class *p = Chead;
if(Chead==NULL){
Chead = new;
return Chead;
}
while(p->next!=NULL){
p=p->next;
}
p->next = new;
return Chead;
}
struct Class *CreatClass(int Classes,int Students) //创建班级链表
{
int i;
for(i=0;i<Classes;i++){
struct Class *new;
new = (struct Class*)malloc(sizeof(struct Class));
new->Class = (struct Class*)malloc(sizeof(struct Class));
new -> next = NULL;
new -> Stuhead = NULL;
printf("请输入班级:\n");
scanf("%s",new->Class);
new -> Stuhead = ScanfStudent(Shead,Students);
Chead = AddClassNode(Chead,new);
}
return Chead;
}
int GetSumMax() //求成绩最大值
{
int Max;
struct Class *p1;
p1 = Chead;
struct Student *p2 = NULL;
Max = p1-> Stuhead -> Sum;
while(p1!=NULL){
p2 = p1->Stuhead;
while(p2!=NULL){
if(p2->Sum>Max){
Max = p2->Sum;
}
p2 = p2->next;
}
p1 = p1->next;
}
return Max;
}
int GetSumMin() //求成绩最小值
{
int Min;
struct Class *p1;
p1 = Chead;
struct Student *p2 = NULL;
Min = p1-> Stuhead -> Sum;
while(p1!=NULL){
p2 = p1->Stuhead;
while(p2!=NULL){
if(p2->Sum<Min){
Min = p2->Sum;
}
p2 = p2->next;
}
p1 = p1->next;
}
return Min;
}
float GetSumAverage(int m,int n) //注意传参 m为班级,n为学生人数
{
float total = 0;
struct Class *p1;
p1 = Chead;
struct Student *p2 = NULL;
while(p1!=NULL){
p2 = p1->Stuhead;
while(p2!=NULL){
total += p2->Sum;
p2 = p2->next;
}
p1 = p1->next;
}
return total/(float)(m*n);
}
int main()
{
int m;
int n;
int Max;
int Min;
float Average;
printf("请输入总班级数:\n");
scanf("%d",&m);
printf("请输入班级人数:\n");
scanf("%d",&n);
CreatClass(m,n);
Max = GetSumMax();
Min = GetSumMin();
Average = GetSumAverage(m,n);
printStudentMes();
printf("总分最高的是%d\n总分最低的是:%d\总分平均分是:%f\n",Max ,Min,Average);
return 0;
}