#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义图书结构体
struct Book {
int id; // 图书编号
string name; // 图书名称
int quality; // 图书数量
};
// 定义图书馆类
class Library {
private:
vector<Book> books; // 存储图书的向量
public:
// 图书录入功能
void addBook(int id, string name, int quality) {
Book book = {id, name, quality};
books.push_back(book);
cout << "图书录入成功!" << endl;
}
// 图书查询功能
void searchBook(int id) {
for (int i = 0; i < books.size(); i++) {
if (books[i].id == id) {
cout << "图书编号:" << books[i].id << endl;
cout << "图书名称:" << books[i].name << endl;
cout << "图书数量:" << books[i].quality << endl;
return;
}
}
cout << "未找到该图书!" << endl;
}
// 图书插入功能
void insertBook(int index, int id, string name, int quality) {
Book book = {id, name, quality};
books.insert(books.begin() + index, book);
cout << "图书插入成功!" << endl;
}
// 图书修改功能
void modifyBook(int id, string name, int quality) {
for (int i = 0; i < books.size(); i++) {
if (books[i].id == id) {
books[i].name = name;
books[i].quality = quality;
cout << "图书修改成功!" << endl;
return;
}
}
cout << "未找到该图书!" << endl;
}
// 图书删除功能
void deleteBook(int id) {
for (int i = 0; i < books.size(); i++) {
if (books[i].id == id) {
books.erase(books.begin() + i);
cout << "图书删除成功!" << endl;
return;
}
}
cout << "未找到该图书!" << endl;
}
// 图书添加功能
void addQuality(int id, int quality) {
for (int i = 0; i < books.size(); i++) {
if (books[i].id == id) {
books[i].quality += quality;
cout << "图书添加成功!" << endl;
return;
}
}
cout << "未找到该图书!" << endl;
}
};
int main() {
Library library;
int choice, id, quality, index;
string name;
while (true) {
cout << "请选择功能:" << endl;
cout << "1. 图书录入" << endl;
cout << "2. 图书查询" << endl;
cout << "3. 图书插入" << endl;
cout << "4. 图书修改" << endl;
cout << "5. 图书删除" << endl;
cout << "6. 图书添加" << endl;
cout << "7. 退出" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入图书编号、名称和数量:" << endl;
cin >> id >> name >> quality;
library.addBook(id, name, quality);
break;
case 2:
cout << "请输入要查询的图书编号:" << endl;
cin >> id;
library.searchBook(id);
break;
case 3:
cout << "请输入要插入的位置、图书编号、名称和数量:" << endl;
cin >> index >> id >> name >> quality;
library.insertBook(index, id, name, quality);
break;
case 4:
cout << "请输入要修改的图书编号、名称和数量:" << endl;
cin >> id >> name >> quality;
library.modifyBook(id, name, quality);
break;
case 5:
cout << "请输入要删除的图书编号:" << endl;
cin >> id;
library.deleteBook(id);
break;
case 6:
cout << "请输入要添加数量的图书编号和数量:" << endl;
cin >> id >> quality;
library.addQuality(id, quality);
break;
case 7:
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
}
return 0;
}