本篇文章分为三部分:第一部分给出代码,第二部分给出核心逻辑,第三部分讲解疑难点
一、总体代码如下:
#include <iostream> #include<vector> #include<algorithm> #include<string> using namespace std; class Book{ private:string title, author, isbn; bool available; public:Book(string t, string a, string i) :title(t), author(a), isbn(i), available(true){} string getTitle()const { return title; } string getAuthor()const { return author; } string getISBN()const { return isbn; } bool isavailable()const { return available; } //借书 void borrowBook() { if (available) { available = false; cout << "成功借出书籍" << endl; } else { cout << "书籍已被借出" << endl; } } //还书 void returnBook() { available = true; cout << "成功归还书籍" << endl; } //浏览 void display()const { cout << "书名" << title<<endl; cout << "作者" << author<<endl; cout << "ISBN" << isbn << endl; cout << "状态" << (available ? "可借" : "已借出") << endl; } //排序 bool operator<(const Book& other) const { return title < other.title; } }; class Library { private:vector<Book>books; public: //添加书籍 void addBook() { string title, author, isbn; cout << "请输入书名:" << endl; getline(cin, title); cout << "请输入作者名:" << endl; getline(cin, author); cout << "请输入ISBN码:" << endl; getline(cin, isbn); books.emplace_back(title, author, isbn); cout << "添加书籍成功" << endl; } //搜索书籍 void searchBook() { string keyword; cout << "输入搜索关键词(书名/作者/ISBN)" << endl; getline(cin, keyword); bool found = false; for (const auto& book : books) { if (book.getTitle().find(keyword) != string::npos || book.getAuthor().find(keyword) != string::npos || book.getISBN() == keyword) { book.display(); found = true; } } if (!found) { cout << "未找到相关书籍" << endl; } } //显示书籍 void displayAllBooks() { if (books.empty()) { cout << "图书馆没有书籍" << endl; } for (const auto& book : books) { book.display(); } } //借书功能 void borrowBook() { string isbn; cout << "请输入要借阅的书籍ISBN:" << endl; getline(cin, isbn); auto it = find_if(books.begin(), books.end(), [&isbn](const Book& b) { return b.getISBN() == isbn; }); if (it != books.end()) { it->borrowBook(); } else { cout << "未找到该书籍!" << endl; } } //还书功能 void returnBook() { string isbn; cout << "请输入要归还的书籍ISBN;" << endl; getline(cin, isbn); auto it = find_if(books.begin(), books.end(),[&isbn](const Book& b){ return b.getISBN() == isbn; }); if (it != books.end()) { it->returnBook(); } else { cout << "未找到该书籍!" << endl; } } //删除书籍 void deleteBook() { string isbn; if (books.empty()) { cout << "图书列表为空,无法删除!" << endl; return; } cout << "请输入要删除的书籍ISBN" << endl; getline(cin, isbn); auto it = find_if(books.begin(), books.end(), [&isbn](const Book& b) { return b.getISBN() == isbn; }); if (it != books.end()) { books.erase(it); cout << "书籍删除成功!" << endl; } else { cout << "未找到该书籍" << endl; } } //排序书籍 void sortBook() { sort(books.begin(), books.end()); cout << "书籍排序成功" << endl; } }; void Menu() { cout << "-----------------------------------" << endl; cout << "欢迎来到BAIKAI图书馆" << endl; cout << "0.退出系统" << endl; cout << "1.登记书籍" << endl; cout << "2.浏览书籍" << endl; cout << "3.查找书籍" << endl; cout << "4.借阅书籍" << endl; cout << "5.归还书籍" << endl; cout << "6.书籍排序" << endl; cout << "7.删除书籍" << endl; cout << "-----------------------------------" << endl; cout << "请输入(0~7)" << endl; }; int main() { Library library; Menu(); int choice; cin >> choice; switch (choice) { case 0: cout << "退出成功,谢谢使用" << endl; system("pause"); exit(0); break; case 1: cout << "【登记】"; library.addBook(); break; case 2: cout << "【浏览】"; library.displayAllBooks(); break; case 3: cout << "【查找】"; library.searchBook(); break; case 4: cout << "【借阅】"; library.borrowBook(); break; case 5: cout << "【归还】"; library.returnBook(); break; case 6: cout << "【排序】"; library.sortBook(); break; case 7: cout << "【删除】"; library.deleteBook(); break; default:cout << "error" << endl; } return 0; }运行结果如下:二、核心逻辑
利用class定义book类型,如何通过library成员函数实现图书馆管理系统的不同功能
三、知识点讲解
OK,那现在来学习知识点
1.string是什么?
string是字符串类型但比char安全
2.Book(string t, string a, string i) :title(t), author(a), isbn(i), available(true){}是什么?
代表列表初始化,将括号内的参数赋值到对应的变量
Book(string t, string a, string i) :title(t), author(a), isbn(i), available(true){}3.如何理解vector<Book>books?
vector是一个动态数组,储存了Book类型的books变量
vector<Book>books;4.如何理解auto?
它是一个关键字,可以自动推导出变量类型
const auto& book : books5.如何理解string::npos?
它表示无效位置
string::npos6.如何理解auto it = find_if(books.begin(), books.end(), [&isbn](const Book& b) {
return b.getISBN() == isbn; });
find_if是库<algorithm>的标准函数用来查找元素
begin()和end()表示books容器的起止范围
[&isbn]表示捕获isbn列表
return表示判断条件
auto it = find_if(books.begin(), books.end(), [&isbn](const Book& b) { return b.getISBN() == isbn; });7.如何理解operator?
它是一个重载操作符,可以搭配+,==,<等使用,具体操作方法网上查
bool operator<(const Book& other) const { return title < other.title;OK,这就全部内容了,感谢您的观看!