📌  相关文章
📜  C程序,用于将学生记录存储为结构,并按年龄或ID对它们进行排序

📅  最后修改于: 2021-05-28 05:22:06             🧑  作者: Mango

给定学生的记录,每个记录包含学生的ID,姓名和年龄。编写一个C程序以读取这些记录并按年龄或ID排序显示它们。

按年龄排序

例子:

Input: Student Records = {
{Id = 1, Name = bd, Age = 12 },
{Id = 2, Name = ba, Age = 10 },
{Id = 3, Name = bc, Age = 8 },
{Id = 4, Name = aaz, Age = 9 },
{Id = 5, Name = az, Age = 10 } }

Output:
{{Id = 3, Name = bc, Age = 8 },
{Id = 4, Name = aaz, Age = 9 },
{Id = 2, Name = ba, Age = 10 },
{Id = 5, Name = az, Age = 10 },
{Id = 1, Name = bd, Age = 12 } }

方法:可通过以下步骤解决此问题:

  • 创建具有ID,名称和年龄字段的结构。
  • 阅读结构中的学生记录
  • 通过设置比较规则来定义比较器。在这里,年龄可以通过两个学生年龄的不同来分类。 (学生1->年龄–学生2->年龄)
  • 现在借助qsort()方法基于已定义的比较器对结构进行排序。
  • 打印排序的学生记录。

程序:

// C program to read Student records
// like id, name and age,
// and display them in sorted order by Age
  
#include 
#include 
#include 
  
// struct person with 3 fields
struct Student {
    char* name;
    int id;
    char age;
};
  
// setting up rules for comparison
// to sort the students based on age
int comparator(const void* p, const void* q)
{
    return (((struct Student*)p)->age - ((struct Student*)q)->age);
}
  
// Driver program
int main()
{
    int i = 0, n = 5;
  
    struct Student arr[n];
  
    // Get the students data
    arr[0].id = 1;
    arr[0].name = "bd";
    arr[0].age = 12;
  
    arr[1].id = 2;
    arr[1].name = "ba";
    arr[1].age = 10;
  
    arr[2].id = 3;
    arr[2].name = "bc";
    arr[2].age = 8;
  
    arr[3].id = 4;
    arr[3].name = "aaz";
    arr[3].age = 9;
  
    arr[4].id = 5;
    arr[4].name = "az";
    arr[4].age = 10;
  
    // Print the Unsorted Structure
    printf("Unsorted Student Records:\n");
    for (i = 0; i < n; i++) {
        printf("Id = %d, Name = %s, Age = %d \n",
               arr[i].id, arr[i].name, arr[i].age);
    }
    // Sort the structure
    // based on the specified comparator
    qsort(arr, n, sizeof(struct Student), comparator);
  
    // Print the Sorted Structure
    printf("\n\nStudent Records sorted by Age:\n");
    for (i = 0; i < n; i++) {
        printf("Id = %d, Name = %s, Age = %d \n",
               arr[i].id, arr[i].name, arr[i].age);
    }
  
    return 0;
}
输出:
Unsorted Student Records:
Id = 1, Name = bd, Age = 12 
Id = 2, Name = ba, Age = 10 
Id = 3, Name = bc, Age = 8 
Id = 4, Name = aaz, Age = 9 
Id = 5, Name = az, Age = 10 


Student Records sorted by Age:
Id = 3, Name = bc, Age = 8 
Id = 4, Name = aaz, Age = 9 
Id = 2, Name = ba, Age = 10 
Id = 5, Name = az, Age = 10 
Id = 1, Name = bd, Age = 12

按编号排序

例子:

Input: Student Records = {
{Id = 1, Name = bd, Age = 12 },
{Id = 2, Name = ba, Age = 10 },
{Id = 3, Name = bc, Age = 8 },
{Id = 4, Name = aaz, Age = 9 },
{Id = 5, Name = az, Age = 10 } }

Output:
{{Id = 1, Name = bd, Age = 12 },
{Id = 2, Name = ba, Age = 10 },
{Id = 3, Name = bc, Age = 8 },
{Id = 4, Name = aaz, Age = 9 },
{Id = 5, Name = az, Age = 10 } }

方法:可通过以下步骤解决此问题:

  • 创建具有ID,名称和年龄字段的结构。
  • 阅读结构中的学生记录
  • 通过设置比较规则来定义比较器。在这里,id可以借助2个学生的id的差异进行排序。 (学生1-> ID –学生2-> ID)
  • 现在借助qsort()方法基于已定义的比较器对结构进行排序。
  • 打印排序的学生记录。

程序:

// C program to read Student records
// like id, name and age,
// and display them in sorted order by ID
  
#include 
#include 
#include 
  
// struct person with 3 fields
struct Student {
    char* name;
    int id;
    char age;
};
  
// setting up rules for comparison
// to sort the students based on ID
int comparator(const void* p, const void* q)
{
    return (((struct Student*)p)->id - ((struct Student*)q)->id);
}
  
// Driver program
int main()
{
    int i = 0, n = 5;
  
    struct Student arr[n];
  
    // Get the students data
    arr[0].id = 1;
    arr[0].name = "bd";
    arr[0].age = 12;
  
    arr[1].id = 2;
    arr[1].name = "ba";
    arr[1].age = 10;
  
    arr[2].id = 3;
    arr[2].name = "bc";
    arr[2].age = 8;
  
    arr[3].id = 4;
    arr[3].name = "aaz";
    arr[3].age = 9;
  
    arr[4].id = 5;
    arr[4].name = "az";
    arr[4].age = 10;
  
    // Print the Unsorted Structure
    printf("Unsorted Student Records:\n");
    for (i = 0; i < n; i++) {
        printf("Id = %d, Name = %s, Age = %d \n",
               arr[i].id, arr[i].name, arr[i].age);
    }
    // Sort the structure
    // based on the specified comparator
    qsort(arr, n, sizeof(struct Student), comparator);
  
    // Print the Sorted Structure
    printf("\n\nStudent Records sorted by ID:\n");
    for (i = 0; i < n; i++) {
        printf("Id = %d, Name = %s, Age = %d \n",
               arr[i].id, arr[i].name, arr[i].age);
    }
  
    return 0;
}
输出:
Unsorted Student Records:
Id = 1, Name = bd, Age = 12 
Id = 2, Name = ba, Age = 10 
Id = 3, Name = bc, Age = 8 
Id = 4, Name = aaz, Age = 9 
Id = 5, Name = az, Age = 10 


Student Records sorted by ID:
Id = 1, Name = bd, Age = 12 
Id = 2, Name = ba, Age = 10 
Id = 3, Name = bc, Age = 8 
Id = 4, Name = aaz, Age = 9 
Id = 5, Name = az, Age = 10

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。