帮我用C++语言实现一个的图书管理系统,在控制台进行操作,代码需要有详细的代码注释,要有书籍类、图书类型类、图书类,系统要有这些功能: 1)建立书籍类、图书类型类、图书类 2)书籍、图书类型、图书信息的初始化 3)书籍、图书类型、图书信息的添加、修改、删除、查找 4)图书信息的输出 5)对图书信息按编号、书名排序 6)将图书信息保存为文件 7)读入保存的图书信息
时间: 2023-08-11 10:04:13 浏览: 127
好的,这个任务比较复杂,我们需要分步骤来完成,下面是一个详细的实现代码及注释:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
// 定义图书类型类
class BookType {
public:
string type; // 图书类型
int borrowDays; // 借阅周期
BookType(string type, int borrowDays) { // 构造函数
this->type = type;
this->borrowDays = borrowDays;
}
};
// 定义书籍类
class Book {
public:
string name; // 书名
string author; // 作者
string ISBN; // ISBN号
double price; // 价格
BookType* type; // 图书类型
Book(string name, string author, string ISBN, double price, BookType* type) { // 构造函数
this->name = name;
this->author = author;
this->ISBN = ISBN;
this->price = price;
this->type = type;
}
void displayInfo() { // 输出书籍信息
cout << "书名:" << name << endl;
cout << "作者:" << author << endl;
cout << "ISBN号:" << ISBN << endl;
cout << "价格:" << price << " 元" << endl;
cout << "图书类型:" << type->type << endl;
cout << "借阅周期:" << type->borrowDays << " 天" << endl;
}
};
// 定义图书管理系统类
class BookManager {
private:
vector<Book*> books; // 存储所有书籍的指针
vector<BookType*> types; // 存储所有图书类型的指针
public:
void init() { // 初始化图书信息
BookType* type1 = new BookType("小说", 30);
types.push_back(type1);
BookType* type2 = new BookType("科技", 60);
types.push_back(type2);
Book* book1 = new Book("红楼梦", "曹雪芹", "9787532735807", 15.5, type1);
books.push_back(book1);
Book* book2 = new Book("三体", "刘慈欣", "9787536692930", 28.8, type2);
books.push_back(book2);
}
void addBook() { // 添加书籍信息
cout << "请输入书名:";
string name;
cin >> name;
cout << "请输入作者:";
string author;
cin >> author;
cout << "请输入ISBN号:";
string ISBN;
cin >> ISBN;
cout << "请输入价格:";
double price;
cin >> price;
cout << "请选择图书类型(输入数字):" << endl;
for (int i = 0; i < types.size(); i++) {
cout << i + 1 << ". " << types[i]->type << endl;
}
int typeIndex;
cin >> typeIndex;
BookType* type = types[typeIndex - 1];
Book* book = new Book(name, author, ISBN, price, type);
books.push_back(book);
cout << "添加成功!" << endl;
}
void updateBook() { // 修改书籍信息
cout << "请输入要修改的书籍ISBN号:";
string ISBN;
cin >> ISBN;
bool found = false;
for (int i = 0; i < books.size(); i++) {
if (books[i]->ISBN == ISBN) {
cout << "请输入新的书名(原书名:" << books[i]->name << "):";
string name;
cin >> name;
books[i]->name = name;
cout << "请输入新的作者(原作者:" << books[i]->author << "):";
string author;
cin >> author;
books[i]->author = author;
cout << "请输入新的ISBN号(原ISBN号:" << books[i]->ISBN << "):";
string ISBN;
cin >> ISBN;
books[i]->ISBN = ISBN;
cout << "请输入新的价格(原价格:" << books[i]->price << "):";
double price;
cin >> price;
books[i]->price = price;
cout << "请选择新的图书类型(输入数字):" << endl;
for (int j = 0; j < types.size(); j++) {
cout << j + 1 << ". " << types[j]->type << endl;
}
int typeIndex;
cin >> typeIndex;
BookType* type = types[typeIndex - 1];
books[i]->type = type;
found = true;
break;
}
}
if (!found) {
cout << "未找到该书籍!" << endl;
} else {
cout << "修改成功!" << endl;
}
}
void deleteBook() { // 删除书籍信息
cout << "请输入要删除的书籍ISBN号:";
string ISBN;
cin >> ISBN;
bool found = false;
for (int i = 0; i < books.size(); i++) {
if (books[i]->ISBN == ISBN) {
books.erase(books.begin() + i);
found = true;
break;
}
}
if (!found) {
cout << "未找到该书籍!" << endl;
} else {
cout << "删除成功!" << endl;
}
}
void findBook() { // 查找书籍信息
cout << "请输入要查找的书籍ISBN号:";
string ISBN;
cin >> ISBN;
bool found = false;
for (int i = 0; i < books.size(); i++) {
if (books[i]->ISBN == ISBN) {
books[i]->displayInfo();
found = true;
break;
}
}
if (!found) {
cout << "未找到该书籍!" << endl;
}
}
void display() { // 输出所有书籍信息
for (int i = 0; i < books.size(); i++) {
books[i]->displayInfo();
cout << endl;
}
}
void sort(int sortType) { // 按编号或书名排序
if (sortType == 1) { // 按编号排序
sort(books.begin(), books.end(), [](Book* a, Book* b) { return a->ISBN < b->ISBN; });
} else if (sortType == 2) { // 按书名排序
sort(books.begin(), books.end(), [](Book* a, Book* b) { return a->name < b->name; });
}
cout << "排序成功!" << endl;
}
void saveToFile() { // 将图书信息保存到文件中
ofstream outfile("books.txt");
for (int i = 0; i < books.size(); i++) {
outfile << books[i]->name << "," << books[i]->author << "," << books[i]->ISBN << "," << books[i]->price << "," << books[i]->type->type << "," << books[i]->type->borrowDays << endl;
}
outfile.close();
cout << "保存成功!" << endl;
}
void loadFromFile() { // 从文件中读取图书信息
ifstream infile("books.txt");
string line;
while (getline(infile, line)) {
string name, author, ISBN, type;
int borrowDays;
double price;
int commaIndex = line.find(",");
name = line.substr(0, commaIndex);
line = line.substr(commaIndex + 1);
commaIndex = line.find(",");
author = line.substr(0, commaIndex);
line = line.substr(commaIndex + 1);
commaIndex = line.find(",");
ISBN = line.substr(0, commaIndex);
line = line.substr(commaIndex + 1);
commaIndex = line.find(",");
price = stod(line.substr(0, commaIndex));
line = line.substr(commaIndex + 1);
commaIndex = line.find(",");
type = line.substr(0, commaIndex);
borrowDays = stoi(line.substr(commaIndex + 1));
BookType* bookType;
for (int i = 0; i < types.size(); i++) {
if (types[i]->type == type) {
bookType = types[i];
break;
}
}
Book* book = new Book(name, author, ISBN, price, bookType);
books.push_back(book);
}
infile.close();
cout << "读取成功!" << endl;
}
};
int main() {
BookManager bookManager;
bookManager.init(); // 初始化图书信息
while (true) {
cout << "请选择要执行的操作(输入数字):" << endl;
cout << "1. 添加书籍" << endl;
cout << "2. 修改书籍信息" << endl;
cout << "3. 删除书籍" << endl;
cout << "4. 查找书籍信息" << endl;
cout << "5. 输出所有书籍信息" << endl;
cout << "6. 按编号排序" << endl;
cout << "7. 按书名排序" << endl;
cout << "8. 将图书信息保存到文件" << endl;
cout << "9. 从文件中读取图书信息" << endl;
cout << "0. 退出程序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: // 添加书籍信息
bookManager.addBook();
break;
case 2: // 修改书籍信息
bookManager.updateBook();
break;
case 3: // 删除书籍信息
bookManager.deleteBook();
break;
case 4: // 查找书籍信息
bookManager.findBook();
break;
case 5: // 输出所有书籍信息
bookManager.display();
break;
case 6: // 按编号排序
bookManager.sort(1);
break;
case 7: // 按书名排序
bookManager.sort(2);
break;
case 8: // 将图书信息保存到文件
bookManager.saveToFile();
break;
case 9: // 从文件中读取图书信息
bookManager.loadFromFile();
break;
case 0: // 退出程序
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
注意:上述代码中,为了简化实现,没有考虑重复添加图书的情况,实际应用中需要进行判断并给出提示。同时,为了方便存储和读取,将图书信息保存到了一个文本文件中,实际应用中可以使用数据库等方式进行存储。
阅读全文