本帖最后由 claws0n 于 2018-9-24 13:36 编辑
你的好像不是真正的冒泡排序,多跑了。#include <stdio.h>
#include <malloc.h>
struct Student
{
int age;
float score;
char name[100];
};
void sort_struct(struct Student *Array, int len)
{
for(int i = 0; i < len-1; i++)
{
for(int j = 0 ; j < len-i-1; j++)
{
if(Array[j].score < Array[j+1].score)
{
struct Student t = Array[j];
Array[j] = Array[j+1];
Array[j+1] = t;
}
}
}
}
int main(void)
{
int i,j;
int len;
struct Student * pArr;
struct Student t;
printf("请输入您学生的个数:\n");
printf("len = ");
scanf("%d",&len);
pArr = (struct Student *)malloc(sizeof(struct Student) * len);
//赋值
for(i = 0; i < len; i++)
{
printf("请输入您第%d个学生的信息\n", i+1);
printf("age = ");
scanf("%d", &pArr[i].age);
printf("score = ");
scanf("%f", &pArr[i].score);
printf("name = ");
scanf("%s", pArr[i].name);
}
//冒泡排序
sort_struct(pArr, len);
//输出
for(i=0;i<len;i++)
{
printf("\n");
printf("这是您您第%d个学生的信息\n",i+1);
printf("age=%d\n",pArr[i].age);
printf("score=%.2f\n",pArr[i].score); // 格式化 2 位小数
printf("name=%s\n",pArr[i].name);
}
return 0;
}
|