C语言实现图书管理系统:按出版日期排序图书信息
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book {
char name[101];
char author[101];
char date[11];
};
int cmp(const void* a, const void* b) {
struct Book* bookA = (struct Book*)a;
struct Book* bookB = (struct Book*)b;
return strcmp(bookA->date, bookB->date);
}
int main() {
int n;
scanf('%d', &n);
struct Book* books = (struct Book*)malloc(n * sizeof(struct Book));
for (int i = 0; i < n; i++) {
scanf('%s %s %s', books[i].name, books[i].author, books[i].date);
}
qsort(books, n, sizeof(struct Book), cmp);
for (int i = 0; i < n; i++) {
printf('%s %s %s
', books[i].name, books[i].author, books[i].date);
}
free(books);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/ojKj 著作权归作者所有。请勿转载和采集!