此题和接下来的几道题要求你设计并实现一个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 07:01:09 浏览: 171
定义一个Book类,实现简单操作实验报告
4星 · 用户满意度95%
好的,我可以帮你设计并实现一个Book类。以下是代码实现:
Book.h文件:
```
#ifndef BOOK_H
#define BOOK_H
#include <string>
enum Genre { FICTION, NONFICTION, PERIODICAL, BIOGRAPHY, CHILDREN };
class Book {
private:
std::string isbn;
std::string title;
std::string author;
std::string copyrightDate;
bool isBorrowed;
Genre genre;
public:
Book(std::string isbn, std::string title, std::string author, std::string copyrightDate,
bool isBorrowed, Genre genre);
std::string getIsbn() const;
std::string getTitle() const;
std::string getAuthor() const;
std::string getCopyrightDate() const;
bool getIsBorrowed() const;
Genre getGenre() const;
void setIsBorrowed(bool isBorrowed);
void borrow();
void returnBook();
bool isValidISBN(std::string isbn);
};
bool operator==(const Book& b1, const Book& b2);
bool operator!=(const Book& b1, const Book& b2);
std::ostream& operator<<(std::ostream& os, const Book& b);
#endif
```
Book.cpp文件:
```
#include "Book.h"
#include <iostream>
Book::Book(std::string isbn, std::string title, std::string author, std::string copyrightDate,
bool isBorrowed, Genre genre)
: isbn(isbn), title(title), author(author), copyrightDate(copyrightDate),
isBorrowed(isBorrowed), genre(genre) {}
std::string Book::getIsbn() const {
return isbn;
}
std::string Book::getTitle() const {
return title;
}
std::string Book::getAuthor() const {
return author;
}
std::string Book::getCopyrightDate() const {
return copyrightDate;
}
bool Book::getIsBorrowed() const {
return isBorrowed;
}
Genre Book::getGenre() const {
return genre;
}
void Book::setIsBorrowed(bool isBorrowed) {
this->isBorrowed = isBorrowed;
}
void Book::borrow() {
isBorrowed = true;
}
void Book::returnBook() {
isBorrowed = false;
}
bool Book::isValidISBN(std::string isbn) {
if (isbn.length() != 9)
return false;
for (int i = 0; i < 3; i++) {
if (!isdigit(isbn[i]))
return false;
}
if (isbn[3] != '-')
return false;
for (int i = 4; i < 7; i++) {
if (!isdigit(isbn[i]))
return false;
}
if (isbn[7] != '-')
return false;
if (!isalnum(isbn[8]))
return false;
return true;
}
bool operator==(const Book& b1, const Book& b2) {
return b1.getIsbn() == b2.getIsbn();
}
bool operator!=(const Book& b1, const Book& b2) {
return b1.getIsbn() != b2.getIsbn();
}
std::ostream& operator<<(std::ostream& os, const Book& b) {
os << "Title: " << b.getTitle() << std::endl;
os << "Author: " << b.getAuthor() << std::endl;
os << "ISBN: " << b.getIsbn() << std::endl;
return os;
}
```
main.cpp文件:
```
#include "Book.h"
#include <iostream>
#include <fstream>
#include <vector>
void addBook(std::vector<Book>& books) {
std::string isbn, title, author, copyright;
int genre;
std::cout << "Enter the ISBN (format n-n-n-x): ";
std::cin >> isbn;
while (!Book::isValidISBN(isbn)) {
std::cout << "Invalid ISBN format. Please try again." << std::endl;
std::cout << "Enter the ISBN (format n-n-n-x): ";
std::cin >> isbn;
}
std::cout << "Enter the title: ";
std::cin >> title;
std::cout << "Enter the author: ";
std::cin >> author;
std::cout << "Enter the copyright date: ";
std::cin >> copyright;
std::cout << "Enter the genre (0 for fiction, 1 for nonfiction, 2 for periodical, 3 for biography, 4 for children): ";
std::cin >> genre;
books.push_back(Book(isbn, title, author, copyright, false, static_cast<Genre>(genre)));
}
void saveBooks(std::vector<Book>& books) {
std::ofstream fout("books.txt");
for (const auto& b : books) {
fout << b.getIsbn() << " " << b.getTitle() << " " << b.getAuthor() << " " << b.getCopyrightDate()
<< " " << b.getIsBorrowed() << " " << b.getGenre() << std::endl;
}
fout.close();
}
void loadBooks(std::vector<Book>& books) {
std::ifstream fin("books.txt");
if (fin) {
std::string line;
while (getline(fin, line)) {
std::istringstream iss(line);
std::string isbn, title, author, copyright;
bool isBorrowed;
int genre;
iss >> isbn >> title >> author >> copyright >> isBorrowed >> genre;
books.push_back(Book(isbn, title, author, copyright, isBorrowed, static_cast<Genre>(genre)));
}
}
fin.close();
}
int main() {
std::vector<Book> books;
loadBooks(books);
int choice;
do {
std::cout << "1. Add book" << std::endl;
std::cout << "2. Borrow book" << std::endl;
std::cout << "3. Return book" << std::endl;
std::cout << "4. List all books" << std::endl;
std::cout << "5. Save and exit" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
addBook(books);
break;
case 2: {
std::string isbn;
std::cout << "Enter the ISBN of the book to borrow: ";
std::cin >> isbn;
auto it = std::find_if(books.begin(), books.end(), [isbn](const Book& b) { return b.getIsbn() == isbn; });
if (it != books.end()) {
if (it->getIsBorrowed()) {
std::cout << "This book is already borrowed." << std::endl;
} else {
it->borrow();
std::cout << "Book borrowed successfully." << std::endl;
}
} else {
std::cout << "Book not found." << std::endl;
}
break;
}
case 3: {
std::string isbn;
std::cout << "Enter the ISBN of the book to return: ";
std::cin >> isbn;
auto it = std::find_if(books.begin(), books.end(), [isbn](const Book& b) { return b.getIsbn() == isbn; });
if (it != books.end()) {
if (!it->getIsBorrowed()) {
std::cout << "This book is not borrowed." << std::endl;
} else {
it->returnBook();
std::cout << "Book returned successfully." << std::endl;
}
} else {
std::cout << "Book not found." << std::endl;
}
break;
}
case 4:
for (const auto& b : books) {
std::cout << b;
}
break;
case 5:
saveBooks(books);
std::cout << "Exiting program." << std::endl;
break;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
break;
}
std::cout << std::endl;
} while (choice != 5);
return 0;
}
```
books.txt文件:
```
123-456-7A The Great Gatsby F. Scott Fitzgerald 1925 0 0
456-789-0B To Kill a Mockingbird Harper Lee 1960 0 0
789-012-3C The Catcher in the Rye J.D. Salinger 1951 1 0
```
注意:以上代码只是一个简单的示例,实际应用中还需要更完善的错误处理和其他功能。
阅读全文