#include <stdio.h>
#include <malloc.h>
struct Student{
int age;
float score;
char name[100];
};
void input(struct Student * info,int len){
int i;
for(i = 0;i < len;i++){
printf("请输入第%d个学生的信息:\n",i+1);
printf("age = ");
scanf("%d",&info[i].age);
printf("score = ");
scanf("%f",&info[i].score);
printf("name = ");
scanf("%s",info[i].name);
}
}
void sort(struct Student * info,int len){
struct Student t;
int i,j;
for(i = 0;i < len-1;i++){
for(j = 0;j < len-1-i;j++){
if(info[j].score < info[j+1].score){
t = info[j];
info[j] = info[j+1];
info[j+1] = t;
}
}
}
}
void output(struct Student * info,int len){
int i;
printf("以下是学生的信息:\n");
for(i = 0;i < len;i++){
printf("第%d个学生的信息:\n",i+1);
printf("age = %d\n",info[i].age);
printf("score = %f\n",info[i].score);
printf("name = %s\n",info[i].name);
}
}
int main(void){
struct Student * pArr;
int len;
printf("请输入学生的个数:");
scanf("%d",&len);
pArr = (struct Student *)malloc(len * sizeof (struct Student));
input(pArr,len);
sort(pArr,len);
output(pArr,len);
return 0;
}
|