通过链表做学生成绩管理系统

小项目需求:

链表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;
}

 

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枕上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值