帮我写一个图书馆管理系统 菜单包括:查询、添加、删除和修改
时间: 2023-08-11 08:05:03 浏览: 41
好的,下面是一个简单的图书馆管理系统的示例代码:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 定义图书类
class Book {
public:
Book(string bookName, string author, int pages) {
this->bookName = bookName;
this->author = author;
this->pages = pages;
}
string getBookName() const {
return bookName;
}
string getAuthor() const {
return author;
}
int getPages() const {
return pages;
}
private:
string bookName;
string author;
int pages;
};
// 定义图书馆类
class Library {
public:
// 添加图书
void addBook(Book book) {
books.push_back(book);
cout << "添加成功!" << endl;
}
// 删除图书
void deleteBook(string bookName) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getBookName() == bookName) {
books.erase(books.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该书籍!" << endl;
}
// 修改图书
void updateBook(string bookName, string author, int pages) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getBookName() == bookName) {
books[i] = Book(bookName, author, pages);
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该书籍!" << endl;
}
// 查询图书
void queryBook(string bookName) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getBookName() == bookName) {
cout << "书名:" << books[i].getBookName() << endl;
cout << "作者:" << books[i].getAuthor() << endl;
cout << "页数:" << books[i].getPages() << endl;
return;
}
}
cout << "未找到该书籍!" << endl;
}
private:
vector<Book> books;
};
// 主函数
int main() {
Library library;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 查询图书" << endl;
cout << "2. 添加图书" << endl;
cout << "3. 删除图书" << endl;
cout << "4. 修改图书" << endl;
cout << "0. 退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入书名:" << endl;
string bookName;
cin >> bookName;
library.queryBook(bookName);
break;
case 2:
cout << "请输入书名、作者、页数(用空格分隔):" << endl;
string addBookName;
string addAuthor;
int addPages;
cin >> addBookName >> addAuthor >> addPages;
library.addBook(Book(addBookName, addAuthor, addPages));
break;
case 3:
cout << "请输入要删除的书名:" << endl;
string deleteBookName;
cin >> deleteBookName;
library.deleteBook(deleteBookName);
break;
case 4:
cout << "请输入要修改的书名、作者、页数(用空格分隔):" << endl;
string updateBookName;
string updateAuthor;
int updatePages;
cin >> updateBookName >> updateAuthor >> updatePages;
library.updateBook(updateBookName, updateAuthor, updatePages);
break;
case 0:
return 0;
default:
cout << "输入无效,请重新输入!" << endl;
}
}
}
```
这个示例代码的功能比较简单,可以根据需要进行修改和完善。