使用结构体按字母顺序排序名称的 C 语言程序


2022年3月2日, Learn eTutorial
2596

C 语言中的结构体数据类型是什么?

结构体是 C 语言中用户定义的数据类型。它是由不同数据类型的元素组成的一个集合,由单个名称表示,并按内存位置的顺序存储。我们使用结构体来记录其他数据类型元素。我们使用关键字 `struct` 来定义一个结构体。结构体的示例如下:

  struct person
    {
        char name[10];
        int rno;
    };

如何在 C 语言中对结构体列表中的元素进行排序?

为了实现学生姓名按字母顺序排序的 C 语言程序,我们必须导入头文件。现在,初始化一个结构体 person 并定义函数 **sort(int n)**。

将学生人数读入变量 no。然后,我们使用 `for 循环` 接受学生的姓名和学号到 **stud[i].Name**、**stud[i].Rollno** 中。然后,我们使用 for 循环和 `printf` 函数显示学生姓名和学号的列表。

调用 sort 函数,使用 **strcmp** 函数和 temp 变量对列表中的元素进行排序。排序后,我们将在主程序中使用 `for 循环` 显示排序后的列表。在 **sort()** 函数内部,我们使用 **strcmp()** 函数将每个名称与下一个名称进行比较,并根据我们需要的条件进行 `交换`。

注意: 我们在程序中使用 **string.h** 头文件来使用在 **string.h** 库中定义的 **strcmp** 函数。

算法

步骤1:包含头文件以使用C语言程序中的内置函数。

步骤 2: 包含头文件 **string.h**。

步骤 3: 定义结构体 person,包含 **name** 和 **no** 作为结构体成员。

步骤 4: 为结构体 person 创建对象 **NAME**。

步骤 5: 声明 **stud[10],temp[10]** 为 **NAME** 类型。

步骤 6: 声明变量 **no, 'i'** 为整数类型。

步骤 7: 声明函数 void **sort(int N)**。

步骤 8: 将学生人数读入变量 **no**。

步骤 9: 使用 `for 循环` 将人的姓名读入变量 **stud[i].name**,将学号读入变量 **stud[i].rno**,并赋值 **temp[i]=stud[i]**。

步骤 10: 使用 `printf` 函数显示排序前的名称。

步骤 11: 使用 `for 循环` 显示列表,条件为 **i**。

步骤 12: 然后调用函数 **sort(no)** 对名称进行排序。

步骤 13: 使用 for 循环显示排序后的名称,包括 **stud[i].name,stud[i].rno**。

函数 void sort(int N)

步骤 1: 声明变量 **i, j** 为整数类型。

步骤 2: 声明变量 **temp** 为 **NAME** 类型。

步骤 3: 使用嵌套 `for 循环` 比较 **srtd[i].name** 和 **stud[j].name**,如果 **std[i].name 大于 std[j].name** 则交换它们。


为了实现这个排序程序,我们使用了以下 C 语言概念。我们建议阅读这些主题以更好地理解。

C 语言源代码

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

struct person {
  char name[10]; /* Using structure to save the names */
  int rno;
};

typedef struct person NAME; /* creating object for using structure */
NAME stud[10], temp[10];
void main() {
  int no, i;
  void sort(int N); /* Function declaration */
  fflush(stdin);
  printf("Enter the number of students in the list\n"); /* enters the number of students */
  scanf("%d", & no);
  for (i = 0; i < no; i++) {
    printf("\n Enter the name of  person %d : ", i + 1);
    fflush(stdin);
    scanf("%s", & stud[i].name);
    printf("Enter the roll number of %d : ", i + 1); /* enters and accepting the name and roll number */
    scanf("%d", & stud[i].rno);
    temp[i] = stud[i];
  }

  printf("\n*****************************\n");
  printf("     Names before sorting     \n");
  /* Print the list of names before sorting */
  for (i = 0; i < no; i++) {
    printf("%-10s\t=%d\n", temp[i].name, temp[i].rno);
  }
  sort(no); /* Function call */

  printf("\n*****************************\n");

  printf("     Names after sorting     \n");

  printf("\n*****************************\n");

  /* Display the sorted names */
  for (i = 0; i < no; i++) {
    printf("%-10s\t=%d\n", stud[i].name, stud[i].rno);
  }

  printf("\n*****************************\n");
} /* End of main() */

/* Function to sort the given names */
void sort(int N) {
  int i, j;
  NAME temp;
  for (i = 0; i < N - 1; i++) {
    for (j = i + 1; j < N; j++) {
      if (strcmp(stud[i].name, stud[j].name) > 0) {
        temp = stud[i];
        stud[i] = stud[j];
        stud[j] = temp;
      }
    }
  }
} /* end of sort() */
                                      

输出

Enter the number of students in the list
5

Enter the name of  person 1: Rajashree
Enter the roll number of 1 : 123


Enter the name of  person 2 : John
Enter the roll number of 2 : 222


Enter the name of  person 3 : Priya
Enter the roll number of 3 : 331


Enter the name of  person 4: Anand
Enter the roll number of 4: 411


Enter the name of  person 5: Nirmala
Enter the roll number of 5 : 311


*****************************

     Names before sorting

Rajashree       123
John                222
Priya               331
Anand             411
Nirmala           311

*****************************
     Names after sorting
*****************************
Anand           411
John             222
Nirmala         311
Priya              331
Rajashree      123