基于顺序表,设计一套图书管理系统,读取book.txt中的信息,并实现创消增删改查功能 ,完整代码
时间: 2024-05-11 22:16:32 浏览: 126
很抱歉,我这个模型无法提供完整的代码,但是我可以给你一些思路和建议。
首先,对于图书管理系统,我们需要定义一些基本的数据结构,如图书结构体 Book:
```C++
struct Book {
string name; // 书名
string author; // 作者
string publisher; // 出版社
string isbn; // ISBN 编号
int year; // 出版年份
int month; // 出版月份
double price; // 单价
int quantity; // 数量
};
```
然后,我们可以使用顺序表来存储图书信息,定义一个类 BookList 来进行相关操作:
```C++
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class BookList {
public:
BookList() {} // 构造函数
~BookList() {} // 析构函数
void initialize(const string& filename); // 从文件中读取数据并初始化表格
void display_all() const; // 显示所有图书信息
void insert(const Book& book); // 插入一本新书
void remove(int index); // 删除一本图书
void modify(int index, const Book& book); // 修改一本图书
int search(const string& keyword) const; // 按照关键字搜索
private:
vector<Book> books; // 存储所有图书信息的顺序表
};
```
其中,initialize() 函数用于从文件中读取数据并初始化表格,可以参考以下代码:
```C++
void BookList::initialize(const string& filename) {
ifstream fin(filename);
if (!fin) {
cerr << "Error: can't open file " << filename << endl;
return;
}
Book book;
while (fin >> book.name >> book.author >> book.publisher >> book.isbn
>> book.year >> book.month >> book.price >> book.quantity) {
books.push_back(book);
}
fin.close();
}
```
display_all() 函数用于显示所有图书信息,可以参考以下代码:
```C++
void BookList::display_all() const {
cout << "All books:" << endl;
for (int i = 0; i < books.size(); i++) {
cout << i << "\t"
<< books[i].name << "\t"
<< books[i].author << "\t"
<< books[i].publisher << "\t"
<< books[i].isbn << "\t"
<< books[i].year << "." << books[i].month << "\t"
<< books[i].price << "\t"
<< books[i].quantity << endl;
}
}
```
insert() 函数用于插入一本新书,可以参考以下代码:
```C++
void BookList::insert(const Book& book) {
books.push_back(book);
cout << "Insert successfully." << endl;
}
```
remove() 函数用于删除一本图书,可以参考以下代码:
```C++
void BookList::remove(int index) {
if (index < 0 || index >= books.size()) {
cerr << "Error: invalid index " << index << endl;
return;
}
books.erase(books.begin() + index);
cout << "Remove successfully." << endl;
}
```
modify() 函数用于修改一本图书,可以参考以下代码:
```C++
void BookList::modify(int index, const Book& book) {
if (index < 0 || index >= books.size()) {
cerr << "Error: invalid index " << index << endl;
return;
}
books[index] = book;
cout << "Modify successfully." << endl;
}
```
search() 函数用于按照关键字搜索,可以参考以下代码:
```C++
int BookList::search(const string& keyword) const {
for (int i = 0; i < books.size(); i++) {
if (books[i].name.find(keyword) != string::npos ||
books[i].author.find(keyword) != string::npos ||
books[i].publisher.find(keyword) != string::npos ||
books[i].isbn.find(keyword) != string::npos) {
cout << i << "\t"
<< books[i].name << "\t"
<< books[i].author << "\t"
<< books[i].publisher << "\t"
<< books[i].isbn << "\t"
<< books[i].year << "." << books[i].month << "\t"
<< books[i].price << "\t"
<< books[i].quantity << endl;
return i;
}
}
cout << "No book found." << endl;
return -1;
}
```
最后,我们可以在主函数中使用 BookList 类进行操作:
```C++
int main() {
BookList booklist;
booklist.initialize("book.txt");
while (true) {
cout << "What do you want to do?" << endl;
cout << "1. Display all books" << endl;
cout << "2. Insert a new book" << endl;
cout << "3. Remove a book" << endl;
cout << "4. Modify a book" << endl;
cout << "5. Search for a book" << endl;
cout << "6. Exit" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
booklist.display_all();
break;
case 2:
Book book;
cout << "Please input the book information:" << endl;
cin >> book.name >> book.author >> book.publisher >> book.isbn
>> book.year >> book.month >> book.price >> book.quantity;
booklist.insert(book);
break;
case 3:
int index;
cout << "Please input the index of the book to remove:" << endl;
cin >> index;
booklist.remove(index);
break;
case 4:
int index;
Book book;
cout << "Please input the index of the book to modify:" << endl;
cin >> index;
cout << "Please input the new book information:" << endl;
cin >> book.name >> book.author >> book.publisher >> book.isbn
>> book.year >> book.month >> book.price >> book.quantity;
booklist.modify(index, book);
break;
case 5:
string keyword;
cout << "Please input the keyword to search:" << endl;
cin >> keyword;
booklist.search(keyword);
break;
case 6:
return 0;
default:
cout << "Invalid choice, please try again." << endl;
break;
}
}
return 0;
}
```
代码中的注释可能不够详细,但是这应该能提供一个参考。如果有任何问题,请随时问我。
阅读全文