设计一个c++的图书管理系统
时间: 2023-10-24 17:08:41 浏览: 53
好的,下面是一个简单的C++图书管理系统的设计:
1. 需要创建一个Book类,用于存储每本书的信息,包括书名、作者、出版社、出版日期、ISBN号等信息。
2. 需要创建一个Library类,用于管理所有的书。Library类应该包括添加一本新书、删除一本书、查找一本书、显示所有书籍等方法。
3. 在主函数中,可以创建一个Library对象,并通过菜单选项允许用户添加、删除、查找和显示书籍信息。
下面是一个例子:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Book类
class Book {
public:
Book(string title, string author, string publisher, string pubDate, string isbn) {
this->title = title;
this->author = author;
this->publisher = publisher;
this->pubDate = pubDate;
this->isbn = isbn;
}
string getTitle() {
return title;
}
string getAuthor() {
return author;
}
string getPublisher() {
return publisher;
}
string getPubDate() {
return pubDate;
}
string getIsbn() {
return isbn;
}
private:
string title;
string author;
string publisher;
string pubDate;
string isbn;
};
// Library类
class Library {
public:
// 添加一本新书
void addBook(Book book) {
books.push_back(book);
}
// 删除一本书
void deleteBook(string isbn) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getIsbn() == isbn) {
books.erase(books.begin() + i);
return;
}
}
cout << "Book not found." << endl;
}
// 查找一本书
void findBook(string isbn) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getIsbn() == isbn) {
cout << "Title: " << books[i].getTitle() << endl;
cout << "Author: " << books[i].getAuthor() << endl;
cout << "Publisher: " << books[i].getPublisher() << endl;
cout << "Publication date: " << books[i].getPubDate() << endl;
cout << "ISBN: " << books[i].getIsbn() << endl;
return;
}
}
cout << "Book not found." << endl;
}
// 显示所有书籍
void displayBooks() {
for (int i = 0; i < books.size(); i++) {
cout << "Title: " << books[i].getTitle() << endl;
cout << "Author: " << books[i].getAuthor() << endl;
cout << "Publisher: " << books[i].getPublisher() << endl;
cout << "Publication date: " << books[i].getPubDate() << endl;
cout << "ISBN: " << books[i].getIsbn() << endl << endl;
}
}
private:
vector<Book> books;
};
int main() {
Library library;
int choice;
string title, author, publisher, pubDate, isbn;
while (true) {
cout << "1. Add book" << endl;
cout << "2. Delete book" << endl;
cout << "3. Find book" << endl;
cout << "4. Display all books" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter title: ";
cin >> title;
cout << "Enter author: ";
cin >> author;
cout << "Enter publisher: ";
cin >> publisher;
cout << "Enter publication date: ";
cin >> pubDate;
cout << "Enter ISBN: ";
cin >> isbn;
library.addBook(Book(title, author, publisher, pubDate, isbn));
break;
case 2:
cout << "Enter ISBN: ";
cin >> isbn;
library.deleteBook(isbn);
break;
case 3:
cout << "Enter ISBN: ";
cin >> isbn;
library.findBook(isbn);
break;
case 4:
library.displayBooks();
break;
case 5:
exit(0);
default:
cout << "Invalid choice." << endl;
}
}
return 0;
}
```
以上是一个简单的C++图书管理系统的设计,您可以根据需求进行调整和修改。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)