图书管理系统:编写一个控制台应用程序,可以添加、删除和查找图书,并将它们存储在文件中带有注释
时间: 2024-02-12 13:04:39 浏览: 28
好的,下面是一个简单的C++图书管理系统:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// 定义Book类
class Book {
public:
Book(string title, string author, string isbn) {
this->title = title;
this->author = author;
this->isbn = isbn;
}
string getTitle() {
return title;
}
string getAuthor() {
return author;
}
string getISBN() {
return isbn;
}
private:
string title;
string author;
string isbn;
};
// 定义BookManager类
class BookManager {
public:
// 构造函数
BookManager() {
// 从文件中读取图书信息
ifstream fin("books.txt");
if (fin.is_open()) {
string title, author, isbn;
while (getline(fin, title) && getline(fin, author) && getline(fin, isbn)) {
books.push_back(Book(title, author, isbn));
}
fin.close();
}
}
// 添加图书
void addBook() {
string title, author, isbn;
cout << "请输入图书信息:" << endl;
cout << "书名:";
getline(cin, title);
cout << "作者:";
getline(cin, author);
cout << "ISBN:";
getline(cin, isbn);
books.push_back(Book(title, author, isbn));
cout << "添加成功!" << endl;
}
// 删除图书
void deleteBook() {
string title;
cout << "请输入要删除的图书名称:";
getline(cin, title);
for (vector<Book>::iterator it = books.begin(); it != books.end(); it++) {
if (it->getTitle() == title) {
books.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该图书!" << endl;
}
// 查找图书
void findBook() {
string title;
cout << "请输入要查找的图书名称:";
getline(cin, title);
for (vector<Book>::iterator it = books.begin(); it != books.end(); it++) {
if (it->getTitle() == title) {
cout << "书名:" << it->getTitle() << endl;
cout << "作者:" << it->getAuthor() << endl;
cout << "ISBN:" << it->getISBN() << endl;
return;
}
}
cout << "未找到该图书!" << endl;
}
// 显示所有图书
void showAllBooks() {
for (vector<Book>::iterator it = books.begin(); it != books.end(); it++) {
cout << "书名:" << it->getTitle() << endl;
cout << "作者:" << it->getAuthor() << endl;
cout << "ISBN:" << it->getISBN() << endl;
cout << endl;
}
}
// 保存图书信息到文件
void saveToFile() {
ofstream fout("books.txt");
if (fout.is_open()) {
for (vector<Book>::iterator it = books.begin(); it != books.end(); it++) {
fout << it->getTitle() << endl;
fout << it->getAuthor() << endl;
fout << it->getISBN() << endl;
}
fout.close();
}
}
private:
vector<Book> books;
};
int main() {
BookManager bookManager;
int choice;
while (true) {
cout << "请选择要进行的操作:" << endl;
cout << "1. 添加图书" << endl;
cout << "2. 删除图书" << endl;
cout << "3. 查找图书" << endl;
cout << "4. 显示所有图书" << endl;
cout << "5. 退出程序" << endl;
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
bookManager.addBook();
break;
case 2:
bookManager.deleteBook();
break;
case 3:
bookManager.findBook();
break;
case 4:
bookManager.showAllBooks();
break;
case 5:
bookManager.saveToFile();
cout << "程序已退出!" << endl;
return 0;
default:
cout << "请选择正确的操作!" << endl;
break;
}
}
}
```
上述代码中,Book类表示一本书,包含书名、作者和ISBN,BookManager类表示图书管理系统,包含添加图书、删除图书、查找图书、显示所有图书和保存图书信息到文件等功能。图书信息以文本文件的形式存储,每本书占三行,分别表示书名、作者和ISBN。
注意,在读取和写入文件时,需要使用ifstream和ofstream类。在添加、删除和查找图书时,需要使用getline函数读取用户输入的字符串。在添加图书时,需要使用cin.ignore()函数忽略掉之前输入操作留下的回车符。