学生得记录由学号和成绩组称个,N名大学生得数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能时:按分数的高低排列学生的记录,高分在前。

学生得记录由学号和成绩组称个,N名大学生得数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能时:按分数的高低排列学生的记录,高分在前。

#include <stdio.h>
#define MAX_STUDENTS 100
typedef struct Student
{
    int studentId;
    int score;
} Student;
void bubbleSort(Student students[], int n)
{
    Student temp;
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (students[j].score < students[j + 1].score)
            {
                temp=students[j];
                students[j]=students[j+1];
                students[j+1]=temp;
            }
        }
    }
}
void printStudents(Student students[], int n)
{
    for (int i = 0; i < n; i++)
    {
        printf("学号: %d, 成绩: %d\n", students[i].studentId, students[i].score);
    }
}
int main()
{
    int N = 5;
    Student students[MAX_STUDENTS] = {
        {101, 80},
        {102, 65},
        {103, 90},
        {104, 75},
        {105, 85}};
    printf("排序前的学生记录:\n");
    printStudents(students, N);
    bubbleSort(students, N);
    printf("\n排序后的学生记录:\n");
    printStudents(students, N);

    return 0;
}

posted on 2024-06-26 23:43  wessf  阅读(31)  评论(0)    收藏  举报