写一个C++图书管理系统,管理员可以添加删除修改书籍,读者可以借阅归还书,查看借阅记录,分为reader.cpp,reader.h,book.h,book.h,bookmanager.cpp,bookmananger.h
时间: 2024-02-25 13:54:44 浏览: 144
好的,我可以帮你编写一个简单的图书管理系统,分为管理员和读者两个角色。首先,我们需要定义一些基本的数据结构和功能:
book.h:
```c++
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book {
public:
std::string title;
std::string author;
std::string ISBN;
bool isBorrowed;
Book(std::string title, std::string author, std::string ISBN, bool isBorrowed = false);
void borrow();
void returnBook();
};
#endif
```
book.cpp:
```c++
#include "book.h"
Book::Book(std::string title, std::string author, std::string ISBN, bool isBorrowed) {
this->title = title;
this->author = author;
this->ISBN = ISBN;
this->isBorrowed = isBorrowed;
}
void Book::borrow() {
this->isBorrowed = true;
}
void Book::returnBook() {
this->isBorrowed = false;
}
```
bookmanager.h:
```c++
#ifndef BOOKMANAGER_H
#define BOOKMANAGER_H
#include <vector>
#include "book.h"
class BookManager {
private:
std::vector<Book> books;
public:
BookManager();
void addBook(std::string title, std::string author, std::string ISBN);
void removeBook(std::string ISBN);
void modifyBook(std::string ISBN, std::string newTitle, std::string newAuthor);
Book& findBook(std::string ISBN);
std::vector<Book>& getBooks();
};
#endif
```
bookmanager.cpp:
```c++
#include "bookmanager.h"
BookManager::BookManager() {}
void BookManager::addBook(std::string title, std::string author, std::string ISBN) {
books.push_back(Book(title, author, ISBN));
}
void BookManager::removeBook(std::string ISBN) {
for (auto it = books.begin(); it != books.end(); ++it) {
if (it->ISBN == ISBN) {
books.erase(it);
break;
}
}
}
void BookManager::modifyBook(std::string ISBN, std::string newTitle, std::string newAuthor) {
for (auto it = books.begin(); it != books.end(); ++it) {
if (it->ISBN == ISBN) {
it->title = newTitle;
it->author = newAuthor;
break;
}
}
}
Book& BookManager::findBook(std::string ISBN) {
for (auto& book : books) {
if (book.ISBN == ISBN) {
return book;
}
}
throw "Book not found";
}
std::vector<Book>& BookManager::getBooks() {
return books;
}
```
reader.h:
```c++
#ifndef READER_H
#define READER_H
#include <vector>
#include "book.h"
class Reader {
private:
std::vector<Book*> borrowedBooks;
public:
void borrowBook(Book& book);
void returnBook(Book& book);
std::vector<Book*> getBorrowedBooks();
bool isBorrowed(Book& book);
};
#endif
```
reader.cpp:
```c++
#include "reader.h"
void Reader::borrowBook(Book& book) {
if (book.isBorrowed) {
throw "Book is already borrowed";
}
book.borrow();
borrowedBooks.push_back(&book);
}
void Reader::returnBook(Book& book) {
book.returnBook();
for (auto it = borrowedBooks.begin(); it != borrowedBooks.end(); ++it) {
if (*it == &book) {
borrowedBooks.erase(it);
break;
}
}
}
std::vector<Book*> Reader::getBorrowedBooks() {
return borrowedBooks;
}
bool Reader::isBorrowed(Book& book) {
for (auto& borrowedBook : borrowedBooks) {
if (borrowedBook == &book) {
return true;
}
}
return false;
}
```
现在我们可以编写一个测试程序:
```c++
#include <iostream>
#include "book.h"
#include "bookmanager.h"
#include "reader.h"
int main() {
BookManager bookManager;
bookManager.addBook("C++ Primer", "Lippman, Lajoie, and Moo", "978-7-121-34008-9");
bookManager.addBook("Effective C++", "Scott Meyers", "978-7-121-29027-1");
Reader reader;
try {
Book& cplusplus = bookManager.findBook("978-7-121-34008-9");
reader.borrowBook(cplusplus);
std::cout << "Borrowed book: " << cplusplus.title << std::endl;
} catch (const char* msg) {
std::cerr << msg << std::endl;
}
try {
Book& effectivecpp = bookManager.findBook("978-7-121-29027-1");
reader.borrowBook(effectivecpp);
std::cout << "Borrowed book: " << effectivecpp.title << std::endl;
} catch (const char* msg) {
std::cerr << msg << std::endl;
}
try {
Book& cplusplus = bookManager.findBook("978-7-121-34008-9");
reader.returnBook(cplusplus);
std::cout << "Returned book: " << cplusplus.title << std::endl;
} catch (const char* msg) {
std::cerr << msg << std::endl;
}
for (auto& book : bookManager.getBooks()) {
std::cout << book.title << " by " << book.author << " [" << book.ISBN << "] ";
if (book.isBorrowed) {
std::cout << "(borrowed)" << std::endl;
} else {
std::cout << "(available)" << std::endl;
}
}
for (auto& borrowedBook : reader.getBorrowedBooks()) {
std::cout << "Borrowed book: " << borrowedBook->title << std::endl;
}
return 0;
}
```
这个程序会输出以下内容:
```
Borrowed book: C++ Primer
Borrowed book: Effective C++
Returned book: C++ Primer
C++ Primer by Lippman, Lajoie, and Moo [978-7-121-34008-9] (available)
Effective C++ by Scott Meyers [978-7-121-29027-1] (borrowed)
Borrowed book: Effective C++
```
这个测试程序实现了以下功能:
- 管理员可以添加和删除书籍,以及修改书籍的标题和作者;
- 读者可以借阅和归还书籍,并查看自己的借阅记录;
- 读者不能借阅已被借出的书籍。
阅读全文