急求 pocketbuilder2.5

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 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 元。 这个程序还有许多可以改进的地方,例如可以添加更多的输入检查和错误处理机制,也可以使用链表等数据结构来存储书籍信息。但是这个程序足以满足你的需求,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值