该程序包含一个 `Book` 类,代表图书馆中的书籍,具有标题、作者、出版商、出版年份和可用性属性。程序中还定义了一个 `bookList` 向量,用于存储图书馆中的所有书籍。
程序的 `main` 函数通过循环显示菜单,然后根据用户的选择调用相应的函数。用户可以选择添加新书籍、搜索现有书籍、显示所有书籍、借书或还书。每个函数都有与之对应的操作,如添加新书籍、打印书籍详情等。文章来源:https://www.toymoban.com/news/detail-518383.html
这个程序的核心在于 `Book` 类和 `bookList` 向量,这些都是通过C++的面向对象编程实现的。此外,程序使用了标准输入和输出库,以及字符串和向量 STL 类,简化了输入和输出操作。文章来源地址https://www.toymoban.com/news/detail-518383.html
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Book {
string title;
string author;
string publisher;
int year;
bool available;
public:
Book(string title, string author, string publisher, int year) {
this->title = title;
this->author = author;
this->publisher = publisher;
this->year = year;
this->available = true;
}
string getTitle() {
return this->title;
}
string getAuthor() {
return this->author;
}
string getPublisher() {
return this->publisher;
}
int getYear() {
return this->year;
}
bool isAvailable() {
return this->available;
}
void setAvailable(bool available) {
this->available = available;
}
void printDetails() {
cout << "Title: " << this->title << endl;
cout << "Author: " << this->author << endl;
cout << "Publisher: " << this->publisher << endl;
cout << "Year: " << this->year << endl;
cout << "Available: " << (this->available ? "Yes" : "No") << endl;
}
};
vector<Book> bookList;
void displayMenu() {
cout << "===============================" << endl;
cout << "Library Management System Menu" << endl;
cout << "===============================" << endl;
cout << "1. Add Book" << endl;
cout << "2. Search Book" << endl;
cout << "3. Display All Books" << endl;
cout << "4. Borrow Book" << endl;
cout << "5. Return Book" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
}
void addBook() {
string title, author, publisher;
int year;
cout << "Enter title: ";
cin.ignore();
getline(cin, title);
cout << "Enter author: ";
getline(cin, author);
cout << "Enter publisher: ";
getline(cin, publisher);
cout << "Enter year: ";
cin >> year;
Book newBook(title, author, publisher, year);
bookList.push_back(newBook);
cout << "Book added successfully." << endl;
}
void searchBook() {
string title;
bool found = false;
cout << "Enter title to search: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
bookList[i].printDetails();
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
}
void displayBooks() {
if (bookList.empty()) {
cout << "No books in the library." << endl;
return;
}
for (int i = 0; i < bookList.size(); i++) {
bookList[i].printDetails();
cout << endl;
}
}
void borrowBook() {
string title;
bool found = false;
cout << "Enter title to borrow: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (bookList[i].isAvailable()) {
bookList[i].setAvailable(false);
cout << "Book borrowed successfully." << endl;
} else {
cout << "Book is not available for borrowing." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
}
void returnBook() {
string title;
bool found = false;
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
cout << "Enter title to return: ";
cin.ignore();
getline(cin, title);
for (int i = 0; i < bookList.size(); i++) {
if (bookList[i].getTitle() == title) {
if (!bookList[i].isAvailable()) {
bookList[i].setAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "This book is already available." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Book not found." << endl;
}
int main() {
int choice;
do {
displayMenu();
cin >> choice;
switch (choice) {
case 1:
addBook();
break;
case 2:
searchBook();
break;
case 3:
displayBooks();
break;
case 4:
borrowBook();
break;
case 5:
returnBook();
break;
case 6:
cout << "Exiting Library Management System..." << endl;
break;
default:
cout << "Invalid choice. Try again." << endl;
}
cout << endl;
} while (choice != 6);
return 0;
}
到了这里,关于用C++实现图书馆管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!