C++图书馆管理系统
时间: 2023-12-19 13:05:42 浏览: 29
以下是一个简单的C++图书馆管理系统的实现,包括书籍类和借还类:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Book {
private:
unsigned int id; // 书籍编号
string name; // 书名
string author; // 作者
string publisher; // 出版社
bool isBorrowed; // 是否被借出
public:
Book(unsigned int id, string name, string author, string publisher) {
this->id = id;
this->name = name;
this->author = author;
this->publisher = publisher;
this->isBorrowed = false;
}
unsigned int getId() const {
return id;
}
string getName() const {
return name;
}
string getAuthor() const {
return author;
}
string getPublisher() const {
return publisher;
}
bool getIsBorrowed() const {
return isBorrowed;
}
void setIsBorrowed(bool isBorrowed) {
this->isBorrowed = isBorrowed;
}
};
class BorrowReturn {
private:
unsigned int bookId; // 书籍编号
unsigned int readerId; // 读者编号
string borrowTime; // 借书时间
string returnTime; // 还书时间
public:
BorrowReturn(unsigned int bookId, unsigned int readerId, string borrowTime, string returnTime) {
this->bookId = bookId;
this->readerId = readerId;
this->borrowTime = borrowTime;
this->returnTime = returnTime;
}
unsigned int getBookId() const {
return bookId;
}
unsigned int getReaderId() const {
return readerId;
}
string getBorrowTime() const {
return borrowTime;
}
string getReturnTime() const {
return returnTime;
}
};
class Library {
private:
vector<Book> books; // 书籍列表
vector<BorrowReturn> borrowReturns; // 借还记录列表
public:
void addBook(Book book) {
books.push_back(book);
}
void deleteBook(unsigned int id) {
for (auto it = books.begin(); it != books.end(); it++) {
if (it->getId() == id) {
it->setIsBorrowed(true); // 标记删除
break;
}
}
}
void updateBook(Book book) {
for (auto it = books.begin(); it != books.end(); it++) {
if (it->getId() == book.getId()) {
*it = book;
break;
}
}
}
void borrowBook(unsigned int bookId, unsigned int readerId, string borrowTime) {
for (auto it = books.begin(); it != books.end(); it++) {
if (it->getId() == bookId) {
it->setIsBorrowed(true);
break; }
}
borrowReturns.push_back(BorrowReturn(bookId, readerId, borrowTime, ""));
}
void returnBook(unsigned int bookId, string returnTime) {
for (auto it = books.begin(); it != books.end(); it++) {
if (it->getId() == bookId) {
it->setIsBorrowed(false);
break; }
}
for (auto it = borrowReturns.begin(); it != borrowReturns.end(); it++) {
if (it->getBookId() == bookId && it->getReturnTime() == "") {
it->setReturnTime(returnTime);
break;
}
}
}
vector<Book> searchBooksByTime(string time) {
vector<Book> result;
for (auto it = borrowReturns.begin(); it != borrowReturns.end(); it++) {
if (it->getBorrowTime() == time) {
for (auto it2 = books.begin(); it2 != books.end(); it2++) {
if (it2->getId() == it->getBookId()) {
result.push_back(*it2);
break;
}
}
}
}
return result;
}
vector<Book> searchBooksById(unsigned int id) {
vector<Book> result;
for (auto it = books.begin(); it != books.end(); it++) {
if (it->getId() == id) {
result.push_back(*it);
break;
}
}
return result;
}
vector<Book> searchBooksByReaderId(unsigned int readerId) {
vector<Book> result;
for (auto it = borrowReturns.begin(); it != borrowReturns.end(); it++) {
if (it->getReaderId() == readerId) {
for (auto it2 = books.begin(); it2 != books.end(); it2++) {
if (it2->getId() == it->getBookId()) {
result.push_back(*it2);
break;
}
}
}
}
return result;
}
};
int main() {
Library library;
Book book1(1, "C++ Primer", "Lippman", "O'Reilly");
Book book2(2, "Effective C++", "Meyers", "Addison-Wesley");
library.addBook(book1);
library.addBook(book2);
library.deleteBook(2);
Book book3(3, "Thinking in Java", "Eckel", "Prentice Hall");
library.updateBook(book3);
library.borrowBook(1, 1, "2021-01-01");
library.returnBook(1, "2021-01-10");
vector<Book> books1 = library.searchBooksByTime("2021-01-01");
vector<Book> books2 = library.searchBooksById(1);
vector<Book> books3 = library.searchBooksByReaderId(1);
return 0;
}
```