在C中的结构中进行冒泡排序

v8wbuo2f  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(118)

我已经编写了一个基于结构数组的程序,它的所有部分都很好,但是我的一个函数sort_student函数有一个bug。在这个代码中,首先用户将输入一些学生和他们的信息,包括名字,姓氏,id和平均值,之后用户将有几个选项,除了排序功能外,所有选项都可以。这个函数的主要问题是,它只能对平均值进行排序,然后它会把第一个学生的名字,姓氏和id放在所有学生的结构中,我不知道怎么解决这个问题。我想我不得不说,我是业余的编码,所以请帮助,如果你想帮助,如果你不想请不要发表负面评论。谢谢.
这是有问题的函数:

void sort_student()
{
    float selected_item;
    int j;

    for (int i = 0; i < students_count; i++)
    {
        selected_item = student[i].average;
        j = i - 1;
        while ((j >= 0) && (selected_item < student[j].average))
        {
            student[j + 1].average = student[j].average;
            strcpy(student[j + 1].first_name, student[j].first_name);
            strcpy(student[j + 1].last_name, student[j].last_name);
            strcpy(student[j + 1].id, student[j].id);

            j--;
        }
        student[j + 1].average = selected_item;
    }

    printf("The students are sorted now.");
}

如果你需要,这是我所有的代码:

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

struct school
{
    char first_name[80];
    char last_name[80];
    char id[12];
    float average;
};

struct school *student;

int students_count;
int con = 1;
int chosen_option;

FILE *fptr;

void options();
void add_student();
void remove_student();
void search_student();
void sort_student();
void save_to_file();
void upload_students();

int main() {
    student = calloc(300, sizeof *student);

    printf("How many students do you have ? (MAX = 300)\n");
    scanf("%d", &students_count);

    for (int i = 0; i < students_count; i++)
    {
        printf("Whats your student number %d first name ? \n", i+1);
        scanf("%79s", student[i].first_name);

        printf("Whats your student number %d last name ? \n", i+1);
        scanf("%79s", student[i].last_name);

        printf("Whats your student number %d id ? \n", i+1);
        scanf("%11s", student[i].id);

        printf("Whats your student number %d average ? \n", i+1);
        scanf("%f", &student[i].average);
    }

    printf("\nOK! you've entered the students and their information successfully.\n");

    while (con == 1)
    {
        options();
    }
    return 0;
}

void options()
{
    printf("\nOK! Do you want to continue? (Yes : 1/N : 0)");
    scanf("%d", &con);

    if (con == 1)
    {
        printf("What do you want to do now ? \n");

        printf("\n1. Add student\n");
        printf("\n2. Remove student\n");
        printf("\n3. Search student\n");
        printf("\n4. Sort by average\n");
        printf("\n5. save the students information in a file\n");
        printf("\n6. upload all of students information\n");

        printf("\nPlease enter the number of you choice : ");
        scanf("%d", &chosen_option);

        switch (chosen_option) {
            case 1:
                add_student();
                break;
            case 2:
                remove_student();
                break;
            case 3:
                search_student();
                break;
            case 4:
                sort_student();
                break;
            case 5:
                save_to_file();
                break;
            case 6:
                upload_students();
                break;
            default:
                printf("Your chosen option is invalid\n");
                break;
        }
    }
    else if (con == 0)
    {
        printf("OK! You don't want to continue. Bye!\n");
        exit(0);
    } else
    {
        printf("You can't continue with invalid options.\n");
        exit(0);
    }
}

