首頁 > 後端開發 > C++ > C程式以結構體儲存學生記錄,並依姓名進行排序

C程式以結構體儲存學生記錄,並依姓名進行排序

王林
發布: 2023-08-26 15:17:06
轉載
1109 人瀏覽過

C程式以結構體儲存學生記錄,並依姓名進行排序

在這個問題中,我們被給定一個學生的記錄,其中包含學生的學號、姓名和百分比。我們的任務是建立一個C程序,將學生記錄儲存為結構,並按照姓名對它們進行排序。

讓我們舉一個例子來理解這個問題,

#輸入 - 學生記錄=

{{ student_id = 1, student_name = nupur, student_percentage = 98},
{ student_id = 2, student_name = Akash, student_percentage = 75},
{ student_id = 3, student_name = Yash, student_percentage = 62},
{ student_id = 4, student_name = Jyoti, student_percentage = 87},
{ student_id = 5, student_name = Ramlal, student_percentage = 80}}
登入後複製

輸出 − 學生記錄=

{{ student_id = 2, student_name = Akash, student_percentage = 75},
{ student_id = 4, student_name = Jyoti, student_percentage = 87},
{ student_id = 1, student_name = nupur, student_percentage = 98},
{ student_id = 5, student_name = Ramlal, student_percentage = 80},
{ student_id = 3, student_name = Yash, student_percentage = 62}}
登入後複製

為了解決這個問題,我們首先會建立一個儲存學生詳細資料的結構。現在,我們將使用qsort()函數,並在該函數中定義一個比較器函數,該函數將使用strcmp()方法比較結構的名稱。

範例

將學生記錄儲存為結構並按名稱排序的程式

 線上示範

//C program to store Student records as Structures and Sort them by Name
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
   int student_id;
   char* student_name;
   int student_percentage;
};
int comparator(const void* s1, const void* s2){
   return strcmp(((struct Student*)s1)->student_name,((struct Student*)s2)->student_name);
}
int main() {
   int n = 5;
   struct Student arr[n];
   //student 1
   arr[0].student_id = 1;
   arr[0].student_name = "Nupur";
   arr[0].student_percentage = 98;
   //student 2
   arr[1].student_id = 2;
   arr[1].student_name = "Akash";
   arr[1].student_percentage = 75;
   //student 3
   arr[2].student_id = 3;
   arr[2].student_name = "Yash";
   arr[2].student_percentage = 62;
   //student 4
   arr[3].student_id = 4;
   arr[3].student_name = "Jyoti";
   arr[3].student_percentage = 87;
   //student 5
   arr[4].student_id = 5;
   arr[4].student_name = "Ramlal";
   arr[4].student_percentage = 80;
   printf("Unsorted Student Record:</p><p>");
   for (int i = 0; i < n; i++) {
      printf("Id = %d, Name = %s, Age = %d </p><p>", arr[i].student_id, arr[i].student_name, arr[i].student_percentage);
   }
   qsort(arr, n, sizeof(struct Student), comparator);
   printf("</p><p></p><p>Student Records sorted by Name:</p><p>");
   for (int i = 0; i < n; i++) {
      printf("Id = %d, Name = %s, Age = %d </p><p>", arr[i].student_id, arr[i].student_name, arr[i].student_percentage);
   }
   return 0;
}
登入後複製

輸出

Unsorted Student Record:
Id = 1, Name = Nupur, Age = 98
Id = 2, Name = Akash, Age = 75
Id = 3, Name = Yash, Age = 62
Id = 4, Name = Jyoti, Age = 87
Id = 5, Name = Ramlal, Age = 80
Student Records sorted by Name:
Id = 2, Name = Akash, Age = 75
Id = 4, Name = Jyoti, Age = 87
Id = 1, Name = Nupur, Age = 98
Id = 5, Name = Ramlal, Age = 80
Id = 3, Name = Yash, Age = 62
登入後複製

以上是C程式以結構體儲存學生記錄,並依姓名進行排序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板