C++实现书库管理系统数据结构
时间: 2023-07-08 08:50:41 浏览: 56
以下是一个简单的C++实现书库管理系统的数据结构示例:
```c++
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// 书籍信息数据结构
struct Book {
string name; // 书名
string author; // 作者
string isbn; // ISBN
string publisher; // 出版社
string pub_date; // 出版日期
double price; // 价格
int stock; // 库存数量
};
// 读者信息数据结构
struct Reader {
string name; // 姓名
string gender; // 性别
int age; // 年龄
vector<string> borrow_list; // 借阅记录
};
// 借阅记录数据结构
struct BorrowRecord {
string reader_name; // 读者姓名
string book_name; // 书籍名称
string borrow_date; // 借阅日期
string return_date; // 归还日期
bool borrowed; // 是否已借阅
};
// 书架信息数据结构
struct BookShelf {
int shelf_no; // 书架编号
string location; // 存放位置
int capacity; // 容量
int stored_num; // 已存数量
};
// 书库管理系统类
class Library {
private:
unordered_map<string, Book> book_info; // 书籍信息哈希表
unordered_map<string, Reader> reader_info; // 读者信息哈希表
vector<BorrowRecord> borrow_records; // 借阅记录队列
vector<BookShelf> book_shelves; // 书架信息数组
public:
// 添加书籍信息
void addBook(string isbn, Book book) {
book_info[isbn] = book;
}
// 删除书籍信息
void deleteBook(string isbn) {
book_info.erase(isbn);
}
// 查询书籍信息
Book searchBook(string isbn) {
return book_info[isbn];
}
// 添加读者信息
void addReader(string name, Reader reader) {
reader_info[name] = reader;
}
// 删除读者信息
void deleteReader(string name) {
reader_info.erase(name);
}
// 查询读者信息
Reader searchReader(string name) {
return reader_info[name];
}
// 借阅书籍
void borrowBook(string reader_name, string book_name) {
BorrowRecord record;
record.reader_name = reader_name;
record.book_name = book_name;
record.borrow_date = "2021-12-01";
record.return_date = "2022-01-01";
record.borrowed = true;
borrow_records.push_back(record);
}
// 归还书籍
void returnBook(string reader_name, string book_name) {
for (auto& record : borrow_records) {
if (record.reader_name == reader_name && record.book_name == book_name && record.borrowed == true) {
record.borrowed = false;
break;
}
}
}
// 添加书架信息
void addBookShelf(BookShelf shelf) {
book_shelves.push_back(shelf);
}
// 删除书架信息
void deleteBookShelf(int shelf_no) {
for (int i = 0; i < book_shelves.size(); i++) {
if (book_shelves[i].shelf_no == shelf_no) {
book_shelves.erase(book_shelves.begin() + i);
break;
}
}
}
// 查询书架信息
BookShelf searchBookShelf(int shelf_no) {
for (auto& shelf : book_shelves) {
if (shelf.shelf_no == shelf_no) {
return shelf;
}
}
}
};
int main() {
// 创建书库管理系统对象
Library lib;
// 添加书籍信息
Book book1 = { "C++ Primer", "Lippman", "978-7-121-15535-2", "机械工业出版社", "2021-01-01", 68.00, 100 };
lib.addBook(book1.isbn, book1);
// 查询书籍信息
Book book = lib.searchBook(book1.isbn);
cout << "Book Name: " << book.name << endl;
cout << "Author: " << book.author << endl;
cout << "ISBN: " << book.isbn << endl;
cout << "Publisher: " << book.publisher << endl;
cout << "Publish Date: " << book.pub_date << endl;
cout << "Price: " << book.price << endl;
cout << "Stock: " << book.stock << endl;
// 添加读者信息
Reader reader1 = { "Tom", "Male", 20 };
lib.addReader(reader1.name, reader1);
// 查询读者信息
Reader reader = lib.searchReader(reader1.name);
cout << "Reader Name: " << reader.name << endl;
cout << "Gender: " << reader.gender << endl;
cout << "Age: " << reader.age << endl;
// 借阅书籍
lib.borrowBook(reader1.name, book1.name);
// 归还书籍
lib.returnBook(reader1.name, book1.name);
// 添加书架信息
BookShelf shelf1 = { 1, "A1", 100, 0 };
lib.addBookShelf(shelf1);
// 查询书架信息
BookShelf shelf = lib.searchBookShelf(shelf1.shelf_no);
cout << "Shelf No: " << shelf.shelf_no << endl;
cout << "Location: " << shelf.location << endl;
cout << "Capacity: " << shelf.capacity << endl;
cout << "Stored Num: " << shelf.stored_num << endl;
return 0;
}
```
上述示例中,我们使用了`unordered_map`、`vector`等STL容器来实现书库管理系统的数据结构。例如,`unordered_map<string, Book>`用来存储书籍信息,`vector<BorrowRecord>`用来存储借阅记录,`vector<BookShelf>`用来存储书架信息等。
在`Library`类中,我们实现了添加、删除、查询书籍、读者、书架信息等基本操作,以及借阅、归还书籍等高级操作。使用这些操作,我们可以方便地管理书库的各种信息。