用C++写一个小程序,要求写出完整的代码,该程序允许用户选择5个操作之一:向图书馆添加书,借出书,还书,显示图书馆的当前状态和退出程序.在显示菜单,输入一个正确的数字之后就可以选定操作.当选择退出选项后,就会结束显示菜单和执行所选操作这一循环.
时间: 2023-02-06 07:25:01 浏览: 51
这是一个可能的 C 程序来实现这些功能:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_BOOKS 100
typedef struct {
char title[50];
char author[50];
int borrowed; // 0 表示当前书没有借出,1 表示当前书已借出
} Book;
Book library[MAX_BOOKS];
int num_books = 0;
void add_book() {
if (num_books >= MAX_BOOKS) {
printf("图书馆书架已满,无法添加新书。\n");
return;
}
printf("请输入书名:");
scanf("%s", library[num_books].title);
printf("请输入作者:");
scanf("%s", library[num_books].author);
library[num_books].borrowed = 0;
num_books++;
}
void borrow_book() {
char title[50];
printf("请输入要借的书的书名:");
scanf("%s", title);
for (int i = 0; i < num_books; i++) {
if (!library[i].borrowed && strcmp(library[i].title, title) == 0) {
library[i].borrowed = 1;
printf("成功借出《%s》。\n", title);
return;
}
}
printf("图书馆中没有这本书或者这本书已经被借出。\n");
}
void return_book() {
char title[50];
printf("请输入要还的书的书名:");
scanf("%s", title);
for (int i = 0; i < num_books; i++) {
if (library[i].borrowed && strcmp(library[i].title, title) == 0) {
library[i].borrowed = 0;
printf("成功归还《%s》。\n", title);
return;
}
}
printf("图书馆中没有这本书或者这本书没有被借出。\n");
}
void show_books() {
printf("目前图书馆共有 %d 本书,分别是:\n", num_books);
for (int
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)