該程序包含一個 `Book` 類,代表圖書館中的書籍,具有標題、作者、出版商、出版年份和可用性屬性。程序中還定義了一個 `bookList` 向量,用于存儲圖書館中的所有書籍。
程序的 `main` 函數(shù)通過循環(huán)顯示菜單,然后根據(jù)用戶的選擇調(diào)用相應(yīng)的函數(shù)。用戶可以選擇添加新書籍、搜索現(xiàn)有書籍、顯示所有書籍、借書或還書。每個函數(shù)都有與之對應(yīng)的操作,如添加新書籍、打印書籍詳情等。文章來源:http://www.zghlxwxcb.cn/news/detail-518383.html
這個程序的核心在于 `Book` 類和 `bookList` 向量,這些都是通過C++的面向?qū)ο缶幊虒崿F(xiàn)的。此外,程序使用了標準輸入和輸出庫,以及字符串和向量 STL 類,簡化了輸入和輸出操作。文章來源地址http://www.zghlxwxcb.cn/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;
}
到了這里,關(guān)于用C++實現(xiàn)圖書館管理系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!