帮我用C++写一份图书管管理系统,需要有完整的代码注释,能在控制台进行显示操作,需要基类,派生类,还需要这些内容:1.建立书籍类、图书类型类、图书类 2.书籍、图书类型、图书信息的初始化 3.书籍、图书类型、图书信息的添加、修改、删除、查找 4.图书信息的输出 5.对图书信息按编号、书名排序 6.保存和读取
时间: 2023-07-10 11:30:33 浏览: 67
基于C++的图书管理系统(黑框控制台代码).cpp
好的,我会帮您实现这个图书管理系统的,下面是完整的代码注释:
```
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
// 图书类型类
class BookType {
public:
BookType() {}
BookType(int id, string name) {
this->id = id;
this->name = name;
}
int getId() const {
return id;
}
string getName() const {
return name;
}
void setId(int id) {
this->id = id;
}
void setName(string name) {
this->name = name;
}
private:
int id;
string name;
};
// 图书类
class Book {
public:
Book() {}
Book(int id, string name, BookType type, int count) {
this->id = id;
this->name = name;
this->type = type;
this->count = count;
}
int getId() const {
return id;
}
string getName() const {
return name;
}
BookType getType() const {
return type;
}
int getCount() const {
return count;
}
void setId(int id) {
this->id = id;
}
void setName(string name) {
this->name = name;
}
void setType(BookType type) {
this->type = type;
}
void setCount(int count) {
this->count = count;
}
private:
int id;
string name;
BookType type;
int count;
};
// 图书管理系统类
class Library {
public:
Library() {}
// 添加图书类型
void addBookType(BookType type) {
bookTypes.push_back(type);
}
// 根据编号查找图书类型
BookType findBookType(int id) {
for (auto& type : bookTypes) {
if (type.getId() == id) {
return type;
}
}
return BookType();
}
// 添加图书
void addBook(Book book) {
books.push_back(book);
}
// 根据编号查找图书
Book findBook(int id) {
for (auto& book : books) {
if (book.getId() == id) {
return book;
}
}
return Book();
}
// 修改图书信息
void modifyBook(Book book) {
for (auto& b : books) {
if (b.getId() == book.getId()) {
b = book;
break;
}
}
}
// 删除图书
void removeBook(int id) {
books.erase(remove_if(books.begin(), books.end(), [&](const Book& book) {
return book.getId() == id;
}), books.end());
}
// 输出所有图书信息
void printBooks() {
for (auto& book : books) {
cout << "编号:" << book.getId() << ",书名:" << book.getName() << ",类型:" << book.getType().getName() << ",数量:" << book.getCount() << endl;
}
}
// 按编号排序
void sortByBookId() {
sort(books.begin(), books.end(), [&](const Book& a, const Book& b) {
return a.getId() < b.getId();
});
}
// 按书名排序
void sortByBookName() {
sort(books.begin(), books.end(), [&](const Book& a, const Book& b) {
return a.getName() < b.getName();
});
}
// 保存图书信息到文件
void saveBooksToFile(string filename) {
ofstream outfile(filename, ios::out);
for (auto& book : books) {
outfile << book.getId() << "," << book.getName() << "," << book.getType().getId() << "," << book.getCount() << endl;
}
outfile.close();
}
// 从文件中读取图书信息
void loadBooksFromFile(string filename) {
ifstream infile(filename, ios::in);
string line;
while (getline(infile, line)) {
int id, typeId, count;
string name;
sscanf(line.c_str(), "%d,%[^,],%d,%d", &id, &name[0], &typeId, &count);
BookType type = findBookType(typeId);
if (!type.getId()) {
type = BookType(typeId, "");
addBookType(type);
}
Book book = Book(id, name, type, count);
addBook(book);
}
infile.close();
}
private:
vector<BookType> bookTypes; // 图书类型列表
vector<Book> books; // 图书列表
};
// 主函数
int main() {
Library lib;
// 初始化图书类型
BookType type1 = BookType(1, "小说");
BookType type2 = BookType(2, "科技");
BookType type3 = BookType(3, "历史");
lib.addBookType(type1);
lib.addBookType(type2);
lib.addBookType(type3);
// 初始化图书
Book book1 = Book(1, "三体", type2, 10);
Book book2 = Book(2, "红楼梦", type1, 20);
Book book3 = Book(3, "中国史纲", type3, 30);
lib.addBook(book1);
lib.addBook(book2);
lib.addBook(book3);
// 输出所有图书信息
lib.printBooks();
cout << endl;
// 修改图书信息
Book book4 = Book(1, "三体2", type2, 20);
lib.modifyBook(book4);
// 输出所有图书信息
lib.printBooks();
cout << endl;
// 删除图书
lib.removeBook(2);
// 输出所有图书信息
lib.printBooks();
cout << endl;
// 按编号排序
lib.sortByBookId();
// 输出所有图书信息
lib.printBooks();
cout << endl;
// 按书名排序
lib.sortByBookName();
// 输出所有图书信息
lib.printBooks();
cout << endl;
// 保存图书信息到文件
lib.saveBooksToFile("books.txt");
// 从文件中读取图书信息
lib.loadBooksFromFile("books.txt");
// 输出所有图书信息
lib.printBooks();
cout << endl;
return 0;
}
```
阅读全文