c++实现控制台图书管理系统(使用fstream)(将其分为读者和管理员两大部分)(读者拥有借书,还书的功能)(管理员拥有增减书籍,增减读者的功能)
时间: 2023-12-27 20:04:50 浏览: 131
好的,这是一个比较典型的图书管理系统的实现,下面是一份基本的伪代码实现:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Book {
public:
string title;
string author;
string ISBN;
bool borrowed;
string borrower;
Book(string title, string author, string ISBN) {
this->title = title;
this->author = author;
this->ISBN = ISBN;
borrowed = false;
borrower = "";
}
};
class Reader {
public:
string name;
vector<Book*> borrowed_books;
Reader(string name) {
this->name = name;
}
bool borrow(Book* book) {
if (book->borrowed)
return false;
book->borrowed = true;
book->borrower = name;
borrowed_books.push_back(book);
return true;
}
bool return_book(Book* book) {
for (int i=0; i<borrowed_books.size(); ++i) {
if (borrowed_books[i] == book) {
book->borrowed = false;
book->borrower = "";
borrowed_books.erase(borrowed_books.begin() + i);
return true;
}
}
return false;
}
};
class Admin {
public:
vector<Book*> book_list;
vector<Reader*> reader_list;
void add_book(Book* book) {
book_list.push_back(book);
}
void remove_book(Book* book) {
for (int i=0; i<book_list.size(); ++i) {
if (book_list[i] == book) {
book_list.erase(book_list.begin() + i);
return;
}
}
}
void add_reader(Reader* reader) {
reader_list.push_back(reader);
}
void remove_reader(Reader* reader) {
for (int i=0; i<reader_list.size(); ++i) {
if (reader_list[i] == reader) {
reader_list.erase(reader_list.begin() + i);
return;
}
}
}
};
int main() {
Admin admin;
Reader* reader = new Reader("Alice");
Book* book1 = new Book("The Lord of the Rings", "J.R.R. Tolkien", "978-0544003415");
Book* book2 = new Book("Harry Potter and the Philosopher's Stone", "J.K. Rowling", "978-1781100219");
admin.add_reader(reader);
admin.add_book(book1);
admin.add_book(book2);
cout << "Welcome to the library management system!" << endl;
while (true) {
cout << "Please select your role: " << endl;
cout << "1. Reader" << endl;
cout << "2. Admin" << endl;
int role;
cin >> role;
if (role == 1) {
cout << "Welcome, " << reader->name << "!" << endl;
while (true) {
cout << "Please select your action: " << endl;
cout << "1. Borrow a book" << endl;
cout << "2. Return a book" << endl;
cout << "3. Exit" << endl;
int action;
cin >> action;
if (action == 1) {
cout << "Please input the book's ISBN: " << endl;
string ISBN;
cin >> ISBN;
bool found = false;
for (int i=0; i<admin.book_list.size(); ++i) {
if (admin.book_list[i]->ISBN == ISBN) {
if (reader->borrow(admin.book_list[i])) {
cout << "Borrow success!" << endl;
} else {
cout << "Borrow failed: the book is already borrowed!" << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found!" << endl;
}
} else if (action == 2) {
cout << "Please input the book's ISBN: " << endl;
string ISBN;
cin >> ISBN;
bool found = false;
for (int i=0; i<reader->borrowed_books.size(); ++i) {
if (reader->borrowed_books[i]->ISBN == ISBN) {
reader->return_book(reader->borrowed_books[i]);
cout << "Return success!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "Book not found in your borrowed list!" << endl;
}
} else if (action == 3) {
break;
}
}
} else if (role == 2) {
cout << "Welcome, Administrator!" << endl;
while (true) {
cout << "Please select your action: " << endl;
cout << "1. Add a book" << endl;
cout << "2. Remove a book" << endl;
cout << "3. Add a reader" << endl;
cout << "4. Remove a reader" << endl;
cout << "5. Exit" << endl;
int action;
cin >> action;
if (action == 1) {
cout << "Please input the book's title, author and ISBN: " << endl;
string title, author, ISBN;
cin >> title >> author >> ISBN;
Book* book = new Book(title, author, ISBN);
admin.add_book(book);
cout << "Add success!" << endl;
} else if (action == 2) {
cout << "Please input the book's ISBN: " << endl;
string ISBN;
cin >> ISBN;
bool found = false;
for (int i=0; i<admin.book_list.size(); ++i) {
if (admin.book_list[i]->ISBN == ISBN) {
admin.remove_book(admin.book_list[i]);
cout << "Remove success!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "Book not found!" << endl;
}
} else if (action == 3) {
cout << "Please input the reader's name: " << endl;
string name;
cin >> name;
Reader* reader = new Reader(name);
admin.add_reader(reader);
cout << "Add success!" << endl;
} else if (action == 4) {
cout << "Please input the reader's name: " << endl;
string name;
cin >> name;
bool found = false;
for (int i=0; i<admin.reader_list.size(); ++i) {
if (admin.reader_list[i]->name == name) {
admin.remove_reader(admin.reader_list[i]);
cout << "Remove success!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "Reader not found!" << endl;
}
} else if (action == 5) {
break;
}
}
} else {
cout << "Invalid role!" << endl;
}
}
return 0;
}
```
需要注意的几个问题:
1. 该实现中使用了面向对象的设计思想,通过定义 Book 类、Reader 类和 Admin 类来实现图书管理系统的各种功能。
2. 在 main 函数中,分别对读者和管理员进行了不同的处理,并利用 while 循环来保持程序的持续运行。
3. 在用户输入数据时,需要做好输入校验和异常处理,以防止程序出现崩溃的情况。
4. 在使用 fstream 进行文件读写操作时,还需要注意文件的打开和关闭操作。
阅读全文