帮我编写一个图书管理系统,实现图书库存、借阅信息管理。每本图书的记录信息包括:书号、书名、作者、单价、库存数量、出版时间、出版单位等。
时间: 2023-07-04 17:05:33 浏览: 110
好的,下面是一个简单的图书管理系统的实现:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义 Book 类
class Book {
public:
string bookNo; // 书号
string bookName; // 书名
string author; // 作者
float price; // 单价
int quantity; // 库存数量
string publishDate; // 出版时间
string publisher; // 出版单位
// 构造函数
Book(string no, string name, string author, float price, int quantity, string date, string publisher) {
this->bookNo = no;
this->bookName = name;
this->author = author;
this->price = price;
this->quantity = quantity;
this->publishDate = date;
this->publisher = publisher;
}
};
// 定义 Library 类
class Library {
public:
vector<Book> books; // 保存所有图书信息的容器
// 添加一本图书
void addBook(Book book) {
books.push_back(book);
cout << "添加成功!" << endl;
}
// 删除一本图书
void deleteBook(string bookNo) {
for (int i = 0; i < books.size(); i++) {
if (books[i].bookNo == bookNo) {
books.erase(books.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该书号的图书!" << endl;
}
// 查找一本图书
void searchBook(string bookNo) {
for (int i = 0; i < books.size(); i++) {
if (books[i].bookNo == bookNo) {
cout << "书号:" << books[i].bookNo << endl;
cout << "书名:" << books[i].bookName << endl;
cout << "作者:" << books[i].author << endl;
cout << "单价:" << books[i].price << endl;
cout << "库存数量:" << books[i].quantity << endl;
cout << "出版时间:" << books[i].publishDate << endl;
cout << "出版单位:" << books[i].publisher << endl;
return;
}
}
cout << "未找到该书号的图书!" << endl;
}
// 显示所有图书
void showAllBooks() {
cout << "共有 " << books.size() << " 本图书:" << endl;
for (int i = 0; i < books.size(); i++) {
cout << "书号:" << books[i].bookNo << endl;
cout << "书名:" << books[i].bookName << endl;
cout << "作者:" << books[i].author << endl;
cout << "单价:" << books[i].price << endl;
cout << "库存数量:" << books[i].quantity << endl;
cout << "出版时间:" << books[i].publishDate << endl;
cout << "出版单位:" << books[i].publisher << endl;
cout << endl;
}
}
};
int main() {
Library library;
bool quit = false;
while (!quit) {
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: {
string no, name, author, date, publisher;
float price;
int quantity;
cout << "请输入书号、书名、作者、单价、库存数量、出版时间、出版单位:" << endl;
cin >> no >> name >> author >> price >> quantity >> date >> publisher;
Book book(no, name, author, price, quantity, date, publisher);
library.addBook(book);
break;
}
case 2: {
string bookNo;
cout << "请输入要删除的图书的书号:" << endl;
cin >> bookNo;
library.deleteBook(bookNo);
break;
}
case 3: {
string bookNo;
cout << "请输入要查找的图书的书号:" << endl;
cin >> bookNo;
library.searchBook(bookNo);
break;
}
case 4: {
library.showAllBooks();
break;
}
case 5: {
quit = true;
break;
}
default: {
cout << "输入有误,请重新选择!" << endl;
break;
}
}
}
return 0;
}
```
这个程序使用了 `Book` 类来保存每一本图书的信息,并使用 `vector` 容器来保存所有图书的信息。`Library` 类封装了对图书信息的操作,如添加图书、删除图书、查找图书、显示所有图书等。在 `main` 函数中,我们通过一个循环来不断接收用户的操作,并调用相应的函数来执行操作。
阅读全文