运用C++语言描述书籍类、图书类型类、图书类。设计基类和派生类。要求设计具有继承与派生以及多态性的类。帮我编写一个图书管理系统,具有这些功能: 1.建立书籍类、图书类型类、图书类 2.书籍、图书类型、图书信息的初始化 3.书籍、图书类型、图书信息的添加、修改、删除、查找 4.图书信息的输出 5.对图书信息按编号、书名排序 6.将图书信息保存为文件 7.读入文件信息
时间: 2023-08-11 14:08:38 浏览: 109
基于C++的控制台图书管理系统,可提供管理员登录和用户登录两种模式,管理员模式登录后可对图书的相关信息进行管理
好的,我将为您编写一个基于C++的图书管理系统,满足您的需求。
我们先定义三个类:书籍类 Book,图书类型类 BookType,图书类 Library。
```cpp
class Book {
private:
int id; // 书籍编号
string name; // 书籍名称
string author; // 作者
string publisher; // 出版社
BookType* bookType; // 书籍类型
public:
Book(int id = 0, string name = "", string author = "", string publisher = "", BookType* bookType = nullptr);
int getId() const;
string getName() const;
string getAuthor() const;
string getPublisher() const;
BookType* getBookType() const;
void setBookType(BookType* bookType);
};
class BookType {
private:
int id; // 图书类型编号
string name; // 图书类型名称
public:
BookType(int id = 0, string name = "");
int getId() const;
string getName() const;
};
class Library {
private:
vector<Book> books; // 图书信息
vector<BookType> types; // 图书类型信息
public:
void init(); // 初始化图书和图书类型
void addBook(); // 添加图书
void editBook(); // 修改图书
void deleteBook(); // 删除图书
void searchBook(); // 查找图书
void listBooks(); // 输出图书列表
void sortByNumber(); // 按编号排序
void sortByName(); // 按书名排序
void saveToFile(); // 将图书信息保存到文件
void readFromFile(); // 从文件中读取图书信息
};
```
`Book` 类表示一本书,包含书籍编号、书籍名称、作者、出版社和图书类型。`BookType` 类表示图书类型,包含图书类型编号和图书类型名称。`Library` 类表示图书管理系统,包含一个 `vector` 类型的图书信息和图书类型信息。
下面是具体实现:
```cpp
// Book 类的具体实现
Book::Book(int id, string name, string author, string publisher, BookType* bookType) {
this->id = id;
this->name = name;
this->author = author;
this->publisher = publisher;
this->bookType = bookType;
}
int Book::getId() const {
return id;
}
string Book::getName() const {
return name;
}
string Book::getAuthor() const {
return author;
}
string Book::getPublisher() const {
return publisher;
}
BookType* Book::getBookType() const {
return bookType;
}
void Book::setBookType(BookType* bookType) {
this->bookType = bookType;
}
// BookType 类的具体实现
BookType::BookType(int id, string name) {
this->id = id;
this->name = name;
}
int BookType::getId() const {
return id;
}
string BookType::getName() const {
return name;
}
// Library 类的具体实现
void Library::init() {
types.emplace_back(1, "小说");
types.emplace_back(2, "散文");
types.emplace_back(3, "诗歌");
BookType* type1 = &types[0];
BookType* type2 = &types[1];
BookType* type3 = &types[2];
books.emplace_back(1, "红楼梦", "曹雪芹", "人民文学出版社", type1);
books.emplace_back(2, "西游记", "吴承恩", "人民文学出版社", type1);
books.emplace_back(3, "水浒传", "施耐庵", "人民文学出版社", type1);
books.emplace_back(4, "儒林外史", "吴敬梓", "人民文学出版社", type2);
books.emplace_back(5, "纪录片", "方舟子", "中国青年出版社", type2);
books.emplace_back(6, "静夜思", "李白", "人民文学出版社", type3);
}
void Library::addBook() {
int id;
string name, author, publisher;
int typeId;
cout << "请输入书籍编号:";
cin >> id;
cout << "请输入书籍名称:";
cin >> name;
cout << "请输入作者:";
cin >> author;
cout << "请输入出版社:";
cin >> publisher;
cout << "请输入书籍类型编号:";
cin >> typeId;
for (auto& type : types) {
if (type.getId() == typeId) {
BookType* bookType = &type;
books.emplace_back(id, name, author, publisher, bookType);
cout << "成功添加图书!" << endl;
return;
}
}
cout << "未找到该图书类型,请先添加图书类型!" << endl;
}
void Library::editBook() {
int id;
cout << "请输入要修改的书籍编号:";
cin >> id;
for (auto& book : books) {
if (book.getId() == id) {
string name, author, publisher;
int typeId;
cout << "请输入书籍名称:";
cin >> name;
cout << "请输入作者:";
cin >> author;
cout << "请输入出版社:";
cin >> publisher;
cout << "请输入书籍类型编号:";
cin >> typeId;
for (auto& type : types) {
if (type.getId() == typeId) {
BookType* bookType = &type;
book.setBookType(bookType);
book = Book(id, name, author, publisher, bookType);
cout << "成功修改图书!" << endl;
return;
}
}
cout << "未找到该图书类型,请先添加图书类型!" << endl;
return;
}
}
cout << "未找到该书籍!" << endl;
}
void Library::deleteBook() {
int id;
cout << "请输入要删除的书籍编号:";
cin >> id;
for (auto it = books.begin(); it != books.end(); ++it) {
if (it->getId() == id) {
books.erase(it);
cout << "成功删除图书!" << endl;
return;
}
}
cout << "未找到该书籍!" << endl;
}
void Library::searchBook() {
int choice;
cout << "1.按书籍编号查找\n2.按书籍名称查找\n请选择:";
cin >> choice;
if (choice == 1) {
int id;
cout << "请输入书籍编号:";
cin >> id;
for (auto& book : books) {
if (book.getId() == id) {
cout << "书籍编号:" << book.getId() << endl;
cout << "书籍名称:" << book.getName() << endl;
cout << "作者:" << book.getAuthor() << endl;
cout << "出版社:" << book.getPublisher() << endl;
cout << "书籍类型编号:" << book.getBookType()->getId() << endl;
cout << "书籍类型名称:" << book.getBookType()->getName() << endl;
return;
}
}
cout << "未找到该书籍!" << endl;
}
else if (choice == 2) {
string name;
cout << "请输入书籍名称:";
cin >> name;
for (auto& book : books) {
if (book.getName() == name) {
cout << "书籍编号:" << book.getId() << endl;
cout << "书籍名称:" << book.getName() << endl;
cout << "作者:" << book.getAuthor() << endl;
cout << "出版社:" << book.getPublisher() << endl;
cout << "书籍类型编号:" << book.getBookType()->getId() << endl;
cout << "书籍类型名称:" << book.getBookType()->getName() << endl;
return;
}
}
cout << "未找到该书籍!" << endl;
}
}
void Library::listBooks() {
cout << "书籍编号\t书籍名称\t作者\t出版社\t书籍类型编号\t书籍类型名称" << endl;
for (auto& book : books) {
cout << book.getId() << "\t\t" << book.getName() << "\t\t" << book.getAuthor() << "\t\t" << book.getPublisher() << "\t\t" << book.getBookType()->getId() << "\t\t" << book.getBookType()->getName() << endl;
}
}
void Library::sortByNumber() {
sort(books.begin(), books.end(), [](const Book& a, const Book& b) {
return a.getId() < b.getId();
});
}
void Library::sortByName() {
sort(books.begin(), books.end(), [](const Book& a, const Book& b) {
return a.getName() < b.getName();
});
}
void Library::saveToFile() {
ofstream outFile("books.txt");
if (outFile.is_open()) {
for (auto& book : books) {
outFile << book.getId() << "," << book.getName() << "," << book.getAuthor() << "," << book.getPublisher() << "," << book.getBookType()->getId() << endl;
}
cout << "成功保存图书信息到文件 books.txt!" << endl;
}
else {
cout << "无法打开文件 books.txt!" << endl;
}
outFile.close();
}
void Library::readFromFile() {
ifstream inFile("books.txt");
if (inFile.is_open()) {
books.clear();
string line;
while (getline(inFile, line)) {
stringstream ss(line);
string str;
int id, typeId;
string name, author, publisher;
getline(ss, str, ',');
id = stoi(str);
getline(ss, name, ',');
getline(ss, author, ',');
getline(ss, publisher, ',');
getline(ss, str);
typeId = stoi(str);
for (auto& type : types) {
if (type.getId() == typeId) {
BookType* bookType = &type;
books.emplace_back(id, name, author, publisher, bookType);
break;
}
}
}
cout << "成功从文件 books.txt 读取图书信息!" << endl;
}
else {
cout << "无法打开文件 books.txt!" << endl;
}
inFile.close();
}
```
在 `init()` 函数中,我们初始化了一些书籍和图书类型。在 `addBook()` 函数中,我们首先输入书籍信息,然后判断输入的图书类型是否存在,如果存在,就添加该图书到 `books` 中。在 `editBook()` 函数中,我们首先输入要修改的书籍编号,然后遍历 `books` 列表找到该书籍,再输入新的书籍信息,最后修改该书籍的信息。在 `deleteBook()` 函数中,我们首先输入要删除的书籍编号,然后遍历 `books` 列表找到该书籍,最后删除该书籍。在 `searchBook()` 函数中,我们可以选择按书籍编号或者按书籍名称来查找书籍,找到该书籍之后输出书籍信息。在 `listBooks()` 函数中,我们遍历 `books` 列表输出所有书籍信息。在 `sortByNumber()` 和 `sortByName()` 函数中,我们分别按书籍编号和书籍名称进行排序。在 `saveToFile()` 函数中,我们将 `books` 列表中的所有书籍信息保存到文件 `books.txt` 中。在 `readFromFile()` 函数中,我们从文件 `books.txt` 中读取图书信息。
现在我们可以在 `main()` 函数中调用上面的函数来实现图书管理系统了。
```cpp
int main() {
Library library;
library.init();
while (true) {
cout << "1.添加图书\n2.修改图书\n3.删除图书\n4.查找图书\n5.输出图书列表\n6.按编号排序\n7.按书名排序\n8.将图书信息保存到文件\n9.从文件中读取图书信息\n0.退出\n请选择:";
int choice;
cin >> choice;
switch (choice) {
case 1:
library.addBook();
break;
case 2:
library.editBook();
break;
case 3:
library.deleteBook();
break;
case 4:
library.searchBook();
break;
case 5:
library.listBooks();
break;
case 6:
library.sortByNumber();
break;
case 7:
library.sortByName();
break;
case 8:
library.saveToFile();
break;
case 9:
library.readFromFile();
break;
case 0:
return 0;
default:
cout << "无效的选项!" << endl;
}
}
return 0;
}
```
以上就是一个简单的图书管理系统的实现。
阅读全文