c语言图书逾期还书系统,急求程序!!!简单图书馆借/还书管理子系统
时间: 2023-08-14 15:05:18 浏览: 56
以下是一个简单的 C 语言图书逾期还书系统的程序,希望能够帮助到你:
```c
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAX_BOOKS 100
struct book {
char title[50];
char author[50];
bool is_borrowed;
int borrowed_days;
};
struct book books[MAX_BOOKS];
int num_books = 0;
void add_book() {
if (num_books >= MAX_BOOKS) {
printf("The library is full.\n");
return;
}
printf("Enter the title of the book: ");
scanf("%s", books[num_books].title);
printf("Enter the author of the book: ");
scanf("%s", books[num_books].author);
books[num_books].is_borrowed = false;
books[num_books].borrowed_days = 0;
num_books++;
printf("The book has been added to the library.\n");
}
void borrow_book() {
char title[50];
printf("Enter the title of the book you want to borrow: ");
scanf("%s", title);
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].title, title) == 0) {
if (books[i].is_borrowed) {
printf("The book is already borrowed.\n");
return;
}
books[i].is_borrowed = true;
books[i].borrowed_days = 0;
printf("You have borrowed the book.\n");
return;
}
}
printf("The book is not in the library.\n");
}
void return_book() {
char title[50];
printf("Enter the title of the book you want to return: ");
scanf("%s", title);
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].title, title) == 0) {
if (!books[i].is_borrowed) {
printf("The book is not borrowed.\n");
return;
}
int overdue_days = books[i].borrowed_days - 30;
if (overdue_days > 0) {
printf("The book is %d days overdue. Please pay $%d.\n", overdue_days, overdue_days * 2);
} else {
printf("Thank you for returning the book.\n");
}
books[i].is_borrowed = false;
books[i].borrowed_days = 0;
return;
}
}
printf("The book is not in the library.\n");
}
void print_books() {
printf("Title\t\tAuthor\t\tBorrowed\n");
for (int i = 0; i < num_books; i++) {
printf("%s\t\t%s\t\t%s\n", books[i].title, books[i].author, books[i].is_borrowed ? "Yes" : "No");
}
}
void update_books() {
for (int i = 0; i < num_books; i++) {
if (books[i].is_borrowed) {
books[i].borrowed_days++;
}
}
}
int main() {
int choice;
do {
printf("1. Add book\n");
printf("2. Borrow book\n");
printf("3. Return book\n");
printf("4. Print books\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
borrow_book();
break;
case 3:
return_book();
break;
case 4:
print_books();
break;
case 5:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
update_books();
} while (choice != 5);
return 0;
}
```
这个程序使用一个结构体数组来存储图书的信息,包括书名、作者、是否被借阅以及借阅天数。在主函数中,使用一个 do-while 循环来读入用户的选择,并调用相应的函数来执行操作。具体来说,用户可以选择添加书籍、借阅书籍、归还书籍、打印书籍列表或退出程序。
当用户借阅一本书时,程序会将这本书的 is_borrowed 属性设置为 true,并将 borrowed_days 属性设为 0。当用户归还一本书时,程序会计算这本书被借阅的天数,如果超过了 30 天,则需要缴纳相应的罚款。注意,这里的罚款是每天 2 元。
这个程序还有许多可以改进的地方,例如可以添加更多的输入检查和错误处理机制,也可以使用链表等数据结构来存储书籍信息。但是这个程序足以满足你的需求,希望能对你有所帮助。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)