C语言实现图书管理系统:按出版日期排序图书信息
C语言实现图书管理系统:按出版日期排序图书信息
本程序使用 C 语言实现一个简单的图书管理系统,可以输入多本图书的信息,并按出版日期升序排序后输出。
代码实现
#include <stdio.h>
#include <string.h>
struct Book {
char title[101];
char author[101];
char date[11];
};
int main() {
int n;
scanf('%d', &n);
struct Book books[n];
for (int i = 0; i < n; i++) {
scanf('%s %s %s', books[i].title, books[i].author, books[i].date);
}
// 冒泡排序
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (strcmp(books[j].date, books[j+1].date) > 0) {
struct Book temp = books[j];
books[j] = books[j+1];
books[j+1] = temp;
}
}
}
// 输出排序后的图书信息
for (int i = 0; i < n; i++) {
printf('%s %s %s
', books[i].title, books[i].author, books[i].date);
}
return 0;
}
代码说明
- 数据结构定义: 使用
struct Book定义图书结构体,包含书名title、作者author和出版日期date三个字段。 - 输入图书信息: 使用
scanf函数从键盘输入图书数量n和每本图书的信息(书名、作者、出版日期)。 - 冒泡排序: 使用双层循环实现冒泡排序算法,比较图书的出版日期,按升序排列。
- 输出排序后的图书信息: 使用
printf函数输出排序后的图书信息,每本图书一行,包括书名、作者和出版日期。
运行示例
输入:
3
The Hitchhiker's Guide to the Galaxy Douglas Adams 1979-05-12
The Lord of the Rings J. R. R. Tolkien 1954-07-29
The Hobbit J. R. R. Tolkien 1937-09-21
输出:
The Hobbit J. R. R. Tolkien 1937-09-21
The Lord of the Rings J. R. R. Tolkien 1954-07-29
The Hitchhiker's Guide to the Galaxy Douglas Adams 1979-05-12
总结
本程序展示了使用 C 语言实现简单的图书管理系统,并通过冒泡排序算法将图书按出版日期升序排序。可以根据实际需求修改数据结构和排序算法,实现更复杂的图书管理功能。
原文地址: https://www.cveoy.top/t/topic/ojKs 著作权归作者所有。请勿转载和采集!