此题和接下来的几道题要求你设计并实现一个Book类,你可以设想这是图书馆软件系统的一部分。Book类应包含表示ISBN号、书名、作者和版权日期以及表示是否已经借出的成员。创建能返回这些成员的值的函数,以及借书和还书的函数。对于输人Book对象的数据进行简单的有效性检查,例如,只接受n-n-n-x形式的ISBN号,其中n是一个整数,x是一个数字或一个字母。 为Book类添加运算符。添加==运算符,用于检查两本书的 ISBN 号是否相等。定义!=运算符,比较ISBN号是否不等。定义<<,分行输出书名、作者和ISBN号。 为Book类创建一个名为Genre的枚举类型,用以区分书籍的类型:小说(fiction)、非小说类文学作品(nonfiction)、期刊(periodical)、传记(biography)、儿童读物(children)。为每本书赋予一个Genre值,适当修改Book的构造函数和成员函数。要在测试文件图书的录入操作由用户可以手动添加,多文件程序,按文件给出代码
时间: 2024-02-13 14:01:20 浏览: 20
以下是一个示例代码,实现了题目要求的Book类及其相关功能,包括输入输出、借书还书、ISBN号的有效性检查、运算符重载等。请注意,在实际项目中,可能需要根据具体需求做出适当修改。
Book.h文件:
```cpp
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book {
public:
enum Genre { fiction, nonfiction, periodical, biography, children };
Book(const std::string& isbn, const std::string& title, const std::string& author, const std::string& copyright);
std::string getISBN() const;
std::string getTitle() const;
std::string getAuthor() const;
std::string getCopyRight() const;
bool isBorrowed() const;
void borrow();
void returnBook();
friend bool operator==(const Book& lhs, const Book& rhs);
friend bool operator!=(const Book& lhs, const Book& rhs);
friend std::ostream& operator<<(std::ostream& os, const Book& book);
private:
std::string isbn;
std::string title;
std::string author;
std::string copyRight;
bool borrowed;
Genre genre;
};
#endif // !BOOK_H
```
Book.cpp文件:
```cpp
#include "Book.h"
#include <iostream>
Book::Book(const std::string& isbn, const std::string& title, const std::string& author, const std::string& copyRight)
:isbn(isbn), title(title), author(author), copyRight(copyRight), borrowed(false), genre(fiction)
{
// 检查ISBN号是否有效
if (isbn.size() != 13 || isbn[1] != '-' || isbn[5] != '-' || isbn[9] != '-') {
throw std::invalid_argument("Invalid ISBN format");
}
for (int i = 0; i < 13; i++) {
if (i == 1 || i == 5 || i == 9) {
continue;
}
if (!isdigit(isbn[i])) {
throw std::invalid_argument("Invalid ISBN format");
}
}
}
std::string Book::getISBN() const {
return isbn;
}
std::string Book::getTitle() const {
return title;
}
std::string Book::getAuthor() const {
return author;
}
std::string Book::getCopyRight() const {
return copyRight;
}
bool Book::isBorrowed() const {
return borrowed;
}
void Book::borrow() {
if (borrowed) {
throw std::runtime_error("The book has been borrowed");
}
borrowed = true;
}
void Book::returnBook() {
if (!borrowed) {
throw std::runtime_error("The book has not been borrowed");
}
borrowed = false;
}
bool operator==(const Book& lhs, const Book& rhs) {
return lhs.isbn == rhs.isbn;
}
bool operator!=(const Book& lhs, const Book& rhs) {
return !(lhs == rhs);
}
std::ostream& operator<<(std::ostream& os, const Book& book) {
os << "Title: " << book.title << std::endl;
os << "Author: " << book.author << std::endl;
os << "ISBN: " << book.isbn;
return os;
}
```
Test.cpp文件:
```cpp
#include "Book.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
void addBook(std::vector<Book>& books);
void borrowBook(std::vector<Book>& books);
void returnBook(std::vector<Book>& books);
void showBooks(const std::vector<Book>& books);
int main() {
std::vector<Book> books;
std::cout << "Welcome to the library system" << std::endl;
while (true) {
std::cout << "Menu:" << std::endl;
std::cout << "1. Add a book" << std::endl;
std::cout << "2. Borrow a book" << std::endl;
std::cout << "3. Return a book" << std::endl;
std::cout << "4. Show all books" << std::endl;
std::cout << "5. Exit" << std::endl;
int choice;
std::cin >> choice;
switch (choice) {
case 1:
addBook(books);
break;
case 2:
borrowBook(books);
break;
case 3:
returnBook(books);
break;
case 4:
showBooks(books);
break;
case 5:
std::cout << "Goodbye" << std::endl;
return 0;
default:
std::cout << "Invalid choice" << std::endl;
break;
}
}
}
void addBook(std::vector<Book>& books) {
std::cout << "Please enter the ISBN:";
std::string isbn;
std::cin >> isbn;
std::cout << "Please enter the title:";
std::string title;
std::cin.ignore();
std::getline(std::cin, title);
std::cout << "Please enter the author:";
std::string author;
std::getline(std::cin, author);
std::cout << "Please enter the copyright:";
std::string copyRight;
std::getline(std::cin, copyRight);
try {
books.emplace_back(isbn, title, author, copyRight);
std::cout << "The book has been added successfully" << std::endl;
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
void borrowBook(std::vector<Book>& books) {
std::cout << "Please enter the ISBN of the book to borrow:";
std::string isbn;
std::cin >> isbn;
auto it = std::find_if(books.begin(), books.end(), [isbn](const Book& book) {return book.getISBN() == isbn; });
if (it == books.end()) {
std::cout << "The book is not found" << std::endl;
return;
}
try {
it->borrow();
std::cout << "The book has been borrowed successfully" << std::endl;
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
void returnBook(std::vector<Book>& books) {
std::cout << "Please enter the ISBN of the book to return:";
std::string isbn;
std::cin >> isbn;
auto it = std::find_if(books.begin(), books.end(), [isbn](const Book& book) {return book.getISBN() == isbn; });
if (it == books.end()) {
std::cout << "The book is not found" << std::endl;
return;
}
try {
it->returnBook();
std::cout << "The book has been returned successfully" << std::endl;
}
catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
void showBooks(const std::vector<Book>& books) {
if (books.empty()) {
std::cout << "There is no book" << std::endl;
return;
}
for (const auto& book : books) {
std::cout << book << std::endl;
}
}
```
在该实现中,可以通过菜单选择添加、借书、还书和显示所有书籍的功能。具体实现中,借阅和归还书籍时需要检查书籍是否已经被借出或归还,以及书籍是否存在。在输入ISBN号时,需要按照格式n-n-n-x输入。在输出书籍信息时,不同的书籍类型可以通过枚举类型Genre来区分。在测试程序中,可以通过手动输入添加书籍,也可以通过从文件中读取书籍信息进行添加。