qfedu-c-level/day12/homework/h8.c

56 lines
1.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 练习结构体数组排序   从键盘输入5个学生的信息姓名、学号、成绩,
// 存入一个结构体数组中,计算平均分,并按成绩高低排序并输出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stu_s
{
char name[32];
int sid;
float score;
} STU;
void arrAndOutput(STU *stus) // 排序并输出
{
float avg = 0.0f;
for (int i = 0; i < 5; i++)
{
avg += stus[i].score / 5;
}
printf("平均分为: %.2f\n", avg);
// 冒泡排序
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - i - 1; j++)
{
if (stus[j].score < stus[j + 1].score)
{
STU tmp = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = tmp;
}
}
}
// 输出
for (int i = 0; i < 5; i++)
{
printf("姓名: %s, 学号: %d, 成绩: %.2f\n", stus[i].name, stus[i].sid, stus[i].score);
}
}
int main()
{
STU s[5];
for (int i = 0; i < 5; i++)
{
printf("请输入第 %d 个学生的,格式为:(姓名 学号 成绩)", i + 1);
scanf("%s %d %f", s[i].name, &s[i].sid, &s[i].score);
}
arrAndOutput(s);
return 0;
}