#include <stdio.h> #include <string.h>

// 定义学生结构体 struct Student { char name[20]; int score; };

// 排序函数 void StructSort(struct Student stu[], int n) { int i, j; struct Student temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (stu[j].score < stu[j + 1].score) { temp = stu[j]; stu[j] = stu[j + 1]; stu[j + 1] = temp; } } } }

int main() { struct Student stu[4]; int i; // 输入学生信息 for (i = 0; i < 4; i++) { printf("请输入第%d个学生的姓名:", i + 1); scanf("%s", stu[i].name); printf("请输入第%d个学生的成绩:", i + 1); scanf("%d", &stu[i].score); } // 排序并输出学生信息 StructSort(stu, 4); printf("按分数降序排列的学生信息:\n"); for (i = 0; i < 4; i++) { printf("姓名:%s,成绩:%d\n", stu[i].name, stu[i].score); } return 0;

学生的记录由姓名和成绩组成在主函数中循环输入4名学生的数据并用结构体数组存储请编写函数StructSort按分数的高低降序排列输出学生的姓名和分数。

原文地址: https://www.cveoy.top/t/topic/fixa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录