请帮我写一个书店管理系统的c++代码
时间: 2023-08-28 20:20:20 浏览: 88
好的,以下是一个简单的书店管理系统的C++代码,其中包括添加书籍、删除书籍、查询书籍、显示书籍信息等功能:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Book {
string name;
string author;
float price;
int quantity;
};
vector<Book> books;
void addBook() {
Book book;
cout << "Enter book name: ";
getline(cin, book.name);
cout << "Enter author name: ";
getline(cin, book.author);
cout << "Enter book price: ";
cin >> book.price;
cout << "Enter book quantity: ";
cin >> book.quantity;
books.push_back(book);
cout << "Book added successfully!" << endl;
}
void deleteBook() {
string name;
cout << "Enter book name to delete: ";
getline(cin, name);
for (int i = 0; i < books.size(); i++) {
if (books[i].name == name) {
books.erase(books.begin() + i);
cout << "Book deleted successfully!" << endl;
return;
}
}
cout << "Book not found!" << endl;
}
void searchBook() {
string name;
cout << "Enter book name to search: ";
getline(cin, name);
for (int i = 0; i < books.size(); i++) {
if (books[i].name == name) {
cout << "Book found!" << endl;
cout << "Name: " << books[i].name << endl;
cout << "Author: " << books[i].author << endl;
cout << "Price: " << books[i].price << endl;
cout << "Quantity: " << books[i].quantity << endl;
return;
}
}
cout << "Book not found!" << endl;
}
void displayBooks() {
if (books.size() == 0) {
cout << "No books in the store!" << endl;
return;
}
cout << "Books in the store:" << endl;
for (int i = 0; i < books.size(); i++) {
cout << "Book " << i+1 << ":" << endl;
cout << "Name: " << books[i].name << endl;
cout << "Author: " << books[i].author << endl;
cout << "Price: " << books[i].price << endl;
cout << "Quantity: " << books[i].quantity << endl;
}
}
int main() {
int choice;
do {
cout << "Bookstore management system" << endl;
cout << "1. Add book" << endl;
cout << "2. Delete book" << endl;
cout << "3. Search book" << endl;
cout << "4. Display books" << endl;
cout << "5. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
searchBook();
break;
case 4:
displayBooks();
break;
case 5:
break;
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 5);
return 0;
}
```
希望能对你有所帮助!
阅读全文