用C++实现图书馆管理系统

该程序使用C++的面向对象编程,定义了一个`Book`类来表示图书馆中的书籍,包括标题、作者、出版商、出版年份和可用性属性。程序维护了一个`bookList`向量来存储所有书籍。用户可以添加书籍、搜索、显示所有书籍、借书和还书。主要功能通过菜单驱动,与用户交互进行操作。
摘要由CSDN通过智能技术生成

该程序包含一个 `Book` 类,代表图书馆中的书籍,具有标题、作者、出版商、出版年份和可用性属性。程序中还定义了一个 `bookList` 向量,用于存储图书馆中的所有书籍。

程序的 `main` 函数通过循环显示菜单,然后根据用户的选择调用相应的函数。用户可以选择添加新书籍、搜索现有书籍、显示所有书籍、借书或还书。每个函数都有与之对应的操作,如添加新书籍、打印书籍详情等。

这个程序的核心在于 `Book` 类和 `bookList` 向量,这些都是通过C++的面向对象编程实现的。此外,程序使用了标准输入和输出库,以及字符串和向量 STL 类,简化了输入和输出操作。

#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;
}


  • 8
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
开题报告 经过大学四年理论课程的学习,以及校内校外的实践,极大的丰富了自身的理论基础,并且也具备了一些处理简单问题的能力,但即将踏上社会的我深知这还远远不够,因此我利用了毕业设计的机会,在老师的指导下去完成一个具有挑战性的,其具体应用及社会服务相结合的项目,深知通过对它的开发,将对我的能力有更高层次的突破。 开发软件的一个目的是针对目前普遍的图书管理系统存在的功能不全,操作复杂,系统要求高等一系列问题,而设想一个具有个性化的图书管理系统。该管理系统与MIS系统相联系,在图书馆内部建成可靠,方便,并且功能齐全的MIS系统。从而在图书馆对新旧书的反应;对书籍借阅的管理能力;对读者和图书馆工作人员的管理能力;对图书馆管理人员软件操作的适应时间和操作感觉这些方面都将大大的提高。当然对该软件的态度是渴望获得显著的社会效益。 开发软件的另一个目的是使其具有强大的实用价值,即它可以满足中小型图书馆的借阅与管理的需要。在一般的学校与科研机构,其下属的很多分支的研究中心,试验中心,各个学院,它们往往都有着自己的规模较大的图书资料室。而这些图书资料室由于本身规模不大因此其对书籍或资料的管理模式一般都比较旧,而且在没有能力也没有必要引进大型的图书馆管理软硬件的基础上,寻求一个针对中小型图书资料室的管理软件是必要的。 这种管理软件对硬件的要求很低,一般有一个比较简单的服务器与PC机组成的网络即可,再加上使用比较廉价、性能不错的软件,这样就可以以较低的成本来实现一个足够使用的功能,而这种模式也正满足了那种中小型资料室,图书馆的要求。但是目前对这种管理软件的开发还处于一种比较原始的阶段。开发者往往都是出于资金和时间的顾虑使用比较陈旧的技术,并且各为己见,并且很少涉及网络。可是当今是一个网络化的社会,像资料室,图书馆这样的信息机构不能与网络联系起来岂不可惜,再加上如今单机版的图书管理软件多如牛毛,去开发一个类似的软件无疑是一种在时间上、精神上和资源上的极大浪费。所以出于多方面的考虑觉得有必要为这种有需要的中小型图书馆,资料室开发一种基于网络的图书资料管理软件。 项目的具体目的: 1. 实现图书馆对外借书,还书的简易操作,提高图书馆对最平凡工作的效率。 2. 实现图书馆对所藏图书的按类别,书名等多方面的查询,最大的方便读者和图书馆工作人员对所需图书的查询。 3. 建立图书馆外借读者数据库,方便工作人员对读者进行有效管理。 4. 建立图书馆工作人员数据库,限定每个工作人员对软件操作的权限,最大限度的保护数据库。 5. 实现图书馆对新书入库,旧书注销的简单处理,并且建立书籍档案,方便进货。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT大鸵鸟

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值