小项目需求:
链表A,每个节点存放一个新的链表B1,B2,B3,B4,B5的头结点。
场景:一个年级,相当链表A
该年级5个班,每个班5个人,相当于链表B1--B5
做一个学生成绩管理系统
学生成绩有语文 数学 英语
功能: 录入成绩 找三科总分的最高分 最低分 算出平均分
需求分析:
重点是通过这个小项目,进一步熟练链表和结构体,其个人遇到的主要难点是:班级链表A与学生链表B的连接
解决难点思路:
在定义的班级的结构体中,再定义一个指向学生的结构体的指针。
我这里只做到了两个班级,每个班级两个人,足够了,要不然在调试的时候调的腰疼。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Class
{
struct Student *stu;
struct Class *next;
};
struct Student
{
char name[128];
int yu;
int shu;
int ying;
int score; //总分
struct Student *next;
};
void printMessage(struct Class *head) //3.打印信息
{
int i = 1;
struct Class *p = head;
struct Student *stu = NULL;
while(p != NULL){
printf("\n++++++++++第%d个班的学生成绩:++++++++++\n",i);
stu = p->stu;
while(stu != NULL){
printf("名字:%s,语文:%d,数学:%d,英语:%d,总分:%d",stu->name,stu->yu,stu->shu,stu->ying,stu->score);
stu = stu->next;
putchar('\n');
}
p = p->next;
i = i+1;
}
putchar('\n');
}
struct Student *stu_weiCha(struct Student *head,struct Student *new)
{
struct Student *p = head;
if(head == NULL){
head = new;
return head;
}
while(p != NULL){
if(p->next == NULL){
p->next = new;
return head;
}
p = p->next;
}
}
struct Student *initStudent() //2.初始化学生,录入学生成绩
{
int i;
struct Student *head = NULL;
struct Student *new = NULL;
for(i=0;i<2;i++){
new = (struct Student *)malloc(sizeof(struct Student));
printf("请输入第%d个学生的名字: ",i+1);
scanf("%s",new->name);
printf("请输入语文成绩: ");
scanf("%d",&new->yu);
printf("请输入数学成绩: ");
scanf("%d",&new->shu);
printf("请输入英语成绩: ");
scanf("%d",&new->ying);
new->score = new->yu+new->shu+new->ying; //一个学生语数英三科总分
head = stu_weiCha(head,new);
}
return head;
}
struct Class *weiCha(struct Class *head,struct Class *new)
{
struct Class *p = head;
if(head == NULL){
head = new;
return head;
}
while(p != NULL){
if(p->next == NULL){
p->next = new;
return head;
}
p = p->next;
}
}
struct Class *initClass(struct Class *head) //1.初始化班级
{
int i;
struct Class *p = head;
struct Class *new = NULL;
for(i=0;i<2;i++){
new = (struct Class *)malloc(sizeof(struct Class));
printf("===========请输入第%d个班的学生成绩===========\n",i+1);
new->stu = initStudent();
head = weiCha(head,new);
}
return head;
}
void findMinn(struct Class *head) //4.总分最低分
{
struct Class *p = head;
struct Student *st = NULL;
struct Student *min = NULL;
min = (struct Student *)malloc(sizeof(struct Student));
min = p->stu;
while(p != NULL){
st = p->stu;
while(st != NULL){
if(min->score > st->score){
min = st;
}
st = st->next;
}
p = p->next;
}
printf("\n最低分的学生是:%s,总分:%d\n",min->name,min->score);
}
void findMax(struct Class *head) //5.总分最高分
{
struct Class *p = head;
struct Student *stu = NULL;
struct Student *max = NULL;
max = (struct Student *)malloc(sizeof(struct Student));
max->score = 0;
while(p != NULL){
stu = p->stu;
while(stu != NULL){
if(max->score < stu->score){
max = stu;
}
stu = stu->next;
}
p = p->next;
}
printf("\n最高分的学生是:%s,总分:%d\n",max->name,max->score);
}
void getAverage(struct Class *head) //6.平均分
{
int i = 0;
int zongfen = 0;
struct Class *p = head;
while(p != NULL){
i += 1;
while(p->stu != NULL){
zongfen = zongfen + p->stu->score;
p->stu = p->stu->next;
}
printf("\n第%d个班的平均分是:%f\n",i,(float)zongfen/2); //班级平均分
p = p->next;
}
putchar('\n');
}
int main()
{
struct Class *head = NULL;
head = initClass(head); //1.初始化班级和学生,也就是录入成绩
printMessage(head); //2.打印信息
findMinn(head); //3.总分最低分
findMax(head); //4.总分最高分
getAverage(head); //5.平均分
return 0;
}