源代码:
#include <stdio.h>
# include <string.h>
typedef struct Student{
int num;
char name[32];
int score[2];
int avg;
}Student;
int main(){
Student temp;
int size = sizeof(temp);
Student stu[3];
for(int i=0;i<3;i++){
printf("请输入你的数据\n");
scanf("%d %s %d %d %d",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].avg);
}
for( i=0;i<2;++i){
for (int j=0;j<2-i;j++){
if (stu[j+1].avg > stu[j].avg){
memcpy(&temp,&stu[j],size);
memcpy(&stu[j],&stu[j+1],size);
memcpy(&stu[j+1],&temp,size);
}
}
}
for(i=0;i<3;++i){
printf("%d %s %d %d %d\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].avg);
}
return 0;
}
测试结果为: