帮我用C++写一份图书管管理系统,需要有完整的代码注释,能在控制台进行显示操作,需要基类,派生类,还需要这些内容:1.建立书籍类、图书类型类、图书类 2.书籍、图书类型、图书信息的初始化 3.书籍、图书类型、图书信息的添加、修改、删除、查找 4.图书信息的输出 5.对图书信息按编号、书名排序 6.文件的保存 7.文件的读取
时间: 2024-01-13 18:04:50 浏览: 90
基于多态的职工管理系统 工程源码+详细注释
好的,我可以为您提供一个基础框架,并给出相应代码注释,您可以在此基础上进行修改和完善。
首先,我们需要建立三个类:Book(书籍类)、BookType(图书类型类)和Library(图书类)。其中,BookType 是 Book 的派生类,Library 是 BookType 的派生类。
在 Book 类中,我们可以设定以下属性:
- bookId: 书籍编号
- bookName: 书名
- author: 作者
- price: 价格
- status: 状态(是否被借出)
在 BookType 类中,我们可以设定以下属性:
- typeId: 图书类型编号
- typeName: 图书类型名称
在 Library 类中,我们可以设定以下属性:
- borrowDate: 借阅日期
- returnDate: 归还日期
接下来,我们需要实现初始化函数 init(),用于给每个属性赋值。
在 Book 类中,初始化函数如下:
```c++
void init(string id, string name, string a, float p, int s) {
bookId = id;
bookName = name;
author = a;
price = p;
status = s;
}
```
在 BookType 类中,初始化函数如下:
```c++
void init(int id, string name) {
typeId = id;
typeName = name;
}
```
在 Library 类中,初始化函数如下:
```c++
void init(string id, string date1, string date2) {
bookId = id;
borrowDate = date1;
returnDate = date2;
}
```
接下来,我们需要实现添加、修改、删除、查找和输出函数。
在 Book 类中,我们可以实现以下函数:
```c++
// 添加书籍
void addBook() {
cout << "请输入书籍编号:";
string id;
cin >> id;
cout << "请输入书名:";
string name;
cin >> name;
cout << "请输入作者:";
string a;
cin >> a;
cout << "请输入价格:";
float p;
cin >> p;
books[id].init(id, name, a, p, 0);
cout << "添加成功!" << endl;
}
// 修改书籍
void modifyBook() {
cout << "请输入需要修改的书籍编号:";
string id;
cin >> id;
if (books.count(id) == 0) {
cout << "该书籍不存在!" << endl;
return;
}
cout << "请输入修改后的书名:";
string name;
cin >> name;
cout << "请输入修改后的作者:";
string a;
cin >> a;
cout << "请输入修改后的价格:";
float p;
cin >> p;
books[id].init(id, name, a, p, books[id].status);
cout << "修改成功!" << endl;
}
// 删除书籍
void deleteBook() {
cout << "请输入需要删除的书籍编号:";
string id;
cin >> id;
if (books.count(id) == 0) {
cout << "该书籍不存在!" << endl;
return;
}
books.erase(id);
cout << "删除成功!" << endl;
}
// 查找书籍
void searchBook() {
cout << "请输入需要查找的书籍编号:";
string id;
cin >> id;
if (books.count(id) == 0) {
cout << "该书籍不存在!" << endl;
return;
}
cout << "书籍编号:" << books[id].bookId << endl;
cout << "书名:" << books[id].bookName << endl;
cout << "作者:" << books[id].author << endl;
cout << "价格:" << books[id].price << endl;
if (books[id].status == 0) {
cout << "状态:未借出" << endl;
} else {
cout << "状态:已借出" << endl;
}
}
// 输出书籍信息
void printBook() {
cout << "图书列表:" << endl;
for (auto iter = books.begin(); iter != books.end(); iter++) {
cout << "书籍编号:" << iter->second.bookId << endl;
cout << "书名:" << iter->second.bookName << endl;
cout << "作者:" << iter->second.author << endl;
cout << "价格:" << iter->second.price << endl;
if (iter->second.status == 0) {
cout << "状态:未借出" << endl;
} else {
cout << "状态:已借出" << endl;
}
cout << endl;
}
}
```
在 BookType 类中,我们可以实现以下函数:
```c++
// 添加图书类型
void addBookType() {
cout << "请输入图书类型编号:";
int id;
cin >> id;
cout << "请输入图书类型名称:";
string name;
cin >> name;
bookTypes[id].init(id, name);
cout << "添加成功!" << endl;
}
// 修改图书类型
void modifyBookType() {
cout << "请输入需要修改的图书类型编号:";
int id;
cin >> id;
if (bookTypes.count(id) == 0) {
cout << "该图书类型不存在!" << endl;
return;
}
cout << "请输入修改后的图书类型名称:";
string name;
cin >> name;
bookTypes[id].init(id, name);
cout << "修改成功!" << endl;
}
// 删除图书类型
void deleteBookType() {
cout << "请输入需要删除的图书类型编号:";
int id;
cin >> id;
if (bookTypes.count(id) == 0) {
cout << "该图书类型不存在!" << endl;
return;
}
bookTypes.erase(id);
cout << "删除成功!" << endl;
}
// 查找图书类型
void searchBookType() {
cout << "请输入需要查找的图书类型编号:";
int id;
cin >> id;
if (bookTypes.count(id) == 0) {
cout << "该图书类型不存在!" << endl;
return;
}
cout << "图书类型编号:" << bookTypes[id].typeId << endl;
cout << "图书类型名称:" << bookTypes[id].typeName << endl;
}
// 输出图书类型信息
void printBookType() {
cout << "图书类型列表:" << endl;
for (auto iter = bookTypes.begin(); iter != bookTypes.end(); iter++) {
cout << "图书类型编号:" << iter->second.typeId << endl;
cout << "图书类型名称:" << iter->second.typeName << endl;
cout << endl;
}
}
```
在 Library 类中,我们可以实现以下函数:
```c++
// 添加图书
void addLibrary() {
cout << "请输入书籍编号:";
string id;
cin >> id;
if (books.count(id) == 0) {
cout << "该书籍不存在!" << endl;
return;
}
cout << "请输入借阅日期(格式:yyyy-mm-dd):";
string borrowDate;
cin >> borrowDate;
cout << "请输入归还日期(格式:yyyy-mm-dd):";
string returnDate;
cin >> returnDate;
libraries[id].init(id, borrowDate, returnDate);
books[id].status = 1;
cout << "添加成功!" << endl;
}
// 修改图书
void modifyLibrary() {
cout << "请输入需要修改的书籍编号:";
string id;
cin >> id;
if (libraries.count(id) == 0) {
cout << "该书籍未借出!" << endl;
return;
}
cout << "请输入修改后的借阅日期(格式:yyyy-mm-dd):";
string borrowDate;
cin >> borrowDate;
cout << "请输入修改后的归还日期(格式:yyyy-mm-dd):";
string returnDate;
cin >> returnDate;
libraries[id].init(id, borrowDate, returnDate);
cout << "修改成功!" << endl;
}
// 删除图书
void deleteLibrary() {
cout << "请输入需要删除的书籍编号:";
string id;
cin >> id;
if (libraries.count(id) == 0) {
cout << "该书籍未借出!" << endl;
return;
}
libraries.erase(id);
books[id].status = 0;
cout << "删除成功!" << endl;
}
// 查找图书
void searchLibrary() {
cout << "请输入需要查找的书籍编号:";
string id;
cin >> id;
if (libraries.count(id) == 0) {
cout << "该书籍未借出!" << endl;
return;
}
cout << "书籍编号:" << libraries[id].bookId << endl;
cout << "借阅日期:" << libraries[id].borrowDate << endl;
cout << "归还日期:" << libraries[id].returnDate << endl;
}
// 输出图书信息
void printLibrary() {
cout << "图书列表:" << endl;
for (auto iter = libraries.begin(); iter != libraries.end(); iter++) {
cout << "书籍编号:" << iter->second.bookId << endl;
cout << "借阅日期:" << iter->second.borrowDate << endl;
cout << "归还日期:" << iter->second.returnDate << endl;
cout << endl;
}
}
```
接下来,我们需要实现对图书信息按编号、书名排序的函数。在 Book 类中,我们可以添加以下函数:
```c++
// 按编号排序
void sortById() {
vector<pair<string, Book>> vec(books.begin(), books.end());
sort(vec.begin(), vec.end(), [](pair<string, Book> a, pair<string, Book> b) {
return a.second.bookId < b.second.bookId;
});
books.clear();
for (auto iter = vec.begin(); iter != vec.end(); iter++) {
books[iter->first] = iter->second;
}
cout << "按编号排序成功!" << endl;
}
// 按书名排序
void sortByName() {
vector<pair<string, Book>> vec(books.begin(), books.end());
sort(vec.begin(), vec.end(), [](pair<string, Book> a, pair<string, Book> b) {
return a.second.bookName < b.second.bookName;
});
books.clear();
for (auto iter = vec.begin(); iter != vec.end(); iter++) {
books[iter->first] = iter->second;
}
cout << "按书名排序成功!" << endl;
}
```
最后,我们需要实现文件的保存和读取。在 main 函数中,我们可以添加以下函数:
```c++
// 保存文件
void saveData() {
ofstream fout("data.txt");
fout << books.size() << endl;
for (auto iter = books.begin(); iter != books.end(); iter++) {
fout << iter->second.bookId << " " << iter->second.bookName << " " << iter->second.author << " " << iter->second.price << " " << iter->second.status << endl;
}
fout << bookTypes.size() << endl;
for (auto iter = bookTypes.begin(); iter != bookTypes.end(); iter++) {
fout << iter->second.typeId << " " << iter->second.typeName << endl;
}
fout << libraries.size() << endl;
for (auto iter = libraries.begin(); iter != libraries.end(); iter++) {
fout << iter->second.bookId << " " << iter->second.borrowDate << " " << iter->second.returnDate << endl;
}
fout.close();
cout << "保存成功!" << endl;
}
// 读取文件
void loadData() {
ifstream fin("data.txt");
if (fin.fail()) {
cout << "文件不存在!" << endl;
return;
}
int n;
fin >> n;
for (int i = 0; i < n; i++) {
string id, name, author;
float price;
int status;
fin >> id >> name >> author >> price >> status;
books[id].init(id, name, author, price, status);
}
fin >> n;
for (int i = 0; i < n; i++) {
int id;
string name;
fin >> id >> name;
bookTypes[id].init(id, name);
}
fin >> n;
for (int i = 0; i < n; i++) {
string id, borrowDate, returnDate;
fin >> id >> borrowDate >> returnDate;
libraries[id].init(id, borrowDate, returnDate);
books[id].status = 1;
}
fin.close();
cout << "读取成功!" << endl;
}
```
完整代码如下:
阅读全文