编写一个程序模拟图书管理系统。用户分为管理员和读者两类,分别显示不同文本格式菜单,通过菜单项对应数字进行选择。读者菜单包括借书、还书、查询等功能。管理员菜单包括图书和读者信息录入、修改和删除。图书信息至少应包括:编号、书名、数量,读者信息至少应包括:编号、姓名、所借图书。可根据图书名称或编号进行图书信息查询,可查询某本书现在被哪些读者借走。c++
时间: 2024-03-18 17:41:59 浏览: 51
好的,以下是一个简单的图书管理系统的C++代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 书籍结构体
struct Book {
string id; // 编号
string name; // 书名
int quantity; // 数量
};
// 读者结构体
struct Reader {
string id; // 编号
string name; // 姓名
vector<string> borrowed_books; // 所借图书编号
};
// 全局变量
vector<Book> books;
vector<Reader> readers;
int book_id_count = 0;
int reader_id_count = 0;
// 函数声明
void show_reader_menu();
void show_admin_menu();
void borrow_book();
void return_book();
void search_book();
void add_book();
void edit_book();
void delete_book();
void add_reader();
void edit_reader();
void delete_reader();
void search_borrowers();
// 主函数
int main() {
// 初始化图书和读者
books.push_back({"B001", "C++ Primer", 5});
books.push_back({"B002", "Effective C++", 3});
readers.push_back({"R001", "张三", {"B001", "B002"}});
readers.push_back({"R002", "李四", {"B001"}});
reader_id_count = 2;
// 显示菜单
cout << "欢迎使用图书管理系统" << endl;
while (true) {
cout << endl << "请选择用户类型:" << endl;
cout << "1. 读者" << endl;
cout << "2. 管理员" << endl;
cout << "3. 退出" << endl;
int user_type;
cin >> user_type;
switch (user_type) {
case 1:
show_reader_menu();
break;
case 2:
show_admin_menu();
break;
case 3:
cout << "再见!" << endl;
return 0;
default:
cout << "无效的选项!" << endl;
}
}
}
// 显示读者菜单
void show_reader_menu() {
while (true) {
cout << endl << "请选择操作:" << endl;
cout << "1. 借书" << endl;
cout << "2. 还书" << endl;
cout << "3. 查询图书" << endl;
cout << "4. 查询借阅者" << endl;
cout << "5. 返回" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
borrow_book();
break;
case 2:
return_book();
break;
case 3:
search_book();
break;
case 4:
search_borrowers();
break;
case 5:
return;
default:
cout << "无效的选项!" << endl;
}
}
}
// 显示管理员菜单
void show_admin_menu() {
while (true) {
cout << endl << "请选择操作:" << endl;
cout << "1. 添加图书" << endl;
cout << "2. 修改图书" << endl;
cout << "3. 删除图书" << endl;
cout << "4. 添加读者" << endl;
cout << "5. 修改读者" << endl;
cout << "6. 删除读者" << endl;
cout << "7. 返回" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
add_book();
break;
case 2:
edit_book();
break;
case 3:
delete_book();
break;
case 4:
add_reader();
break;
case 5:
edit_reader();
break;
case 6:
delete_reader();
break;
case 7:
return;
default:
cout << "无效的选项!" << endl;
}
}
}
// 借书
void borrow_book() {
cout << "请输入图书编号:" << endl;
string book_id;
cin >> book_id;
for (Book& book : books) {
if (book.id == book_id) {
if (book.quantity > 0) {
cout << "借书成功!" << endl;
book.quantity--;
for (Reader& reader : readers) {
if (reader.id == "R001") {
reader.borrowed_books.push_back(book_id);
return;
}
}
} else {
cout << "该图书已全部借出,请稍后再试。" << endl;
}
return;
}
}
cout << "输入的图书编号不存在!" << endl;
}
// 还书
void return_book() {
cout << "请输入图书编号:" << endl;
string book_id;
cin >> book_id;
for (Book& book : books) {
if (book.id == book_id) {
cout << "还书成功!" << endl;
book.quantity++;
for (Reader& reader : readers) {
if (reader.id == "R001") {
for (int i = 0; i < reader.borrowed_books.size(); i++) {
if (reader.borrowed_books[i] == book_id) {
reader.borrowed_books.erase(reader.borrowed_books.begin() + i);
return;
}
}
}
}
return;
}
}
cout << "输入的图书编号不存在!" << endl;
}
// 查询图书
void search_book() {
cout << "请选择查询方式:" << endl;
cout << "1. 按编号查询" << endl;
cout << "2. 按书名查询" << endl;
int choice;
cin >> choice;
if (choice == 1) {
cout << "请输入图书编号:" << endl;
string book_id;
cin >> book_id;
for (const Book& book : books) {
if (book.id == book_id) {
cout << "编号:" << book.id << ",书名:" << book.name << ",数量:" << book.quantity << endl;
return;
}
}
cout << "输入的图书编号不存在!" << endl;
} else if (choice == 2) {
cout << "请输入书名关键字:" << endl;
string keyword;
cin >> keyword;
bool found = false;
for (const Book& book : books) {
if (book.name.find(keyword) != string::npos) {
found = true;
cout << "编号:" << book.id << ",书名:" << book.name << ",数量:" << book.quantity << endl;
}
}
if (!found) {
cout << "未找到相关图书!" << endl;
}
} else {
cout << "无效的选项!" << endl;
}
}
// 添加图书
void add_book() {
string id, name;
int quantity;
cout << "请输入图书编号:" << endl;
cin >> id;
cout << "请输入图书名称:" << endl;
cin >> name;
cout << "请输入图书数量:" << endl;
cin >> quantity;
books.push_back({id, name, quantity});
cout << "添加成功!" << endl;
}
// 修改图书
void edit_book() {
cout << "请输入要修改的图书编号:" << endl;
string book_id;
cin >> book_id;
for (Book& book : books) {
if (book.id == book_id) {
cout << "请输入新的图书名称(留空表示不修改):" << endl;
string new_name;
cin >> new_name;
if (!new_name.empty()) {
book.name = new_name;
}
cout << "请输入新的图书数量(留空表示不修改):" << endl;
int new_quantity;
cin >> new_quantity;
if (new_quantity > 0) {
book.quantity = new_quantity;
}
cout << "修改成功!" << endl;
return;
}
}
cout << "输入的图书编号不存在!" << endl;
}
// 删除图书
void delete_book() {
cout << "请输入要删除的图书编号:" << endl;
string book_id;
cin >> book_id;
for (int i = 0; i < books.size(); i++) {
if (books[i].id == book_id) {
books.erase(books.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "输入的图书编号不存在!" << endl;
}
// 添加读者
void add_reader() {
string name;
cout << "请输入读者姓名:" << endl;
cin >> name;
readers.push_back({to_string(++reader_id_count), name, {}});
cout << "添加成功!" << endl;
}
// 修改读者
void edit_reader() {
cout << "请输入要修改的读者编号:" << endl;
string reader_id;
cin >> reader_id;
for (Reader& reader : readers) {
if (reader.id == reader_id) {
cout << "请输入新的读者姓名(留空表示不修改):" << endl;
string new_name;
cin >> new_name;
if (!new_name.empty()) {
reader.name = new_name;
}
cout << "修改成功!" << endl;
return;
}
}
cout << "输入的读者编号不存在!" << endl;
}
// 删除读者
void delete_reader() {
cout << "请输入要删除的读者编号:" << endl;
string reader_id;
cin >> reader_id;
for (int i = 0; i < readers.size(); i++) {
if (readers[i].id == reader_id) {
readers.erase(readers.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "输入的读者编号不存在!" << endl;
}
// 查询借阅者
void search_borrowers() {
cout << "请输入图书编号:" << endl;
string book_id;
cin >> book_id;
for (const Reader& reader : readers) {
for (const string& borrowed_book_id : reader.borrowed_books) {
if (borrowed_book_id == book_id) {
cout << "读者编号:" << reader.id << ",姓名:" << reader.name << endl;
break;
}
}
}
}
```
以上代码实现了以下功能:
- 读者菜单:借书、还书、查询图书、查询借阅者
- 管理员菜单:添加图书、修改图书、删除图书、添加读者、修改读者、删除读者
- 可根据图书名称或编号进行图书信息查询
- 可查询某本书现在被哪些读者借走
注意,以上代码仅供参考,实际应用时还需要根据具体需求进行修改。
阅读全文