//3名学生,每名学生有3门课成绩,输出平均分最高的学生信息,
//要求利用动态链表存储学生信息。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct student{
char name[20];
int num;
int score[3];
float ave;
struct student *next;//指向下一个地址的指针
};
void main()
{
struct student *p1;
struct student *head=NULL;
struct student *p2;
int i,sum=0,m=0,t=0;
struct student stu[3];
p1=p2=(struct student*)malloc(sizeof(struct student));
printf("请输入学生信息,学号,3门成绩,姓名");
scanf("%d%d%d%d%s",&stu[0].num,&stu[0].score[0],&stu[0].score[1],&stu[0].score[2],stu[0].name);
p1->next=NULL;//保证安全
p2=p1;
head=p1;
for(i=1;i<3;i++){
p1=(struct student*)malloc(sizeof(struct student));
printf("请输入学生信息,学号,3门成绩,姓名");
scanf("%d%d%d%d%s",&stu[i].num,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2],stu[i].name);
p1->next=NULL;//保证安全
p2->next=p1;
p2=p1;}
printf("遍历数据\n");
for(p2=head;p2!=NULL;p2=p2->next){
for(i=0;i<3;i++){
printf("%d %d %d %d %s\n",stu[i].num,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].name);
} break;
}
for(i=0;i<3;i++){
stu[i].ave=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3;
printf("平均成绩为:%f\n",stu[i].ave);}
for(i=0;i<3;i++){
if(stu[i].ave>stu[m].ave){
m=i;}
}
t=m+1;
printf("第%d为学生平均成绩最大\n",t);
printf("**********平均成绩最大的学生信息***********\n");
printf("姓名为:%s\n",stu[m].name);
printf("学号为:%d\n",stu[m].num);
printf("3门成绩为:%d %d %d\n",stu[m].score[0],stu[m].score[1],stu[m].score[2]);
printf("平均成绩为:%f",stu[m].ave);
}
第一次写动态链表————学生信息管理
最新推荐文章于 2022-10-20 11:07:26 发布