实现需求
随意输入n个年级,输入完年级之后;可以随意输入n个班级数; 输入完班级之后,可以输入人数和科目成绩 ,最后找出总分的最高分,最低分,算出平均成绩。
目的
为了更好的把链表,指针,结构体等知识点更好的运用
代码如下
#include <stdio.h>
#include <stdlib.h>
void topic()
{
printf("=======================================================================\n");
printf("链表A,每个节点存放一个新的链表B1,B2,B3,B4,B5的头结点。\n");
printf("场景:一个年级,相当链表A该年级5个班,每个班5个人。\n");
printf("相当于链表B1–B5做一个学生成绩管理系统学生成绩有语文 数学 英语。\n");
printf("录入成绩,找出三科总分的最高分,最低分,算出平均成绩。\n");
printf("=======================================================================\n\n");
}
void welcome()
{
puts("*************************************************************************\n");
puts("欢迎使用学生管理系统");
puts("\n***********************************************************************\n");
}
struct Class
{
char name[32];
struct Class *next;
struct Student *studentHead;
};
struct Student
{
char name[32];
int yingyu;
int yuwen;
int shuxue;
int sum;
struct Student *next;
};
struct Class *classHead = NULL;
struct Student *stuHead = NULL;
void init(int *pclassCount, int *studentCount)
{
void (*ptopic)() = topic;
(*ptopic)();
void (*pwelcome)() = welcome;
(*pwelcome)();
puts("请输入班级数");
scanf("%d",pclassCount);
puts("请输入班级学生人数");
scanf("%d",studentCount);
}
// 遍历链表
void printLink()
{
struct Class *p = classHead;
struct Student *p2 = stuHead;
while(p != NULL){
p2 = p->studentHead;
while(p2 != NULL){
printf("====================================================\n");
printf("%s班的%s语文成绩为%d分\n",p->name,p2->name,p2->yuwen);
printf("%s班的%s数学成绩为%d分\n",p->name,p2->name,p2->shuxue);
printf("%s班的%s英语成绩为%d分\n",p->name,p2->name,p2->yingyu);
printf("%s班的%s总成绩为%d分\n",p->name,p2->name,p2->sum);
printf("====================================================\n\n");
p2 = p2->next;
}
p = p->next;
}
}
// 创建学生节点
struct Student* createStudentNode(struct Student *new)
{
struct Student *p = stuHead;
if(p == NULL){
p = new;
return p;
}
while(p->next != NULL){
p = p->next;
}
p->next = new;
return stuHead;
}
// 登记学生成绩
struct Student* registerStudentScore(int studentCount)
{
int i;
struct Student *new = NULL;
for(i = 0; i < studentCount; i++){
new = (struct Student *)malloc(sizeof(struct Student));
puts("请输入学生姓名");
scanf("%s",&(new->name));
puts("请输入英语成绩");
scanf("%d",&(new->yingyu));
puts("请输入语文成绩");
scanf("%d",&(new->yuwen));
puts("请输入数学成绩");
scanf("%d",&(new->shuxue));
new->next = NULL;
new->sum = new->yingyu + new->yuwen + new->shuxue;
// 创建学生节点
stuHead = createStudentNode(new);
}
return stuHead;
}
// 创建班级节点
struct Class* createClassNode(struct Class *new)
{
struct Class * p = classHead;
if(p == NULL){
p = new;
return p;
}
while(p->next != NULL){
p = p->next;
}
p->next = new;
return classHead;
}
// 初始化班级
struct Class* initClass(int classCount, int studentCount)
{
int i;
struct Class *new = NULL;
for(i = 0; i < classCount; i++){
new = (struct Class *)malloc(sizeof(struct Class));
puts("请输入班级名称!");
scanf("%s",&(new->name));
new->next = NULL;
// 创建班级节点
classHead = createClassNode(new);
// 登记学生成绩
new->studentHead = registerStudentScore(studentCount);
}
//return classHead;
}
// 最高分
int getMax()
{
int max;
struct Class *p = classHead;
struct Student *p2 = NULL;
max = p->studentHead->sum;
while(p != NULL){
p2 = p->studentHead;
while(p2 != NULL){
if(p2->sum > max){
max = p2->sum;
}
p2 = p2->next;
}
p = p->next;
}
return max;
}
int getMin()
{
int min;
struct Class *p = classHead;
struct Student *p2 = NULL;
min = p->studentHead->sum;
while(p != NULL){
p2 = p->studentHead;
while(p2 != NULL){
if(p2->sum < min){
min = p2->sum;
}
p2 = p2->next;
}
p = p->next;
}
return min;
}
float getAver(int classCount, int studentCount){
float aver;
int sum = 0;
struct Class *p = classHead;
struct Student *p2 = NULL;
while(p != NULL){
p2 = p->studentHead;
while(p2 != NULL){
sum = sum + p2->sum;
p2 = p2->next;
}
p = p->next;
}
aver = sum / (float)(classCount * studentCount);
return aver;
}
int main()
{
int classCount;
int studentCount;
int max,min;
float aver;
init(&classCount, &studentCount);
// 初始化班级
initClass(classCount, studentCount);
printLink();
max = getMax();
min = getMin();
aver = getAver(classCount, studentCount);
printf("总分最高的是:%d\n总分最低的是:%d\n总分平均分是:%2.f\n",max ,min, aver);
return 0;
}