void add_student()
{
        printf("Whats your (new) student number %d first name ? \n", students_count+1);
        scanf("%79s", student[students_count].first_name);

        printf("Whats your (new) student number %d last name ? \n", students_count+1);
        scanf("%79s", student[students_count].last_name);

        printf("Whats your (new) student number %d id ? \n", students_count+1);
        scanf("%11s", student[students_count].id);

        printf("Whats your (new) student number %d average ? \n", students_count+1);
        scanf("%f", &student[students_count].average);

        students_count ++;

    printf("OK! You've entered the new student information successfully.\n");
}
void remove_student()
{
    char removing_id[12];
    int i;

    printf("\nPlease enter the id of student that you want to remove : ");
    scanf("%11s", removing_id);

    for (i = 0; i < students_count; i++)
    {
        if (strcmp(removing_id, student[i].id) == 0)
        {
            for (int j = i; j < students_count - 1; j++)
            {
                strcpy(student[j].first_name, student[j+1].first_name);
                strcpy(student[j].last_name, student[j+1].last_name);
                strcpy(student[j].id, student[j+1].id);
                student[j].average = student[j+1].average;
            }
            break;
        }
    }

    if(i == students_count)
    {
        printf("The id is not available!");
        exit(0);
    }
    else
    {
        students_count--;
        printf("The student removed successfully.");
    }
}
void search_student()
{
    char searching_id[12];
    int i;

    printf("\nPlease enter the id of student that you want to search : ");
    scanf("%11s",searching_id);

    for (i = 0; i < students_count; i++)
    {
        if (strcmp(searching_id, student[i].id) == 0)
        {
            printf("\nFIRSTNAME    LASTNAME    ID    AVERAGE\n");

            printf("\n%s\t%s\t%s\t%.2f\n", student[i].first_name, student[i].last_name, student[i].id,
                   student[i].average);
        }
    }

    if (i == students_count)
    {
        printf("We don't have student with this id.");
    }
}
void sort_student()
{
    float selected_item;
    int j;

    for (int i = 0; i < students_count; i++)
    {
        selected_item = student[i].average;
        j = i - 1;
        while ((j >= 0) && (selected_item < student[j].average))
        {
            student[j + 1].average = student[j].average;
            strcpy(student[j + 1].first_name, student[j].first_name);
            strcpy(student[j + 1].last_name, student[j].last_name);
            strcpy(student[j + 1].id, student[j].id);

            j--;
        }
        student[j + 1].average = selected_item;
    }

    printf("The students are sorted now.");
}
void save_to_file()
{
    fptr = fopen("MY SCHOOL FILE.txt", "w");

    fprintf(fptr, "#    FIRSTNAME    LASTNAME    ID    AVERAGE\n");

    for(int i = 0; i < students_count; i++)
    {
        fprintf(fptr, "%d\t%s\t%s\t%s\t%.2f\n", i+1, student[i].first_name, student[i].last_name,
                student[i].id, student[i].average);
    }

    fclose(fptr);

    printf("\nThe file is in project folder.\n");
}

void upload_students()
{
    printf("#    FIRSTNAME    LASTNAME    ID    AVERAGE\n");
    for (int i = 0; i < students_count; i++)
    {
        printf("%d\t%s\t%s\t%s\t%.2f\n", i+1, student[i].first_name, student[i]. last_name,
               student[i].id, student[i].average);
    }
}
kqqjbcuj

kqqjbcuj1#

对于初学者来说,函数sort_student中没有使用冒泡排序方法。实际上,你正在尝试实现插入排序方法。
在这个while循环中

while ((j >= 0) && (selected_item < student[j].average))
    {
        student[j + 1].average = student[j].average;
        strcpy(student[j + 1].first_name, student[j].first_name);
        strcpy(student[j + 1].last_name, student[j].last_name);
        strcpy(student[j + 1].id, student[j].id);

        j--;
    }

元素student[i]的所有数据成员都被覆盖。
该函数可以如下所示

void sort_student( void )
{
    for ( int i = 1; i < students_count; i++ )
    {
        struct school current_student = student[i];

        int j = i;

        for ( ; j != 0 && current_student.average < student[j - 1].average; --j )
        {
            student[j] = student[j-1];
        }

        if ( j != i ) student[j] = current_student;
    }

    printf("The students are sorted now.");
}

相关问题