帮我用C++实现一个的完整图书管理系统,需要在控制台进行操作,需要有代码注释,要有书籍类、图书类型类、图书类,要有这些功能: 1)建立书籍类、图书类型类、图书类 2)书籍、图书类型、图书信息的初始化 3)书籍、图书类型、图书信息的添加、修改、删除、查找 4)图书信息的输出 5)对图书信息按编号、书名排序 6)将图书信息保存为文件 7)读入保存的图书信息
时间: 2023-08-11 21:04:40 浏览: 77
基于C++实现(控制台)图书管理系统【100012474】
5星 · 资源好评率100%
好的,这是一个比较复杂的项目,需要一步步来实现。我们可以先创建一个Book类,包含图书的编号、书名、作者、价格和库存量等信息。代码如下:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Book {
public:
string number; // 图书编号
string name; // 书名
string author; // 作者
double price; // 价格
int count; // 库存量
Book(string num, string na, string au, double pr, int co) {
number = num;
name = na;
author = au;
price = pr;
count = co;
}
};
```
接着,我们创建一个BookType类,包含图书类型的编号和名称等信息。代码如下:
```cpp
class BookType {
public:
string number; // 图书类型编号
string name; // 图书类型名称
BookType(string num, string na) {
number = num;
name = na;
}
};
```
接着,我们在Book类中添加一个成员变量,表示图书类型。代码如下:
```cpp
class Book {
public:
string number; // 图书编号
string name; // 书名
string author; // 作者
double price; // 价格
int count; // 库存量
BookType bookType; // 图书类型
Book(string num, string na, string au, double pr, int co, BookType bt) {
number = num;
name = na;
author = au;
price = pr;
count = co;
bookType = bt;
}
};
```
然后,我们创建一个BookManager类,包含对图书信息的添加、修改、删除和查找等操作。代码如下:
```cpp
#include <vector>
class BookManager {
public:
vector<Book> books; // 存储所有图书信息
void add(Book book) {
books.push_back(book);
}
void remove(string number) {
for (auto iter = books.begin(); iter != books.end(); iter++) {
if ((*iter).number == number) {
books.erase(iter);
break;
}
}
}
void modify(string number, Book book) {
for (auto iter = books.begin(); iter != books.end(); iter++) {
if ((*iter).number == number) {
(*iter).name = book.name;
(*iter).author = book.author;
(*iter).price = book.price;
(*iter).count = book.count;
(*iter).bookType = book.bookType;
break;
}
}
}
Book find(string number) {
for (auto iter = books.begin(); iter != books.end(); iter++) {
if ((*iter).number == number) {
return (*iter);
}
}
return Book("", "", "", 0, 0, BookType("", ""));
}
void sortByName() {
sort(books.begin(), books.end(), [](Book a, Book b) {
return a.name < b.name;
});
}
void sortByNumber() {
sort(books.begin(), books.end(), [](Book a, Book b) {
return a.number < b.number;
});
}
void saveToFile(string filename) {
ofstream out(filename);
for (auto book : books) {
out << book.number << "," << book.name << "," << book.author << "," << book.price << "," << book.count << "," << book.bookType.number << "," << book.bookType.name << endl;
}
out.close();
}
void readFromFile(string filename) {
ifstream in(filename);
string line;
while (getline(in, line)) {
stringstream ss(line);
string num, name, author, typeNum, typeName;
double price;
int count;
getline(ss, num, ',');
getline(ss, name, ',');
getline(ss, author, ',');
ss >> price;
ss.ignore();
ss >> count;
ss.ignore();
getline(ss, typeNum, ',');
getline(ss, typeName, ',');
BookType bt(typeNum, typeName);
Book book(num, name, author, price, count, bt);
add(book);
}
in.close();
}
};
```
最后,我们创建一个main函数,来测试上述功能。代码如下:
```cpp
int main() {
BookType bt1("001", "小说");
BookType bt2("002", "科技");
Book book1("001", "三体", "刘慈欣", 29.99, 100, bt2);
Book book2("002", "红楼梦", "曹雪芹", 19.99, 50, bt1);
Book book3("003", "C++程序设计", "谭浩强", 39.99, 80, bt2);
BookManager bm;
bm.add(book1);
bm.add(book2);
bm.add(book3);
bm.sortByName();
for (auto book : bm.books) {
cout << book.number << " " << book.name << " " << book.author << " " << book.price << " " << book.count << " " << book.bookType.number << " " << book.bookType.name << endl;
}
bm.sortByNumber();
for (auto book : bm.books) {
cout << book.number << " " << book.name << " " << book.author << " " << book.price << " " << book.count << " " << book.bookType.number << " " << book.bookType.name << endl;
}
bm.remove("001");
Book book = bm.find("002");
cout << book.name << endl;
bm.modify("002", Book("002", "红楼梦2", "曹雪芹", 29.99, 200, bt1));
book = bm.find("002");
cout << book.name << endl;
bm.saveToFile("books.txt");
bm.books.clear();
bm.readFromFile("books.txt");
for (auto book : bm.books) {
cout << book.number << " " << book.name << " " << book.author << " " << book.price << " " << book.count << " " << book.bookType.number << " " << book.bookType.name << endl;
}
return 0;
}
```
这个程序已经可以完成基本的图书管理功能了,包括对图书信息的添加、修改、删除和查找,以及对图书信息的排序和保存/读取功能。需要注意的是,保存到文件和读取文件的格式需要保持一致。
阅读全文