3名学生,每名学生有3门课成绩,输出平均分最高的学生信息, 要求利用动态链表存储学生信息。————函数升级版优化

//3名学生,每名学生有3门课成绩,输出平均分最高的学生信息,
//要求利用动态链表存储学生信息。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct student{
	char name[20];
	int num;
	int score[3];
	float ave;
	
};
struct Node{
	struct student stu;
	struct Node *next;//指向下一个地址的指针
};
//创建链表
struct Node* add(){
	struct Node *p1;
	struct Node *head=NULL;	
	struct Node *p2;
	int i;
	
	p1=p2=(struct Node*)malloc(sizeof(struct Node));
	printf("请输入学生信息,学号,3门成绩,姓名");
	scanf("%d%d%d%d%s",&p1->stu.num,&p1->stu.score[0],&p1->stu.score[1],&p1->stu.score[2],p1->stu.name);
	p1->stu.ave=(p1->stu.score[0]+p1->stu.score[1]+p1->stu.score[2])/3;
	printf("平均成绩为:%f\n",p1->stu.ave);
	p1->next=NULL;//保证安全
	p2=p1;
	head=p1;
	
	for(i=0;i<2;i++){
	p1=(struct Node*)malloc(sizeof(struct Node));
	printf("请输入学生信息,学号,3门成绩,姓名");
	scanf("%d%d%d%d%s",&p1->stu.num,&p1->stu.score[0],&p1->stu.score[1],&p1->stu.score[2],p1->stu.name);
	p1->stu.ave=(p1->stu.score[0]+p1->stu.score[1]+p1->stu.score[2])/3;
    printf("平均成绩为:%f\n",p1->stu.ave);
	p1->next=NULL;//保证安全
	p2->next=p1;
	p2=p1;
	}
	p2->next=NULL;
	return head;
}
//比较平均值
int change(struct Node* p){
	struct Node*f;
	f=p->next;
	do{ if((p->stu.ave)<(f->stu.ave))p=f;
		f=f->next;
	}while(f!=NULL);
	printf("**********平均成绩最大的学生信息***********\n");
    printf("姓名为:%s\n",p->stu.name);
    printf("学号为:%d\n",p->stu.num);
    printf("3门成绩为:%d %d %d\n",p->stu.score[0],p->stu.score[1],p->stu.score[2]); 
    printf("平均成绩为:%f",p->stu.ave);
	}
	

int main()
{
	struct Node *p2;
	struct Node *p;
	p=add();
	change(p);
	return 0;
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值