帮我写一份图书管理系统,运用C++语言描述书籍类、图书类型类、图书类。设计基类和派生类,复杂点的,需要有注释,并且需要有以下内容:1)建立书籍类、图书类型类、图书类 2)书籍、图书类型、图书信息的初始化 3)书籍、图书类型、图书信息的添加、修改、删除、查找 4)图书信息的输出 5)对图书信息按编号、书名排序 6)将图书信息保存为文件 7)读取图书文件信息
时间: 2023-08-11 07:03:46 浏览: 114
好的,我会为您提供一个简单的图书管理系统的C++代码。请注意,代码仅供参考,可能需要根据您的具体需求进行修改。
首先,我们需要设计三个类:书籍类、图书类型类和图书类。这些类应该包含各自的成员变量和成员函数。
以下是这三个类的基础代码:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
// 书籍类
class Book {
public:
Book(int id, string title, string author, string publisher, int year, int type);
int getID();
string getTitle();
string getAuthor();
string getPublisher();
int getYear();
int getType();
private:
int m_id;
string m_title;
string m_author;
string m_publisher;
int m_year;
int m_type;
};
// 图书类型类
class BookType {
public:
BookType(int id, string name);
int getID();
string getName();
private:
int m_id;
string m_name;
};
// 图书类
class Library {
public:
void addBook(int id, string title, string author, string publisher, int year, int type);
void modifyBook(int id, string title, string author, string publisher, int year, int type);
void deleteBook(int id);
Book* findBook(int id);
void sortByID();
void sortByTitle();
void saveToFile();
void readFromFile();
private:
vector<Book*> m_books;
vector<BookType*> m_types;
};
```
接下来,我们需要实现这三个类的成员函数。下面是这些函数的实现代码。
```c++
// 书籍类的成员函数
Book::Book(int id, string title, string author, string publisher, int year, int type)
: m_id(id), m_title(title), m_author(author), m_publisher(publisher), m_year(year), m_type(type) {}
int Book::getID() { return m_id; }
string Book::getTitle() { return m_title; }
string Book::getAuthor() { return m_author; }
string Book::getPublisher() { return m_publisher; }
int Book::getYear() { return m_year; }
int Book::getType() { return m_type; }
// 图书类型类的成员函数
BookType::BookType(int id, string name) : m_id(id), m_name(name) {}
int BookType::getID() { return m_id; }
string BookType::getName() { return m_name; }
// 图书类的成员函数
void Library::addBook(int id, string title, string author, string publisher, int year, int type) {
Book* book = new Book(id, title, author, publisher, year, type);
m_books.push_back(book);
}
void Library::modifyBook(int id, string title, string author, string publisher, int year, int type) {
Book* book = findBook(id);
if (book != nullptr) {
book->m_title = title;
book->m_author = author;
book->m_publisher = publisher;
book->m_year = year;
book->m_type = type;
}
}
void Library::deleteBook(int id) {
for (vector<Book*>::iterator it = m_books.begin(); it != m_books.end(); it++) {
if ((*it)->getID() == id) {
m_books.erase(it);
break;
}
}
}
Book* Library::findBook(int id) {
for (vector<Book*>::iterator it = m_books.begin(); it != m_books.end(); it++) {
if ((*it)->getID() == id) {
return (*it);
}
}
return nullptr;
}
void Library::sortByID() {
sort(m_books.begin(), m_books.end(), [](Book* a, Book* b) { return a->getID() < b->getID(); });
}
void Library::sortByTitle() {
sort(m_books.begin(), m_books.end(), [](Book* a, Book* b) { return a->getTitle() < b->getTitle(); });
}
void Library::saveToFile() {
ofstream file("books.txt");
for (vector<Book*>::iterator it = m_books.begin(); it != m_books.end(); it++) {
file << (*it)->getID() << "," << (*it)->getTitle() << "," << (*it)->getAuthor() << ","
<< (*it)->getPublisher() << "," << (*it)->getYear() << "," << (*it)->getType() << endl;
}
file.close();
}
void Library::readFromFile() {
ifstream file("books.txt");
if (file.is_open()) {
string line;
while (getline(file, line)) {
int id, year, type;
string title, author, publisher;
sscanf_s(line.c_str(), "%d,%[^,],%[^,],%[^,],%d,%d", &id, &title, &author, &publisher, &year, &type);
addBook(id, title, author, publisher, year, type);
}
file.close();
}
}
```
最后,我们需要在主函数中创建Library对象,并通过调用其成员函数来操作图书信息。以下是一个简单的示例:
```c++
int main() {
Library lib;
// 从文件中读取图书信息
lib.readFromFile();
// 添加图书
lib.addBook(1, "C++ Primer", "Lippman", "Addison-Wesley", 2012, 1);
lib.addBook(2, "Effective C++", "Meyers", "Addison-Wesley", 2005, 1);
lib.addBook(3, "The C++ Programming Language", "Stroustrup", "Addison-Wesley", 2013, 1);
// 修改图书
lib.modifyBook(2, "Effective C++", "Meyers", "Addison-Wesley", 2014, 1);
// 按编号排序
lib.sortByID();
// 按书名排序
lib.sortByTitle();
// 删除图书
lib.deleteBook(3);
// 将图书信息保存到文件
lib.saveToFile();
return 0;
}
```
这就是一个简单的图书管理系统的C++代码。希望对您有所帮助!
阅读全文