用C语言写一个用链表数据结构实现的图书管理系统,要求包括8个数据项:书名、ISBN、作者、出版社、出版时间、入库时间、库存量、借阅数,并完成以下操作,录入、删除、修改、查询、借书、还书、统计、输出
时间: 2023-07-10 13:35:07 浏览: 122
好的,下面是用链表数据结构实现的图书管理系统的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// 定义图书信息结构体
typedef struct Book {
char name[50]; // 书名
char ISBN[20]; // ISBN
char author[20]; // 作者
char publisher[20]; // 出版社
char publish_time[20];// 出版时间
char enter_time[20]; // 入库时间
int stock; // 库存量
int borrowed; // 借阅数
struct Book *next; // 指向下一个节点的指针
} Book;
// 定义链表头指针
Book *head = NULL;
// 函数声明
void add_book();
void delete_book();
void update_book();
void search_book();
void borrow_book();
void return_book();
void statistics();
void output();
void save_file();
void load_file();
int main() {
int choice;
load_file(); // 读取文件中的图书信息
while (1) {
printf("***************************\n");
printf("* 1. 添加图书 *\n");
printf("* 2. 删除图书 *\n");
printf("* 3. 修改图书 *\n");
printf("* 4. 查找图书 *\n");
printf("* 5. 借阅图书 *\n");
printf("* 6. 归还图书 *\n");
printf("* 7. 统计信息 *\n");
printf("* 8. 输出图书列表 *\n");
printf("* 0. 退出 *\n");
printf("***************************\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
delete_book();
break;
case 3:
update_book();
break;
case 4:
search_book();
break;
case 5:
borrow_book();
break;
case 6:
return_book();
break;
case 7:
statistics();
break;
case 8:
output();
break;
case 0:
save_file(); // 保存图书信息到文件中
exit(0);
default:
printf("输入有误,请重新输入。\n");
}
}
return 0;
}
// 添加图书
void add_book() {
Book *new_book = (Book *) malloc(sizeof(Book));
printf("请输入书名:");
scanf("%s", new_book->name);
printf("请输入ISBN:");
scanf("%s", new_book->ISBN);
printf("请输入作者:");
scanf("%s", new_book->author);
printf("请输入出版社:");
scanf("%s", new_book->publisher);
printf("请输入出版时间(例如:2022-01-01):");
scanf("%s", new_book->publish_time);
time_t t = time(NULL);
struct tm *tm = localtime(&t);
sprintf(new_book->enter_time, "%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
printf("请输入库存量:");
scanf("%d", &new_book->stock);
new_book->borrowed = 0;
new_book->next = NULL;
if (head == NULL) {
head = new_book;
} else {
Book *current_book = head;
while (current_book->next != NULL) {
current_book = current_book->next;
}
current_book->next = new_book;
}
printf("添加成功!\n");
}
// 删除图书
void delete_book() {
char name[50];
printf("请输入要删除的书名:");
scanf("%s", name);
if (head == NULL) {
printf("列表为空,无法删除。\n");
return;
}
Book *current_book = head;
Book *previous_book = NULL;
while (current_book != NULL) {
if (strcmp(current_book->name, name) == 0) {
if (previous_book == NULL) {
head = current_book->next;
} else {
previous_book->next = current_book->next;
}
free(current_book);
printf("删除成功!\n");
return;
}
previous_book = current_book;
current_book = current_book->next;
}
printf("图书不存在,无法删除。\n");
}
// 修改图书
void update_book() {
char name[50];
printf("请输入要修改的书名:");
scanf("%s", name);
Book *current_book = head;
while (current_book != NULL) {
if (strcmp(current_book->name, name) == 0) {
printf("请输入新的书名:");
scanf("%s", current_book->name);
printf("请输入新的ISBN:");
scanf("%s", current_book->ISBN);
printf("请输入新的作者:");
scanf("%s", current_book->author);
printf("请输入新的出版社:");
scanf("%s", current_book->publisher);
printf("请输入新的出版时间(例如:2022-01-01):");
scanf("%s", current_book->publish_time);
printf("请输入新的库存量:");
scanf("%d", ¤t_book->stock);
printf("修改成功!\n");
return;
}
current_book = current_book->next;
}
printf("图书不存在,无法修改。\n");
}
// 查找图书
void search_book() {
char name[50];
printf("请输入要查找的书名:");
scanf("%s", name);
Book *current_book = head;
while (current_book != NULL) {
if (strcmp(current_book->name, name) == 0) {
printf("书名:%s\n", current_book->name);
printf("ISBN:%s\n", current_book->ISBN);
printf("作者:%s\n", current_book->author);
printf("出版社:%s\n", current_book->publisher);
printf("出版时间:%s\n", current_book->publish_time);
printf("库存量:%d\n", current_book->stock);
printf("借阅数:%d\n", current_book->borrowed);
return;
}
current_book = current_book->next;
}
printf("图书不存在。\n");
}
// 借阅图书
void borrow_book() {
char name[50];
printf("请输入要借阅的书名:");
scanf("%s", name);
Book *current_book = head;
while (current_book != NULL) {
if (strcmp(current_book->name, name) == 0) {
if (current_book->stock > 0) {
current_book->stock--;
current_book->borrowed++;
printf("借阅成功!\n");
} else {
printf("库存不足,无法借阅。\n");
}
return;
}
current_book = current_book->next;
}
printf("图书不存在,无法借阅。\n");
}
// 归还图书
void return_book() {
char name[50];
printf("请输入要归还的书名:");
scanf("%s", name);
Book *current_book = head;
while (current_book != NULL) {
if (strcmp(current_book->name, name) == 0) {
if (current_book->borrowed > 0) {
current_book->stock++;
current_book->borrowed--;
printf("归还成功!\n");
} else {
printf("该图书没有借阅记录,无法归还。\n");
}
return;
}
current_book = current_book->next;
}
printf("图书不存在,无法归还。\n");
}
// 统计信息
void statistics() {
int total_stock = 0;
int total_borrowed = 0;
int total = 0;
Book *current_book = head;
while (current_book != NULL) {
total_stock += current_book->stock;
total_borrowed += current_book->borrowed;
total++;
current_book = current_book->next;
}
printf("图书总数:%d\n", total);
printf("库存总量:%d\n", total_stock);
printf("借阅总数:%d\n", total_borrowed);
}
// 输出图书列表
void output() {
printf("书名\tISBN\t作者\t出版社\t出版时间\t入库时间\t库存量\t借阅数\n");
Book *current_book = head;
while (current_book != NULL) {
printf("%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\n", current_book->name, current_book->ISBN,
current_book->author, current_book->publisher, current_book->publish_time,
current_book->enter_time, current_book->stock, current_book->borrowed);
current_book = current_book->next;
}
}
// 保存图书信息到文件中
void save_file() {
FILE *fp = fopen("book.txt", "w");
if (fp == NULL) {
printf("文件打开失败。\n");
return;
}
Book *current_book = head;
while (current_book != NULL) {
fprintf(fp, "%s %s %s %s %s %s %d %d\n", current_book->name, current_book->ISBN,
current_book->author, current_book->publisher, current_book->publish_time,
current_book->enter_time, current_book->stock, current_book->borrowed);
current_book = current_book->next;
}
fclose(fp);
}
// 读取文件中的图书信息
void load_file() {
char name[50];
char ISBN[20];
char author[20];
char publisher[20];
char publish_time[20];
char enter_time[20];
int stock;
int borrowed;
FILE *fp = fopen("book.txt", "r");
if (fp == NULL) {
printf("文件打开失败。\n");
return;
}
while (fscanf(fp, "%s %s %s %s %s %s %d %d\n", name, ISBN, author, publisher, publish_time, enter_time, &stock, &borrowed) != EOF) {
Book *new_book = (Book *) malloc(sizeof(Book));
strcpy(new_book->name, name);
strcpy(new_book->ISBN, ISBN);
strcpy(new_book->author, author);
strcpy(new_book->publisher, publisher);
strcpy(new_book->publish_time, publish_time);
strcpy(new_book->enter_time, enter_time);
new_book->stock = stock;
new_book->borrowed = borrowed;
new_book->next = NULL;
if (head == NULL) {
head = new_book;
} else {
Book *current_book = head;
while (current_book->next != NULL) {
current_book = current_book->next;
}
current_book->next = new_book;
}
}
fclose(fp);
}
```
其中,我们用 `Book` 结构体来存储图书信息,定义了链表头指针 `head`,并实现了以下操作:
- 添加图书
- 删除图书
- 修改图书
- 查找图书
- 借阅图书
- 归还图书
- 统计信息
- 输出图书列表
另外,我们还实现了将图书信息保存到文件和从文件中读取图书信息的函数 `save_file()` 和 `load_file()`。这样,在程序运行结束时,可以将图书信息保存到文件中,下次运行程序时可以读取文件中的图书信息。
注意:由于没有对输入做过多判断和处理,所以输入不规范的数据可能会导致程序崩溃,请谨慎使用。另外,由于时间和精力有限,代码可能存在一些潜在的问题,如有发现,请指出。
阅读全文