写一个图书管理系统c++,包括图书信息录入,修改,查询,删除
时间: 2024-03-24 12:36:03 浏览: 17
好的,这里是一个较为完整的图书管理系统的C++代码示例:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Book {
public:
string title;
string author;
string publisher;
int year;
int stock;
Book(string title, string author, string publisher, int year, int stock) {
this->title = title;
this->author = author;
this->publisher = publisher;
this->year = year;
this->stock = stock;
}
};
class Library {
public:
vector<Book> books;
void addBook() {
string title, author, publisher;
int year, stock;
cout << "Enter book title: ";
getline(cin, title);
cout << "Enter book author: ";
getline(cin, author);
cout << "Enter book publisher: ";
getline(cin, publisher);
cout << "Enter book year: ";
cin >> year;
cout << "Enter book stock: ";
cin >> stock;
Book book(title, author, publisher, year, stock);
books.push_back(book);
cout << "Book added successfully." << endl;
}
void modifyBook() {
string title;
bool found = false;
cout << "Enter book title to modify: ";
getline(cin, title);
for (int i = 0; i < books.size(); i++) {
if (books[i].title == title) {
found = true;
cout << "Enter new book title: ";
getline(cin, books[i].title);
cout << "Enter new book author: ";
getline(cin, books[i].author);
cout << "Enter new book publisher: ";
getline(cin, books[i].publisher);
cout << "Enter new book year: ";
cin >> books[i].year;
cout << "Enter new book stock: ";
cin >> books[i].stock;
cout << "Book modified successfully." << endl;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
}
void searchBook() {
string title;
bool found = false;
cout << "Enter book title to search: ";
getline(cin, title);
for (int i = 0; i < books.size(); i++) {
if (books[i].title == title) {
found = true;
cout << "Title: " << books[i].title << endl;
cout << "Author: " << books[i].author << endl;
cout << "Publisher: " << books[i].publisher << endl;
cout << "Year: " << books[i].year << endl;
cout << "Stock: " << books[i].stock << endl;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
}
void deleteBook() {
string title;
bool found = false;
cout << "Enter book title to delete: ";
getline(cin, title);
for (int i = 0; i < books.size(); i++) {
if (books[i].title == title) {
found = true;
books.erase(books.begin() + i);
cout << "Book deleted successfully." << endl;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
}
void displayBooks() {
for (int i = 0; i < books.size(); i++) {
cout << "Title: " << books[i].title << endl;
cout << "Author: " << books[i].author << endl;
cout << "Publisher: " << books[i].publisher << endl;
cout << "Year: " << books[i].year << endl;
cout << "Stock: " << books[i].stock << endl;
cout << endl;
}
}
};
int main() {
Library library;
while (true) {
cout << "1. Add book" << endl;
cout << "2. Modify book" << endl;
cout << "3. Search book" << endl;
cout << "4. Delete book" << endl;
cout << "5. Display all books" << endl;
cout << "6. Exit" << endl;
int choice;
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
library.addBook();
break;
case 2:
library.modifyBook();
break;
case 3:
library.searchBook();
break;
case 4:
library.deleteBook();
break;
case 5:
library.displayBooks();
break;
case 6:
return 0;
default:
cout << "Invalid choice." << endl;
}
}
return 0;
}
```
这个图书管理系统使用了两个类,`Book`和`Library`。`Book`类表示一本书,包含书名、作者、出版社、出版年份和库存等属性。`Library`类表示整个图书馆,包含一个`vector`存储所有书籍,还实现了添加、修改、查询、删除和展示书籍的功能。
在`main`函数中,我们使用一个无限循环来实现菜单,并根据用户不同的选择调用`Library`类中的不同函数来执行相应的功能。
这个图书管理系统还可以进一步完善,例如可以添加借书和还书的功能,或者将数据存储在文件中以实现持久化等。