写一个包含增删改查的图书管理系统
时间: 2023-05-13 18:05:29 浏览: 135
好的,以下是一个简单的图书管理系统的代码示例:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Book {
string title;
string author;
int year;
};
vector<Book> books;
void addBook() {
Book book;
cout << "请输入书名:";
cin >> book.title;
cout << "请输入作者:";
cin >> book.author;
cout << "请输入出版年份:";
cin >> book.year;
books.push_back(book);
cout << "添加成功!" << endl;
}
void deleteBook() {
string title;
cout << "请输入要删除的书名:";
cin >> title;
for (int i = 0; i < books.size(); i++) {
if (books[i].title == title) {
books.erase(books.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该书!" << endl;
}
void updateBook() {
string title;
cout << "请输入要修改的书名:";
cin >> title;
for (int i = 0; i < books.size(); i++) {
if (books[i].title == title) {
cout << "请输入新的书名:";
cin >> books[i].title;
cout << "请输入新的作者:";
cin >> books[i].author;
cout << "请输入新的出版年份:";
cin >> books[i].year;
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该书!" << endl;
}
void searchBook() {
string title;
cout << "请输入要查找的书名:";
cin >> title;
for (int i = 0; i < books.size(); i++) {
if (books[i].title == title) {
cout << "书名:" << books[i].title << endl;
cout << "作者:" << books[i].author << endl;
cout << "出版年份:" << books[i].year << endl;
return;
}
}
cout << "未找到该书!" << endl;
}
int main() {
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加书籍" << endl;
cout << "2. 删除书籍" << endl;
cout << "3. 修改书籍" << endl;
cout << "4. 查找书籍" << endl;
cout << "5. 退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
updateBook();
break;
case 4:
searchBook();
break;
case 5:
return 0;
default:
cout << "无效的选择!" << endl;
}
}
return 0;
}
```
这个程序包含了增加、删除、修改和查找书籍的功能。用户可以通过输入数字选择要进行的操作。
阅读全文