#include <stdio.h>
#include <string.h>
int main()
{
// 提示用户请录入5个学生的成绩,求最高分及该学生学号,求最低分及该学生学号
typedef struct Student
{
char id[6]; // 学号
float score;
} Stu;
char temp[6]; // 临时数组,用于构建ID字符串
Stu stu[5];
Stu stu_max = stu[0], stu_min = stu[0];
for (int i = 0; i < 5; i++)
{
sprintf(temp, "110%d", i); // sprintf会自动在字符串的末尾添加空字符('\0')来作为字符串的结束符。
strcpy(stu[i].id, temp);
printf("请输入第%d学生的成绩\n", i + 1);
scanf("%f", &stu[i].score);
}
for (int i = 0; i < 5; i++)
{
printf("学号为%s学生的成绩%f\n", stu[i].id, stu[i].score);
if (stu_max.score < stu[i].score)
{
stu_max = stu[i];
}
if (stu_min.score > stu[i].score)
{
stu_min = stu[i];
}
}
printf("最高成绩学号为%s学生的成绩%f\n", stu_max.id, stu_max.score);
printf("最低成绩学号为%s学生的成绩%f\n", stu_min.id, stu_min.score);
return 0;
提示用户请录入5个学生的成绩,求最高分及该学生学号,求最低分及该学生学号
最新推荐文章于 2024-09-12 11:35:42 发布