C++实现简单版图书管理系统攻略
本文将介绍如何使用C++语言实现简单版图书管理系统。本系统主要包含以下功能:添加图书信息、删除图书信息、查看图书信息、修改图书信息、退出系统。
设计思路
在开始实现之前,我们需要先确定程序的设计思路。将所有的操作封装成一个类,来实现图书的添加、删除、修改、查询等操作。同时,我们需要设计出一个图书类,包含图书的基本信息。
代码实现
Book类
我们定义了一个Book类,包含以下属性:
- 书名
- 作者
- 出版社
- ISBN号
- 价格
同时,我们也定义了一些public的方法,用于获取属性和设置属性。
#include <iostream>
#include <string>
class Book {
private:
std::string name;
std::string author;
std::string publisher;
std::string isbn;
double price;
public:
Book(std::string name, std::string author, std::string publisher, std::string isbn, double price) {
this->name = name;
this->author = author;
this->publisher = publisher;
this->isbn = isbn;
this->price = price;
}
std::string get_name() {
return name;
}
std::string get_author() {
return author;
}
std::string get_publisher() {
return publisher;
}
std::string get_isbn() {
return isbn;
}
double get_price() {
return price;
}
void set_name(std::string name) {
this->name = name;
}
void set_author(std::string author) {
this->author = author;
}
void set_publisher(std::string publisher) {
this->publisher = publisher;
}
void set_isbn(std::string isbn) {
this->isbn = isbn;
}
void set_price(double price) {
this->price = price;
}
void print_info() {
std::cout << "Book Name: " << name << std::endl;
std::cout << "Author: " << author << std::endl;
std::cout << "Publisher: " << publisher << std::endl;
std::cout << "ISBN: " << isbn << std::endl;
std::cout << "Price: " << price << std::endl;
}
};
Library类
然后我们定义了一个Library类,包含以下方法:
- add_book: 添加一本新书
- remove_book: 删除一本图书
- search_book: 按照名称或ISBN查找图书
- modify_price: 修改图书价格
- print_all_books: 打印所有图书信息
#include <vector>
#include <algorithm>
class Library {
private:
std::vector<Book> books;
public:
void add_book(Book book) {
books.push_back(book);
}
void remove_book(std::string key) {
auto it = std::remove_if(books.begin(), books.end(), [&](Book book){
return book.get_name() == key || book.get_isbn() == key;
});
books.erase(it, books.end());
}
Book search_book(std::string key) {
for (auto book: books) {
if (book.get_name() == key || book.get_isbn() == key) {
return book;
}
}
return nullptr;
}
void modify_price(std::string key, double price) {
Book book = search_book(key);
if (book != nullptr) {
book.set_price(price);
}
}
void print_all_books() {
for (auto book: books) {
book.print_info();
}
}
};
main函数
最后,我们使用main函数实现整个程序的流程。
int main() {
Library library;
while(true) {
std::cout << "Please select operation: " << std::endl;
std::cout << "1. Add book" << std::endl;
std::cout << "2. Remove book" << std::endl;
std::cout << "3. Search book" << std::endl;
std::cout << "4. Modify price" << std::endl;
std::cout << "5. Print all books" << std::endl;
std::cout << "6. Quit" << std::endl;
int choice;
std::cin >> choice;
if (choice == 1) {
std::string name, author, publisher, isbn;
double price;
std::cout << "Please input book name:" << std::endl;
std::cin >> name;
std::cout << "Please input author name:" << std::endl;
std::cin >> author;
std::cout << "Please input publisher name:" << std::endl;
std::cin >> publisher;
std::cout << "Please input ISBN:" << std::endl;
std::cin >> isbn;
std::cout << "Please input price:" << std::endl;
std::cin >> price;
Book book(name, author, publisher, isbn, price);
library.add_book(book);
} else if (choice == 2) {
std::string key;
std::cout << "Please input name or ISBN of book to remove:" << std::endl;
std::cin >> key;
library.remove_book(key);
} else if (choice == 3) {
std::string key;
std::cout << "Please input name or ISBN of book to search:" << std::endl;
std::cin >> key;
Book book = library.search_book(key);
if (book == nullptr) {
std::cout << "No book found." << std::endl;
} else {
book.print_info();
}
} else if (choice == 4) {
std::string key;
double price;
std::cout << "Please input name or ISBN of book to modify:" << std::endl;
std::cin >> key;
std::cout << "Please input new price:" << std::endl;
std::cin >> price;
library.modify_price(key, price);
} else if (choice == 5) {
library.print_all_books();
} else if (choice == 6) {
break;
} else {
std::cout << "Invalid choice." << std::endl;
}
}
return 0;
}
示例
添加图书
选择1,添加一本新书,输入以下信息:
Please input book name:
C++ Primer
Please input author name:
Stanley B. Lippman
Please input publisher name:
Pearson
Please input ISBN:
978-0-321-71411-4
Please input price:
100
添加成功后,可以在"Print all books"中查看是否添加成功。
删除图书
选择2,删除指定书籍,输入以下信息:
Please input name or ISBN of book to remove:
C++ Primer
选择3或者5可以查看是否已删除。
查看图书
选择3,查找指定图书,输入以下信息:
Please input name or ISBN of book to search:
The C Programming Language
可以查看该图书的详细信息。
修改图书价格
选择4,修改指定书籍价格,输入以下信息:
Please input name or ISBN of book to modify:
C++ Primer
Please input new price:
200
修改成功后,可以在"Print all books"中查看是否修改成功。
总结
本文介绍了C++实现简单版图书管理系统的方法和思路,详细介绍了Book类和Library类的实现方法,同时给出了一个相对完整的示例。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C++实现简单版图书管理系统 - Python技术站