图书管理系统设计与实现用C++代码展示
时间: 2023-12-14 14:03:19 浏览: 33
抱歉,我作为一个语言模型AI,无法展示代码,但我可以给您提供一份图书管理系统设计与实现的C++代码框架供您参考:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// 图书类
class Book {
public:
string name; // 书名
string author; // 作者
string publisher; // 出版社
int year; // 出版年份
double price; // 价格
int quantity; // 数量
Book() {} // 默认构造函数
Book(string n, string a, string p, int y, double pr, int q) { // 带参构造函数
name = n;
author = a;
publisher = p;
year = y;
price = pr;
quantity = q;
}
// 显示图书信息
void show() {
cout << "书名: " << name << endl;
cout << "作者: " << author << endl;
cout << "出版社: " << publisher << endl;
cout << "出版年份: " << year << endl;
cout << "价格: " << price << endl;
cout << "数量: " << quantity << endl;
}
};
// 图书管理系统类
class Library {
private:
vector<Book> books; // 图书列表
public:
// 添加图书
void add_book(Book book) {
books.push_back(book);
}
// 显示所有图书
void show_all_books() {
for (int i = 0; i < books.size(); i++) {
books[i].show();
cout << endl;
}
}
// 搜索图书
void search_book(string name) {
bool found = false;
for (int i = 0; i < books.size(); i++) {
if (books[i].name == name) {
books[i].show();
found = true;
}
}
if (!found) {
cout << "未找到该书籍!" << endl;
}
}
// 删除图书
void delete_book(string name) {
for (int i = 0; i < books.size(); i++) {
if (books[i].name == name) {
books.erase(books.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该书籍!" << endl;
}
// 保存图书信息到文件
void save_books_to_file(string filename) {
ofstream outfile(filename);
for (int i = 0; i < books.size(); i++) {
outfile << books[i].name << "," << books[i].author << "," << books[i].publisher << "," << books[i].year << "," << books[i].price << "," << books[i].quantity << endl;
}
outfile.close();
cout << "保存成功!" << endl;
}
// 从文件读取图书信息
void load_books_from_file(string filename) {
ifstream infile(filename);
string line;
while (getline(infile, line)) {
string name, author, publisher;
int year, quantity;
double price;
int pos1 = line.find(",");
name = line.substr(0, pos1);
int pos2 = line.find(",", pos1 + 1);
author = line.substr(pos1 + 1, pos2 - pos1 - 1);
int pos3 = line.find(",", pos2 + 1);
publisher = line.substr(pos2 + 1, pos3 - pos2 - 1);
int pos4 = line.find(",", pos3 + 1);
year = stoi(line.substr(pos3 + 1, pos4 - pos3 - 1));
int pos5 = line.find(",", pos4 + 1);
price = stod(line.substr(pos4 + 1, pos5 - pos4 - 1));
quantity = stoi(line.substr(pos5 + 1));
Book book(name, author, publisher, year, price, quantity);
add_book(book);
}
infile.close();
cout << "读取成功!" << endl;
}
};
int main() {
Library library;
library.load_books_from_file("books.txt");
while (true) {
cout << "请输入操作指令(1: 显示所有图书 2: 搜索图书 3: 删除图书 4: 保存图书信息 0: 退出程序): ";
int choice;
cin >> choice;
if (choice == 1) {
library.show_all_books();
} else if (choice == 2) {
cout << "请输入要搜索的图书名称: ";
string name;
cin >> name;
library.search_book(name);
} else if (choice == 3) {
cout << "请输入要删除的图书名称: ";
string name;
cin >> name;
library.delete_book(name);
} else if (choice == 4) {
library.save_books_to_file("books.txt");
} else if (choice == 0) {
break;
} else {
cout << "无效操作指令,请重新输入!" << endl;
}
}
return 0;
}
```
这份代码实现了一个简单的图书管理系统,包含添加图书、显示所有图书、搜索图书、删除图书、保存图书信息到文件以及从文件读取图书信息等功能。您可以根据您的需求进行修改和扩展。