项目案例一:基于C++的图书馆管理系统
项目思路
本文将为你介绍一个简单的图书馆管理系统,以代码示例方式呈现。
首先,让我们来了解一下图书馆管理系统中各个模块的功能。
Book 类
在图书馆管理系统中,Book 类代表着图书对象。它具有以下属性:
- title:图书标题
- author:作者名字
- isAvailable:可借状态
Book 类提供了构造函数和多个成员函数用于获取和设置图书属性,并能够将图书信息显示到控制台。
Library 类
图书馆被表示为 Library 类,其中包含以下内容:
- books:保存所有图书的向量容器
Library 类提供了一组操作函数,用于对图书馆进行管理:
- addBook:向图书馆添加新的图书。
- borrowBook:借阅指定标题的图书。
- returnBook:归还指定标题的图书。
- displayAllBooks:显示图书馆中所有图书的信息。
文件操作函数
为了封装文件操作,本系统提供了两个函数:
- readDataFromFile:从指定文件中读取数据,并返回一个保存每行数据的字符串向量。
- appendDataToFile:将给定的数据追加到指定文件的末尾。
这些函数使得可以方便地将数据保存到文件中并从文件读取数据。
主函数
在 main 函数中,我们创建了一个 Library 对象作为图书馆实例。然后使用 readDataFromFile 函数读取之前的操作记录,并将图书添加到图书馆。
接下来,通过一个循环菜单实现与用户的交互。以下是提供的选项:
- 添加图书:输入图书标题和作者名字,将一本新的图书添加到图书馆,并将此操作记录追加到文件中。
- 借阅图书:输入要借阅图书的标题,如果该图书可借,则设置其不可借状态,并将此操作记录追加到文件中。
- 归还图书:输入要归还图书的标题,如果该图书不可借,则将其设置为可借状态,并将此操作记录追加到文件中。
- 显示所有图书:展示图书馆中所有图书的详细信息。
- 退出程序:结束图书馆管理系统。
通过这个简单的图书馆管理系统,你可以轻松地添加、借阅和归还图书,并且能保存相关的操作记录。此外,你还可以方便地查看图书馆中所有图书的详细信息。
实现代码
#include <iostream> #include <fstream> #include <sstream> #include <vector> using namespace std; // Book类表示图书对象 class Book { private: string title; // 图书标题 string author; // 作者 bool isAvailable; // 可借状态 public: Book(const string& title, const string& author) : title(title), author(author), isAvailable(true) {} string getTitle() const { return title; } // 获取图书标题 string getAuthor() const { return author; } // 获取作者 bool getAvailability() const { return isAvailable; } // 获取可借状态 void setAvailability(bool availability) { isAvailable = availability; } // 设置可借状态 // 输出图书信息到控制台 void display() const { cout << "Title: " << title << endl; cout << "Author: " << author << endl; string availability = isAvailable ? "Available" : "Not Available"; cout << "Availability: " << availability << endl; } }; // Library类表示整个图书馆 class Library { private: vector<Book> books; // 图书集合 public: // 添加图书到图书馆 void addBook(const Book& book) { books.push_back(book); } // 借阅图书 void borrowBook(const string& title) { for (auto& book : books) { if (book.getTitle() == title && book.getAvailability()) { book.setAvailability(false); break; } } } // 归还图书 void returnBook(const string& title) { for (auto& book : books) { if (book.getTitle() == title && !book.getAvailability()) { book.setAvailability(true); break; } } } // 显示所有图书信息 void displayAllBooks() const { for (const auto& book : books) { book.display(); cout << endl; } } }; // 从文件中读取数据 vector<string> readDataFromFile(const string& filename) { ifstream file(filename); // 打开文件流 vector<string> data; if (file.is_open()) { string line; while (getline(file, line)) { // 逐行读取文件内容 data.push_back(line); } file.close(); // 关闭文件流 } return data; } // 将数据追加到文件末尾 void appendDataToFile(const string& filename, const string& data) { ofstream file(filename, ios::app); // 打开文件流,并将写入位置设为文件末尾 if (file.is_open()) { file << data << endl; // 写入数据 file.close(); // 关闭文件流 } } int main() { Library library; // 创建 Library 对象 vector<string> previousData = readDataFromFile("library.txt"); // 读取之前的操作记录 // 添加之前的图书到图书馆 for (const auto& line : previousData) { istringstream iss(line); // 将每行数据拆分为标题和作者 string title, author; getline(iss, title, ','); getline(iss, author); library.addBook(Book(title, author)); } int choice; while (true) { cout << "Library Management System" << endl; cout << "1. Add Book" << endl; cout << "2. Borrow Book" << endl; cout << "3. Return Book" << endl; cout << "4. Display All Books" << endl; cout << "5. Exit" << endl; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: { cout << "Enter book title: "; cin.ignore(); // 忽略之前的输入缓冲区 string title; getline(cin, title); cout << "Enter author name: "; string author; getline(cin, author); library.addBook(Book(title, author)); appendDataToFile("library.txt", "Added Book - Title: " + title + ", Author: " + author); break; } case 2: { cout << "Enter book title to borrow: "; cin.ignore(); string title; getline(cin, title); library.borrowBook(title); appendDataToFile("library.txt", "Borrowed Book - Title: " + title); break; } case 3: { cout << "Enter book title to return: "; cin.ignore(); string title; getline(cin, title); library.returnBook(title); appendDataToFile("library.txt", "Returned Book - Title: " + title); break; } case 4: library.displayAllBooks(); break; case 5: return 0; default: cout << "Invalid choice. Please try again." << endl; } cout << endl; } }
运行结果:
Library Management System 1. Add Book 2. Borrow Book 3. Return Book 4. Display All Books 5. Exit Enter your choice: 1 Enter book title: Harry Potter and the Philosopher's Stone Enter author name: J.K. Rowling Library Management System 1. Add Book 2. Borrow Book 3. Return Book 4. Display All Books 5. Exit Enter your choice: 1 Enter book title: To Kill a Mockingbird Enter author name: Harper Lee Library Management System 1. Add Book 2. Borrow Book 3. Return Book 4. Display All Books 5. Exit Enter your choice: 2 Enter book title to borrow: To Kill a Mockingbird Library Management System 1. Add Book 2. Borrow Book 3. Return Book 4. Display All Books 5. Exit Enter your choice: 4 Title: Harry Potter and the Philosopher's Stone Author: J.K. Rowling Availability: Available Title: To Kill a Mockingbird Author: Harper Lee Availability: Not Available Library Management System 1. Add Book 2. Borrow Book 3. Return Book 4. Display All Books 5. Exit Enter your choice: 3 Enter book title to return: To Kill a Mockingbird Library Management System 1. Add Book 2. Borrow Book 3. Return Book 4. Display All Books 5. Exit Enter your choice: 4 Title: Harry Potter and the Philosopher's Stone Author: J.K. Rowling Availability: Available Title: To Kill a Mockingbird Author: Harper Lee Availability: Available Library Management System 1. Add Book 2. Borrow Book 3. Return Book 4. Display All Books 5. Exit Enter your choice: 5