#include <stdio.h>
#include <malloc.h>
struct Student
{
int age;
float score;
char name[100];
};
int main(void)
{
struct Student * pst;
struct Student tst;
int i, j, m, n;
int len;
printf("请输入学生的个数len = ");
scanf("%d", &len);
pst = (struct Student *)malloc(
len * sizeof(struct Student));
for(i = 0; i<len; i++)
{
printf("请输入第%d个学生的信息:\n", (i+1));
printf("name = ");
scanf("%s", pst[i].name);
printf("age = ");
scanf("%d", &pst[i].age);
printf("score = ");
scanf("%f", &pst[i].score);
}
//按分数从小到大进行排序
for(m=0; m<len; m++)
{
for(n = 0; n<len-1-m;n++)
if(pst[n].score > pst[n+1].score)
{
tst = pst[n];
pst[n] = pst[n+1];
pst[n+1] = tst;
}
}
printf("\n==========学生信息输出============\n\n");
for(j = 0; j<len; j++)
{
printf("输出第%d个学生的信息:\n", (j+1));
printf("name = ");
printf("%s\n", pst[j].name);
printf("age = ");
printf("%d\n", pst[j].age);
printf("score = ");
printf("%f\n", pst[j].score);
}
return 0;
}
利用结构体输入学生信息并按分数排序后输出
最新推荐文章于 2024-03-05 11:30:00 发布