一个年级,相当链表A,该年级5个班,每个班5个人,相当于链表B1–B5
做一个学生成绩管理系统
#include <stdio.h>
#include <stdlib.h>
struct Student
{
char name[32];
int Chinese;
int math;
int English;
int sum;
struct Student *next;
};
struct Class
{
struct Student *head;
struct Class *next;
};
//插入学生节点
struct Student *insertStudentHead(struct Student *head, struct Student *new)
{
struct Student *point = head;
if (head == NULL)
{
head = new;
return new;
}
while (point->next != NULL)
{
point = point->next;
}
point->next = new;
return head;
}
//学生链表
struct Student *createStudnetLink(struct Student *head, int classnum, int stunum)
{
struct Student *new = NULL;
int i;
for (i = 0; i < stunum; i++)
{
new = (struct Student *)malloc(sizeof(struct Student));
new->next = NULL;
printf("请输入%d班的第%d个学生的学生名\n", classnum + 1, i + 1);
scanf("%s", new->name);
printf("请输入%d班的第%d个学生的语文成绩\n", classnum + 1, i + 1);
scanf("%d", &(new->Chinese));
printf("请输入%d班的第%d个学生的数学成绩\n", classnum + 1, i + 1);
scanf("%d", &(new->math));
printf("请输入%d班的第%d个学生的英语成绩\n", classnum + 1, i + 1);
scanf("%d", &(new->English));
new->sum = new->Chinese + new->math + new->English;
head = insertStudentHead(head, new);
}
return head;
}
//插入班级节点
struct Class *insertClassHead(struct Class *head, struct Class *new)
{
struct Class *point = head;
if (head == NULL)
{
head = new;
return new;
}
while (point->next != NULL)
{
point = point->next;
}
point->next = new;
return head;
}
//创建班级节点
struct Class *createClassLink(struct Class *head, int classnum, int stunum)
{
struct Class *new = NULL;
struct Student *stuhead = NULL;
int i;
for (i = 0; i < classnum; i++)
{
new = (struct Class *)malloc(sizeof(struct Class));
new->next = NULL;
new->head = createStudnetLink(stuhead, i, stunum);
head = insertClassHead(head, new);
}
return head;
}
//最高分
int getMax(struct Class *head)
{
struct Class *p1 = head;
struct Student *p2 = NULL;
int max = 0;
while (p1 != NULL)
{
p2 = p1->head;
while (p2 != NULL)
{
if (max < p2->sum)
{
max = p2->sum;
}
p2 = p2->next;
}
p1 = p1->next;
}
return max;
}
//最低分
int getMin(struct Class *head)
{
struct Class *p1 = head;
struct Student *p2 = NULL;
int min = 10000;
while (p1 != NULL)
{
p2 = p1->head;
while (p2 != NULL)
{
if (min > p2->sum)
{
min = p2->sum;
}
p2 = p2->next;
}
p1 = p1->next;
}
return min;
}
//平均分
float averAge(struct Class *head, int classnum, int stunum)
{
struct Class *p1 = head;
struct Student *p2 = NULL;
int sum = 0;
while (p1 != NULL)
{
p2 = p1->head;
while (p2 != NULL)
{
sum += p2->sum;
p2 = p2->next;
}
p1 = p1->next;
}
return (float)sum / (classnum * stunum);
}
//打印学生信息
void printClassStudent(struct Class *head)
{
struct Class *p1 = head;
struct Student *p2 = NULL;
while (p1 != NULL)
{
p2 = p1->head;
while (p2 != NULL)
{
printf("学生的学生名是%s\n", p2->name);
printf("学生的语文成绩是:%d\n", p2->Chinese);
printf("学生的数学成绩是:%d\n", p2->math);
printf("学生的英语成绩是:%d\n", p2->English);
printf("学生的总分是%d\n", p2->sum);
p2 = p2->next;
}
p1 = p1->next;
}
}
int main()
{
int a;
int b;
int max = 0;
int min=0;
float aver=0;
struct Class *head = NULL;
printf("请输入班级数\n");
scanf("%d", &a);
printf("请输入学生数\n");
scanf("%d", &b);
head = createClassLink(head, a, b);
printClassStudent(head);
max = getMax(head);
printf("max=%d\n", max);
min=getMin(head);
printf("min=%d\n",min);
aver=averAge(head,a,b);
printf("aver=%.2f\n",aver);
return 0;
}