c - 从动态分配的结构中删除和排序元素

标签 c

所以我的类(class)学分项目没有什么问题。 该程序应该是一个简单的数据库,保存有关学生的基本信息,例如他的姓名或号码。现在我的程序看起来或多或少像这样:

(我不会在这里粘贴整个程序,我认为其中的几个部分就足以解决我的问题)

这是结构和变量:

typedef struct {
     char name[20];
     char surname[20];
     int student_id;
}data;


 data*student;
 int number_of_students=0;
 int menu_choice;

这里是带有菜单选项的开关。

    switch(menu_choice)
    {
        case 1:

                system("cls");

                printf("Enter amount of students you want to put in data base\n");
                scanf("%d",&number_of_students);

                student=(data*)malloc(number_of_students*sizeof(data));
                adding_students_to_base( number_of_students);
        break;

        case 2:

                //deleting_records_from_base( number_of_students); 

        break;

        case 3:

                //print_base(number_of_students);

        break;

        case 4:

               // sorting_base( number_of_students);

        break;

        case 5:

                //saving_base_to_file( number_of_students);

        break;

        case 6:

                //loading_base_from_file();

        break;

        case 7:
                printf("Closing program!\n");
                _getch();
                exit(0);
        break;
     }

添加和打印功能:

void adding_students_to_base(int amount_of_students)
{
  int i;


    system("cls");

   for ( i=0;i<amount_of_students;i++)
   {
       printf("\nName: ");
       scanf("%s",&(student+i)->name);
       printf("\nSurname: ");
       scanf("%s",&(student+i)->surname);
       printf("\nId number: ");
       scanf("%d",&(student+i)->student_id);
       system("cls");
   }

 system("cls");
}

void print_base(int amount_of_students)
{
   int i;

   system("cls");

   for ( i=0;i<amount_of_students;i++)
   {
       printf("\n----------|%d|-------------",i+1);
       printf("\nStudent_id: %d",(student+i)->student_id);
       printf("\nName: %s",(student+i)->name);
       printf("\nSurname: %s",(student+i)->surname);
       printf("\n---------------------------\n");

}

   _getch();
   system("cls");
}

我已经成功创建了添加、打印和保存数据到数据库的工作功能,但我不知道如何处理问题排序数据。

您能给我一些如何解决这个问题的提示、线索或代码示例吗?

编辑

我成功解决了排序问题。我想在我的程序中添加最后两个函数,保存数据库和从 txt 文件加载数据库。 我认为保存 fork 很好,它看起来像这样:

void saving_base_to_file(int amount_of_students)
{
    FILE *file;


    system("cls");
    printf("Saving base to file!\n");

    file=fopen("database.txt","wb");
    fprintf(file,"%d",amount_of_students);
    fwrite(student,sizeof( data),amount_of_students,file);  
    fclose(file);

    _getch();
    system("cls");
}

当我想使用“loading_base_from_file”函数时出现问题。 函数如下所示:

void loading_base_from_file()
{
    FILE *file;

    system("cls");
    printf("Reading base from file\n");

    file=fopen("database.txt","rb");

    if (file!= NULL) {
        fscanf(file,"%d",&number_of_students);
        fread(&student,sizeof( data),number_of_students,file); //number_of_students is global variable
        student=(data*)malloc(number_of_students*sizeof(data));
        fclose(file);
    }
    else
    {
        printf("File does not exist!.\r\n");
        printf("File have to be named ""database.txt"" !!!\n");
    }
    _getch();
    system("cls");
}

例如,当我想保存一名学生 ID 为“123456”、名为“Greg”“Tesla”的学生时,文件包含以下内容: Improve this question 。函数 saving_base_to_file 还保存基地中的学生数量。但是当我再次启动我的程序(或在一个程序运行中执行此操作)并尝试从文件加载数据时,我的函数“print_base”会打印以下内容: database.txt

我认为将数据“放入”数组中存在问题,但我不知道到底出了什么问题。 您能告诉我为什么会发生这种情况以及如何解决它吗?

最佳答案

首先,关于您的数据结构:学生数量不固定。您可以删除学生,因此数组 student 的大小会发生变化。调整数组大小既昂贵又费力;使用链表而不是数组会让您受益匪浅。

如果你确实想使用数组:

  • 询问用户他想要删除的学生的 ID
  • 在数组中查找该学生的索引
  • 将所有学生切换到该索引右侧的左侧一位(这样您要删除的学生就会被覆盖,最后一个数组单元格会空闲)
  • 重新分配数组,因为您不需要最后一个数组单元(使用 realloc())。

要对数组进行排序,请考虑快速排序(通常是最有效的 - 库函数 qsort() )、插入排序或选择排序(这些更容易)。您可能必须定义一个函数来比较两个学生。

编辑

您要求提供一个示例,我想那是一个数组:

void delete(int *array, int index, int *size) {
    memmove(&(array[index]+1), &(array[index]), *size - index);
    (*size)--;
    realloc(array, size*sizeof(int));
}

关于c - 从动态分配的结构中删除和排序元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37503973/

相关文章:

c - Scanf矩阵在c中不起作用?

c - 打印数字直到输入负数

c - 在基本井字游戏中保存数据

c - 跳过下一列的数据(C)

android - 您如何从其源代码分发构建适用于 Android 的 FreeImage?

c - 将第二个字符与倒数第二个字符交换

c - 重新连接服务器后如何修复 EPIPE

c++ - 使用 malloc 安全吗?

c - C 中 printf 的标志和修饰符

c - 如何改进寻找孪生素数