c++ 图书管理系统根据编号对书籍进行管理的代码
时间: 2023-10-18 20:23:07 浏览: 38
好的,下面是一个简单的 C++ 图书管理系统根据编号对书籍进行管理的代码示例:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Book {
public:
string title; // 书名
string author; // 作者
string publisher; // 出版社
string ISBN; // ISBN编码
bool borrowed; // 是否借出
// 构造函数
Book(string t, string a, string p, string i, bool b) {
title = t;
author = a;
publisher = p;
ISBN = i;
borrowed = b;
}
};
class Library {
private:
Book** books; // 图书指针数组
int numBooks; // 图书总数
public:
// 构造函数
Library() {
books = new Book*[100]; // 最多存放100本书
numBooks = 0;
}
// 添加图书
void addBook(Book* book) {
books[numBooks] = book;
numBooks++;
}
// 根据编号查找图书
Book* findBook(string ISBN) {
for (int i = 0; i < numBooks; i++) {
if (books[i]->ISBN == ISBN) {
return books[i];
}
}
return NULL;
}
// 借阅图书
void borrowBook(string ISBN) {
Book* book = findBook(ISBN);
if (book != NULL && !book->borrowed) {
book->borrowed = true;
cout << "借阅成功!" << endl;
}
else {
cout << "借阅失败!" << endl;
}
}
// 归还图书
void returnBook(string ISBN) {
Book* book = findBook(ISBN);
if (book != NULL && book->borrowed) {
book->borrowed = false;
cout << "归还成功!" << endl;
}
else {
cout << "归还失败!" << endl;
}
}
// 保存图书信息到文件
void saveToFile(string filename) {
ofstream fout(filename);
for (int i = 0; i < numBooks; i++) {
fout << books[i]->title << "," << books[i]->author << ","
<< books[i]->publisher << "," << books[i]->ISBN << ","
<< books[i]->borrowed << endl;
}
fout.close();
}
// 从文件中加载图书信息
void loadFromFile(string filename) {
ifstream fin(filename);
string t, a, p, i, b;
while (getline(fin, t, ',')) {
getline(fin, a, ',');
getline(fin, p, ',');
getline(fin, i, ',');
getline(fin, b);
bool borrowed = (b == "1");
Book* book = new Book(t, a, p, i, borrowed);
addBook(book);
}
fin.close();
}
};
int main() {
Library lib;
lib.loadFromFile("books.txt");
while (true) {
cout << "-----------------------" << endl;
cout << "1. 添加图书" << endl;
cout << "2. 借阅图书" << endl;
cout << "3. 归还图书" << endl;
cout << "4. 退出" << endl;
cout << "-----------------------" << endl;
int choice;
cout << "请输入操作编号:";
cin >> choice;
if (choice == 1) {
string t, a, p, i;
cout << "请输入书名:";
cin >> t;
cout << "请输入作者:";
cin >> a;
cout << "请输入出版社:";
cin >> p;
cout << "请输入ISBN编码:";
cin >> i;
Book* book = new Book(t, a, p, i, false);
lib.addBook(book);
cout << "添加成功!" << endl;
}
else if (choice == 2) {
string i;
cout << "请输入ISBN编码:";
cin >> i;
lib.borrowBook(i);
}
else if (choice == 3) {
string i;
cout << "请输入ISBN编码:";
cin >> i;
lib.returnBook(i);
}
else if (choice == 4) {
lib.saveToFile("books.txt");
break;
}
else {
cout << "输入有误,请重新输入!" << endl;
}
}
return 0;
}
```
在上述代码中,我们定义了 Book 类表示图书信息,Library 类表示图书管理系统。通过添加图书、借阅图书、归还图书等操作,对图书信息进行管理。在 Library 类中,我们提供了根据编号查找图书的方法 findBook,以便于根据编号进行图书管理。同时,我们还提供了保存图书信息到文件和从文件中加载图书信息的方法,以便于数据的持久化。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)