通过结构体数组给定 3 个学生的信息。信息包括:学号,姓名,成绩,找出成绩最高的学生的所有信息。
#include<stdio.h>
struct Student{
int num;
char name[20];
float score;
};
int main(){
struct Student a[3]={{1,"zhangsan",90},
{2,"list",80},
{3,"wangwu",100
}};
struct Student max=a[0];
int i;
for(i=0;i<=2;i++){
if(max.score<a[i].score)max=a[i];
}
printf("%d %s %f\n",max.num,max.name,max.score);
return 0;
}
给定5个学生的信息。信息包括:学号,姓名,成绩,从高到底排序输出每个学生的全部信息。
#include<stdio.h>
struct Student{
int num;
char name[20];
float score;
};
int main(){
struct Student a[5]={{1,"zhangsan",90},
{2,"list",80},
{3,"wangwu",100
},{3,"wangwu",50
},{3,"wangwu",72
}};
struct Student temp;
int j;
for(j=1;j<=4;j++){
int i;
for(i=0;i<=3;i++){
if(a[i].score<a[i+1].score){
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
int i;
for(i=0;i<=4;i++){
printf("%d %s %f\n",a[i].name,a[i].name,a[i].score);
}
return 0;
}
有3个学生结构体变量,学生信息包括:学号,姓名和3门课的成绩。要求输出平均分最高学生的全部信息。
#include<stdio.h>
#include<string.h>
struct Student{
int num;
char name[20];
float score[3];
float avg;
};
int main(){
struct Student stu[3];
stu[0].num=1;strcpy(stu[0].name,"zhangsan");
stu[0].score[0]=90;stu[0].score[1]=80;
stu[0].score[2]=75;
stu[0].avg=stu[0].score[0]/3+
stu[0].score[1]/3+stu[0].score[2]/3;
stu[1].num=2;strcpy(stu[1].name,"list");
stu[1].score[0]=98;stu[1].score[1]=60;
stu[1].score[2]=72;
stu[1].avg=stu[1].score[0]/3+
stu[1].score[1]/3+stu[1].score[2]/3;
stu[2].num=3;strcpy(stu[2].name,"wangwu");
stu[2].score[0]=70;stu[2].score[1]-90;
stu[2].score[2]=86;
stu[2].avg=stu[2].score[2]/3;
struct Student max=stu[0];
int i;
for(i=0;i<=2;i++){
if(max.avg<stu[i].avg)max=stu[i];
}
printf("%d %s %f %f %f %f",max.num,max.name,max.score[0],max.score[1],max.score[2],max.avg);
return 0;
}