一、项目介绍
(1)图书管理
(a)添加图书:在系统中增加图书信息(可自己设计图书的信息:如书号,书名,出版社,等等)
(b)查询图书
(c)编辑图书
(d)删除图书
(2)读者管理
(a)读者注册:在系统中增加读者信息(可自己设计读者信息:如读者姓名、读者性别,联系方式等)
(b)查询读者
(c)编辑读者
(d)删除读者
(3)借阅管理
当输入图书编号,读者编号之后,能对图书馆中现有的图书进行借阅。
(4)还书管理
当输入读者编号和要归还的图书编号,读者能进行还书。
利用文件保存
二、总体代码实现
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <string>
// for std::setw and std::setfillusing namespace std;// 图书类与读者类的声明
class Book;
class Reader;// 管理图书馆的类
class Library {
private:vector<Book> books;vector<Reader> readers;public:// 构造器Library();// 图书操作void addBook();void searchBook();void editBook();void deleteBook();// 读者操作void addReader();void searchReader();void editReader();void deleteReader();// 借阅与归还操作void borrowBook();void returnBook();// 文件操作void saveData();void loadData();
};// 代表图书的类
class Book {
private:int book_id;string title;string publisher;public:// 构造器Book(int id, const string& t, const string& pub) : book_id(id), title(t), publisher(pub) {}// get方法int getBookId() const { return book_id; }string getTitle() const { return title; }string getPublisher() const { return publisher; }// set方法void setTitle(const string& t) { title = t; }void setPublisher(const string& pub) { publisher = pub; }// 方法来显示读取构造器详细信息void display() const {cout << "图书书号: " << setw(5) << setfill('0') << book_id << " | 图书书名: " << title << " | 图书出版社: " << publisher << endl;}
};// 代表读者的类
class Reader {
private:int reader_id;string name;char gender;string contact;public:// 构造器Reader(int id, const string& n, char g, const string& con) : reader_id(id), name(n), gender(g), contact(con) {}// get方法int getReaderId() const { return reader_id; }string getName() const { return name; }char getGender() const { return gender; }string getContact() const { return contact; }// set方法void setName(const string& n) { name = n; }void setGender(char g) { gender = g; }void setContact(const string& con) { contact = con; }// 方法来显示读取构造器详细信息void display() const {cout << "读者编号: " << setw(5) << setfill('0') << reader_id << " | 读者姓名: " << name << " | 读者性别: " << gender << " | 读者联系方式: " << contact << endl;}
};// 类方法的实现Library::Library() {// 创建库对象时从文件加载数据loadData();
}void Library::addBook() {int id;string title, publisher;cout << "请输入图书书号: ";cin >> id;cin.ignore(); // 从缓冲区中清除换行符cout << "请输入图书书名: ";getline(cin, title);cout << "请输入图书出版社: ";getline(cin, publisher);books.push_back(Book(id, title, publisher));cout << "图书添加成功。\n";saveData(); // 添加成功后保存数据
}void Library::searchBook() {int search_id;cout << "请输入要搜索的图书书号: ";cin >> search_id;bool found = false;for (const auto& book : books) {if (book.getBookId() == search_id) {book.display();found = true;break;}}if (!found) {cout << "没有找到该图书: " << search_id << endl;}
}void Library::editBook() {int edit_id;cout << "请输入要编辑的图书书号: ";cin >> edit_id;for (auto& book : books) {if (book.getBookId() == edit_id) {string new_title, new_publisher;cout << "请输入新的书名(输入以保持当前状态): ";cin.ignore(); // 从缓冲区中清除换行符getline(cin, new_title);if (!new_title.empty()) {book.setTitle(new_title);}cout << "情输入新的出版社 (输入以保持当前状态): ";getline(cin, new_publisher);if (!new_publisher.empty()) {book.setPublisher(new_publisher);}cout << "图书详细信息更新成功。\n";saveData(); // 更新后保存数据return;}}cout << "没有找到该图书: " << edit_id << endl;
}void Library::deleteBook() {int delete_id;cout << "请输入要删除的图书书号: ";cin >> delete_id;auto it = books.begin();while (it != books.end()) {if (it->getBookId() == delete_id) {it = books.erase(it);cout << "图书删除成功。\n";saveData(); // 删除后保存数据return;}else {++it;}}cout << "没有找到该图书: " << delete_id << endl;
}void Library::addReader() {int id;string name, contact;char gender;cout << "请输入读者编号: ";cin >> id;cin.ignore(); // 从缓冲区中清除换行符cout << "请输入读者姓名: ";getline(cin, name);cout << "请输入读者性别(M/F): ";cin >> gender;cout << "请输入读者联系方式: ";cin.ignore(); // 从缓冲区中清除换行符getline(cin, contact);readers.push_back(Reader(id, name, gender, contact));cout << "读者添加成功。\n";saveData(); // 添加成功后保存数据
}void Library::searchReader() {int search_id;cout << "请输入要查询的读者编号: ";cin >> search_id;bool found = false;for (const auto& reader : readers) {if (reader.getReaderId() == search_id) {reader.display();found = true;break;}}if (!found) {cout << "没有找到该读者: " << search_id << endl;}
}void Library::editReader() {int edit_id;cout << "请输入要编辑的读者编号: ";cin >> edit_id;for (auto& reader : readers) {if (reader.getReaderId() == edit_id) {string new_name, new_contact;char new_gender;cout << "请输入新的姓名(输入以保持当前状态): ";cin.ignore(); // 从缓冲区中清除换行符getline(cin, new_name);if (!new_name.empty()) {reader.setName(new_name);}cout << "请输入新的性别 (M/F) (输入以保持当前状态): ";cin >> new_gender;if (new_gender == 'M' || new_gender == 'F') {reader.setGender(new_gender);}cout << "请输入新的联系方式(输入以保持当前状态): ";cin.ignore(); // 从缓冲区中清除换行符getline(cin, new_contact);if (!new_contact.empty()) {reader.setContact(new_contact);}cout << "读者详细信息更新完成。\n";saveData(); // 更新后保存数据return;}}cout << "没有找到该读者: " << edit_id << endl;
}void Library::deleteReader() {int delete_id;cout << "请输入要删除的读者编号: ";cin >> delete_id;auto it = readers.begin();while (it != readers.end()) {if (it->getReaderId() == delete_id) {it = readers.erase(it);cout << "读者删除成功。\n";saveData(); // 删除后保存数据return;}else {++it;}}cout << "没有找到该读者: " << delete_id << endl;
}void Library::borrowBook() {int book_id, reader_id;cout << "请输入要借阅图书的图书编号: ";cin >> book_id;cout << "请输入要借阅图书的读者编号: ";cin >> reader_id;// 借用逻辑的占位符实现cout << "图书借阅成功。\n";
}void Library::returnBook() {int book_id, reader_id;cout << "请输入要归还图书的图书编号: ";cin >> book_id;cout << "请输入要归还图书的读者编号: ";cin >> reader_id;// 借用逻辑的占位符实现cout << "图书归还成功。\n";
}void Library::saveData() {ofstream bookFile("books.txt");if (!bookFile.is_open()) {cerr << "无法打开books.txt进行写入。\n";return;}for (const auto& book : books) {bookFile << book.getBookId() << ',' << book.getTitle() << ',' << book.getPublisher() << '\n';}bookFile.close();ofstream readerFile("readers.txt");if (!readerFile.is_open()) {cerr << "无法打开readers.txt进行写入。\n";return;}for (const auto& reader : readers) {readerFile << reader.getReaderId() << ',' << reader.getName() << ',' << reader.getGender() << ',' << reader.getContact() << '\n';}readerFile.close();cout << "数据保存成功。\n";
}void Library::loadData() {ifstream bookFile("books.txt");if (!bookFile.is_open()) {cerr << "无法打开books.txt进行阅读。\n";return;}int id;string title, publisher;while (bookFile >> id) {bookFile.ignore(); // Ignore the commagetline(bookFile, title, ',');getline(bookFile, publisher);books.push_back(Book(id, title, publisher));}bookFile.close();ifstream readerFile("readers.txt");if (!readerFile.is_open()) {cerr << "无法打开readers.txt进行阅读。\n";return;}char gender;string name, contact;while (readerFile >> id) {readerFile.ignore(); // 忽略逗号getline(readerFile, name, ',');readerFile >> gender;readerFile.ignore(); // 忽略逗号getline(readerFile, contact);readers.push_back(Reader(id, name, gender, contact));}readerFile.close();cout << "数据加载成功。\n";
}// 主界面(驱动程序的主要功能)
int main() {Library library;int choice;do {cout << "\n图书管理系统\n";cout << "1. 添加图书\n";cout << "2. 查询图书\n";cout << "3. 编辑图书\n";cout << "4. 删除图书\n";cout << "5. 读者注册\n";cout << "6. 查询读者\n";cout << "7. 编辑读者\n";cout << "8. 删除读者\n";cout << "9. 借阅图书\n";cout << "10.归还图书\n";cout << "0. 退出程序\n";cout << "请输入您的选择: ";cin >> choice;switch (choice) {case 1:library.addBook();break;case 2:library.searchBook();break;case 3:library.editBook();break;case 4:library.deleteBook();break;case 5:library.addReader();break;case 6:library.searchReader();break;case 7:library.editReader();break;case 8:library.deleteReader();break;case 9:library.borrowBook();break;case 10:library.returnBook();break;case 0:cout << "退出程序\n";break;default:cout << "无效选择。请再次输入。\n";}} while (choice != 0);return 0;
}
三、分布介绍
1.主页面展示
通过简单的可视化界面展示
// 主界面(驱动程序的主要功能)
int main() {Library library;int choice;do {cout << "\n图书管理系统\n";cout << "1. 添加图书\n";cout << "2. 查询图书\n";cout << "3. 编辑图书\n";cout << "4. 删除图书\n";cout << "5. 读者注册\n";cout << "6. 查询读者\n";cout << "7. 编辑读者\n";cout << "8. 删除读者\n";cout << "9. 借阅图书\n";cout << "10.归还图书\n";cout << "0. 退出程序\n";cout << "请输入您的选择: ";cin >> choice;switch (choice) {case 1:library.addBook();break;case 2:library.searchBook();break;case 3:library.editBook();break;case 4:library.deleteBook();break;case 5:library.addReader();break;case 6:library.searchReader();break;case 7:library.editReader();break;case 8:library.deleteReader();break;case 9:library.borrowBook();break;case 10:library.returnBook();break;case 0:cout << "退出程序\n";break;default:cout << "无效选择。请再次输入。\n";}} while (choice != 0);return 0;
}
2.代表图书和读者的类
// 图书类与读者类的声明
class Book;
class Reader;// 管理图书馆的类
class Library {
private:vector<Book> books;vector<Reader> readers;public:// 构造器Library();// 图书操作void addBook();void searchBook();void editBook();void deleteBook();// 读者操作void addReader();void searchReader();void editReader();void deleteReader();// 借阅与归还操作void borrowBook();void returnBook();// 文件操作void saveData();void loadData();
};// 代表图书的类
class Book {
private:int book_id;string title;string publisher;public:// 构造器Book(int id, const string& t, const string& pub) : book_id(id), title(t), publisher(pub) {}// get方法int getBookId() const { return book_id; }string getTitle() const { return title; }string getPublisher() const { return publisher; }// set方法void setTitle(const string& t) { title = t; }void setPublisher(const string& pub) { publisher = pub; }// 方法来显示读取构造器详细信息void display() const {cout << "图书书号: " << setw(5) << setfill('0') << book_id << " | 图书书名: " << title << " | 图书出版社: " << publisher << endl;}
};// 代表读者的类
class Reader {
private:int reader_id;string name;char gender;string contact;public:// 构造器Reader(int id, const string& n, char g, const string& con) : reader_id(id), name(n), gender(g), contact(con) {}// get方法int getReaderId() const { return reader_id; }string getName() const { return name; }char getGender() const { return gender; }string getContact() const { return contact; }// set方法void setName(const string& n) { name = n; }void setGender(char g) { gender = g; }void setContact(const string& con) { contact = con; }// 方法来显示读取构造器详细信息void display() const {cout << "读者编号: " << setw(5) << setfill('0') << reader_id << " | 读者姓名: " << name << " | 读者性别: " << gender << " | 读者联系方式: " << contact << endl;}
};
3.相关类方法的实现
// 类方法的实现Library::Library() {// 创建库对象时从文件加载数据loadData();
}void Library::addBook() {int id;string title, publisher;cout << "请输入图书书号: ";cin >> id;cin.ignore(); // 从缓冲区中清除换行符cout << "请输入图书书名: ";getline(cin, title);cout << "请输入图书出版社: ";getline(cin, publisher);books.push_back(Book(id, title, publisher));cout << "图书添加成功。\n";saveData(); // 添加成功后保存数据
}void Library::searchBook() {int search_id;cout << "请输入要搜索的图书书号: ";cin >> search_id;bool found = false;for (const auto& book : books) {if (book.getBookId() == search_id) {book.display();found = true;break;}}if (!found) {cout << "没有找到该图书: " << search_id << endl;}
}void Library::editBook() {int edit_id;cout << "请输入要编辑的图书书号: ";cin >> edit_id;for (auto& book : books) {if (book.getBookId() == edit_id) {string new_title, new_publisher;cout << "请输入新的书名(输入以保持当前状态): ";cin.ignore(); // 从缓冲区中清除换行符getline(cin, new_title);if (!new_title.empty()) {book.setTitle(new_title);}cout << "情输入新的出版社 (输入以保持当前状态): ";getline(cin, new_publisher);if (!new_publisher.empty()) {book.setPublisher(new_publisher);}cout << "图书详细信息更新成功。\n";saveData(); // 更新后保存数据return;}}cout << "没有找到该图书: " << edit_id << endl;
}void Library::deleteBook() {int delete_id;cout << "请输入要删除的图书书号: ";cin >> delete_id;auto it = books.begin();while (it != books.end()) {if (it->getBookId() == delete_id) {it = books.erase(it);cout << "图书删除成功。\n";saveData(); // 删除后保存数据return;}else {++it;}}cout << "没有找到该图书: " << delete_id << endl;
}void Library::addReader() {int id;string name, contact;char gender;cout << "请输入读者编号: ";cin >> id;cin.ignore(); // 从缓冲区中清除换行符cout << "请输入读者姓名: ";getline(cin, name);cout << "请输入读者性别(M/F): ";cin >> gender;cout << "请输入读者联系方式: ";cin.ignore(); // 从缓冲区中清除换行符getline(cin, contact);readers.push_back(Reader(id, name, gender, contact));cout << "读者添加成功。\n";saveData(); // 添加成功后保存数据
}void Library::searchReader() {int search_id;cout << "请输入要查询的读者编号: ";cin >> search_id;bool found = false;for (const auto& reader : readers) {if (reader.getReaderId() == search_id) {reader.display();found = true;break;}}if (!found) {cout << "没有找到该读者: " << search_id << endl;}
}void Library::editReader() {int edit_id;cout << "请输入要编辑的读者编号: ";cin >> edit_id;for (auto& reader : readers) {if (reader.getReaderId() == edit_id) {string new_name, new_contact;char new_gender;cout << "请输入新的姓名(输入以保持当前状态): ";cin.ignore(); // 从缓冲区中清除换行符getline(cin, new_name);if (!new_name.empty()) {reader.setName(new_name);}cout << "请输入新的性别 (M/F) (输入以保持当前状态): ";cin >> new_gender;if (new_gender == 'M' || new_gender == 'F') {reader.setGender(new_gender);}cout << "请输入新的联系方式(输入以保持当前状态): ";cin.ignore(); // 从缓冲区中清除换行符getline(cin, new_contact);if (!new_contact.empty()) {reader.setContact(new_contact);}cout << "读者详细信息更新完成。\n";saveData(); // 更新后保存数据return;}}cout << "没有找到该读者: " << edit_id << endl;
}void Library::deleteReader() {int delete_id;cout << "请输入要删除的读者编号: ";cin >> delete_id;auto it = readers.begin();while (it != readers.end()) {if (it->getReaderId() == delete_id) {it = readers.erase(it);cout << "读者删除成功。\n";saveData(); // 删除后保存数据return;}else {++it;}}cout << "没有找到该读者: " << delete_id << endl;
}void Library::borrowBook() {int book_id, reader_id;cout << "请输入要借阅图书的图书编号: ";cin >> book_id;cout << "请输入要借阅图书的读者编号: ";cin >> reader_id;// 借用逻辑的占位符实现cout << "图书借阅成功。\n";
}void Library::returnBook() {int book_id, reader_id;cout << "请输入要归还图书的图书编号: ";cin >> book_id;cout << "请输入要归还图书的读者编号: ";cin >> reader_id;// 借用逻辑的占位符实现cout << "图书归还成功。\n";
}void Library::saveData() {ofstream bookFile("books.txt");if (!bookFile.is_open()) {cerr << "无法打开books.txt进行写入。\n";return;}for (const auto& book : books) {bookFile << book.getBookId() << ',' << book.getTitle() << ',' << book.getPublisher() << '\n';}bookFile.close();ofstream readerFile("readers.txt");if (!readerFile.is_open()) {cerr << "无法打开readers.txt进行写入。\n";return;}for (const auto& reader : readers) {readerFile << reader.getReaderId() << ',' << reader.getName() << ',' << reader.getGender() << ',' << reader.getContact() << '\n';}readerFile.close();cout << "数据保存成功。\n";
}void Library::loadData() {ifstream bookFile("books.txt");if (!bookFile.is_open()) {cerr << "无法打开books.txt进行阅读。\n";return;}int id;string title, publisher;while (bookFile >> id) {bookFile.ignore(); // Ignore the commagetline(bookFile, title, ',');getline(bookFile, publisher);books.push_back(Book(id, title, publisher));}bookFile.close();ifstream readerFile("readers.txt");if (!readerFile.is_open()) {cerr << "无法打开readers.txt进行阅读。\n";return;}char gender;string name, contact;while (readerFile >> id) {readerFile.ignore(); // 忽略逗号getline(readerFile, name, ',');readerFile >> gender;readerFile.ignore(); // 忽略逗号getline(readerFile, contact);readers.push_back(Reader(id, name, gender, contact));}readerFile.close();cout << "数据加载成功。\n";
}