找回密码
 注册

QQ登录

只需一步,快速开始

搜索

图书管理系统-C语言代码

[复制链接]
bluesky1 发表于 2023-9-29 01:18:35 | 显示全部楼层 |阅读模式
1. 定义图书类Book,包括如下属性:
- 书名
- 作者
- 出版社
- 出版日期
- ISBN号
- 借出状态

2. 定义读者类Reader,包括如下属性:
- 姓名
- 学号/工号
- 借阅书籍数量
- 借阅历史

3. 定义图书馆类Library,包括如下属性:
- 图书馆名称
- 图书总量
- 已借出图书数量
- 图书列表

4. 定义如下函数进行图书管理:
- add_book():添加书籍
- remove_book():删除书籍
- borrow_book():借阅书籍
- return_book():归还书籍
- check_book_status():检查书籍借出状态
- search_book():查找书籍
- add_reader():添加读者
- remove_reader():删除读者
- borrow_history():查看借阅历史
- check_reader_status():检查读者借书状态
- search_reader():查找读者

5. 根据以上函数实现一个完整的图书馆管理系统。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. // 图书类
  5. typedef struct book_struct {
  6.     char name[100];
  7.     char author[50];
  8.     char publisher[50];
  9.     char publish_date[20];
  10.     char isbn[50];
  11.     int borrowed;
  12. } Book;

  13. // 读者类
  14. typedef struct reader_struct {
  15.     char name[50];
  16.     char id[50];
  17.     int borrow_amount;
  18.     Book *borrow_list[10];   // 保存借阅记录
  19. } Reader;

  20. // 图书馆类
  21. typedef struct library_struct {
  22.     char name[50];
  23.     int total_books;
  24.     int borrowed_books;
  25.     Book *book_list[10000];  // 最多保存10000本书
  26.     Reader *reader_list[1000];  // 最多保存1000名读者
  27. } Library;

  28. // 函数声明
  29. void add_book(Library *lib, Book *book);
  30. void remove_book(Library *lib, Book *book);
  31. void borrow_book(Library *lib, Reader *reader, Book *book);
  32. void return_book(Library *lib, Reader *reader, Book *book);
  33. void check_book_status(Book *book);
  34. Book* search_book(Library *lib, char *name);
  35. void add_reader(Library *lib, Reader *reader);
  36. void remove_reader(Library *lib, Reader *reader);
  37. void borrow_history(Reader *reader);
  38. void check_reader_status(Reader *reader);
  39. Reader* search_reader(Library *lib, char *name);

  40. int main() {
  41.     // 初始化图书馆
  42.     Library lib = {"XX大学图书馆", 0, 0, {NULL}, {NULL}};

  43.     // 初始化几本书
  44.     Book b1 = {"算法导论", "Thomas H. Cormen", "机械工业出版社", "2006-8-1", "9787111187776", 0};
  45.     Book b2 = {"UNIX编程艺术", "Eric S. Raymond", "人民邮电出版社", "2004-1-1", "9787115120024", 0};
  46.     Book b3 = {"信息简史", "James Gleick", "中信出版社", "2002-7-1", "9787508600987", 0};

  47.     // 添加几本书到图书馆
  48.     add_book(&lib, &b1);
  49.     add_book(&lib, &b2);
  50.     add_book(&lib, &b3);

  51.     // 初始化读者
  52.     Reader r1 = {"张三", "2018001", 0, {NULL}};

  53.     // 添加读者
  54.     add_reader(&lib, &r1);

  55.     // 借一本书
  56.     borrow_book(&lib, &r1, &b1);

  57.     // 返还一本书
  58.     return_book(&lib, &r1, &b1);

  59.     return 0;
  60. }

  61. // 添加书籍
  62. void add_book(Library *lib, Book *book) {
  63.     if(lib->total_books >= 10000) {
  64.         printf("Error: 图书馆容量已满,无法添加新书!\n");
  65.         return;
  66.     }
  67.     lib->book_list[lib->total_books] = book;
  68.     lib->total_books++;
  69.     printf("添加 "%s" 成功!\n", book->name);
  70. }

  71. // 删除书籍
  72. void remove_book(Library *lib, Book *book) {
  73.     for(int i=0; i<lib->total_books; i++) {
  74.         if(strcmp(lib->book_list[i]->name, book->name)==0) {
  75.             // 如果这本书借出去了,不允许删除
  76.             if(lib->book_list[i]->borrowed) {
  77.                 printf("Error: 书籍 "%s" 已借出,无法删除!\n", book->name);
  78.                 return;
  79.             }
  80.             lib->total_books--;
  81.             for(int j=i; j<lib->total_books; j++) {
  82.                 lib->book_list[j] = lib->book_list[j+1];
  83.             }
  84.             printf("删除 "%s" 成功!\n", book->name);
  85.             return;
  86.         }
  87.     }
  88.     printf("Error: 没有找到要删除的书籍!\n");
  89. }

  90. // 借阅书籍
  91. void borrow_book(Library *lib, Reader *reader, Book *book) {
  92.     // 判断读者已经借了多少本书了
  93.     if(reader->borrow_amount >= 10) {
  94.         printf("Error: 读者 "%s" 借书已达上限!\n", reader->name);
  95.         return;
  96.     }
  97.     // 判断这本书是否已经借出去了
  98.     if(book->borrowed) {
  99.         printf("Error: 书籍 "%s" 已被借出!\n", book->name);
  100.         return;
  101.     }

  102.     reader->borrow_list[reader->borrow_amount] = book;
  103.     reader->borrow_amount++;
  104.     book->borrowed = 1;
  105.     lib->borrowed_books++;
  106.     printf(""%s" 已被 "%s" 借阅!\n", book->name, reader->name);
  107. }

  108. // 归还书籍
  109. void return_book(Library *lib, Reader *reader, Book *book) {
  110.     // 查找这本书是否由该读者借阅
  111.     int found = 0;
  112.     for(int i=0; i<reader->borrow_amount; i++) {
  113.         if(reader->borrow_list[i] == book) {
  114.             found = 1;
  115.             for(int j=i; j<reader->borrow_amount-1; j++) {
  116.                 reader->borrow_list[j] = reader->borrow_list[j+1];
  117.             }
  118.             reader->borrow_list[reader->borrow_amount-1] = NULL;
  119.             reader->borrow_amount--;
  120.             book->borrowed = 0;
  121.             lib->borrowed_books--;
  122.             printf(""%s" 已被 "%s" 归还!\n", book->name, reader->name);
  123.             break;
  124.         }
  125.     }
  126.     if(!found) {
  127.         printf("Error: "%s" 并没有借阅 "%s"!\n", reader->name, book->name);
  128.     }
  129. }

  130. // 检查书籍的借出状态
  131. void check_book_status(Book *book) {
  132.     if(book->borrowed) {
  133.         printf(""%s" 已经被借出了!\n", book->name);
  134.     } else {
  135.         printf(""%s" 目前还没被借出!\n", book->name);
  136.     }
  137. }

  138. // 查找书籍
  139. Book* search_book(Library *lib, char *name) {
  140.     for(int i=0; i<lib->total_books; i++) {
  141.         if(strcmp(lib->book_list[i]->name, name)==0) {
  142.             return lib->book_list[i];
  143.         }
  144.     }
  145.     printf("Error: 没有找到书籍 "%s"!\n", name);
  146.     return NULL;
  147. }

  148. // 添加读者
  149. void add_reader(Library *lib, Reader *reader) {
  150.     if(lib->total_books >= 1000) {
  151.         printf("Error: 图书馆读者容量已满,无法添加新读者!\n");
  152.         return;
  153.     }
  154.     lib->reader_list[lib->total_books] = reader;
  155.     lib->total_books++;
  156.     printf("添加读者 "%s" 成功!\n", reader->name);
  157. }

  158. // 删除读者
  159. void remove_reader(Library *lib, Reader *reader) {
  160.     for(int i=0; i<lib->total_books; i++) {
  161.         if(lib->reader_list[i] == reader) {
  162.             // 如果这个读者还有未归还的书,不允许删除
  163.             if(reader->borrow_amount > 0) {
  164.                 printf("Error: 读者 "%s" 还有未归还的书籍,无法删除!\n", reader->name);
  165.                 return;
  166.             }
  167.             lib->total_books--;
  168.             for(int j=i; j<lib->total_books; j++) {
  169.                 lib->reader_list[j] = lib->reader_list[j+1];
  170.             }
  171.             printf("删除读者 "%s" 成功!\n", reader->name);
  172.             return;
  173.         }
  174.     }
  175.     printf("Error: 没有找到要删除的读者!\n");
  176. }

  177. // 查看读者借阅记录
  178. void borrow_history(Reader *reader) {
  179.     printf("%s 的借阅历史:\n", reader->name);
  180.     for(int i=0; i<reader->borrow_amount; i++) {
  181.         printf("%d. %s (%s)\n", i+1, reader->borrow_list[i]->name, reader->borrow_list[i]->author);
  182.     }
  183. }

  184. // 检查读者是否有借书超期未归还的情况
  185. void check_reader_status(Reader *reader) {
  186.     if(reader->borrow_amount == 0) {
  187.         return;
  188.     }
  189.     printf("检查 %s 是否有超期未归还的书籍...\n", reader->name);
  190.     // 模拟检查过程,假设借阅期限为14天,超过14天就算超期
  191.     for(int i=0; i<reader->borrow_amount; i++) {
  192.         if(reader->borrow_list[i]->borrowed == 1) {
  193.             printf("警告:"%s" 已经超期未归还 %d 天!\n", reader->borrow_list[i]->name, 28);
  194.         }
  195.     }
  196. }

  197. // 查找读者
  198. Reader* search_reader(Library *lib, char *name) {
  199.     for(int i=0; i<lib->total_books; i++) {
  200.         if(strcmp(lib->reader_list[i]->name, name)==0) {
  201.             return lib->reader_list[i];
  202.         }
  203.     }
  204.     printf("Error: 没有找到读者 "%s"!\n", name);
  205.     return NULL;
  206. }
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-27 20:34

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表