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

这篇具有很好参考价值的文章主要介绍了用C++实现图书馆管理系统。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

这个程序的核心在于 `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模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 学校图书馆管理系统的架构设计与实现

    随着大学生越来越多,学校图书馆的管理变得愈发复杂。为了更好地管理和服务于学生和教职工,学校需要建立一个高效的图书馆管理系统。本文将介绍学校图书馆管理系统的架构设计与实现。 1. 架构设计 学校图书馆管理系统的架构设计主要分为三个部分: 展示层:展示层

    2024年02月22日
    浏览(39)
  • 【HTML】原生js实现的图书馆管理系统

    1、引言 设计结课作业,课程设计无处下手,网页要求的总数量太多?没有合适的模板?数据库,java,python,vue,html作业复杂工程量过大?毕设毫无头绪等等一系列问题。你想要解决的问题,在微信公众号“coding加油站”中全部会得到解决 2、作品介绍 原生js实现的图书馆管理

    2024年02月06日
    浏览(29)
  • java毕业设计——基于java+Java Swing+sqlserver的图书馆书库管理系统设计与实现(毕业论文+程序源码)——图书馆书库管理系统

    大家好,今天给大家介绍基于java+Java Swing+sqlserver的图书馆书库管理系统设计与实现,文章末尾附有本毕业设计的论文和源码下载地址哦。需要下载开题报告PPT模板及论文答辩PPT模板等的小伙伴,可以进入我的博客主页查看左侧最下面栏目中的自助下载方法哦 文章目录: 毕业

    2024年02月03日
    浏览(33)
  • 33基于Java简单实现图书馆借书管理系统设计与实现

    本章节给大家介绍一个基于Java简单实现图书馆借书管理系统的设计与实现 项目总体分为俩种角色,分别是管理员和阅读者,管理员可以登录系统中,进行图书管理,上架下架图书,对用户进行管理、对读者进行管理、查看借阅记录管理等,读者角色可以登录系统查询图书信息

    2024年02月03日
    浏览(39)
  • Java连接数据库实现图书馆管理系统(详细教程)

    该功能用到的软件为 IDEA 、Navicat 、云服务器(非必须) 源码下载 https://www.aliyundrive.com/s/UTz8pNxobGK 在自己的服务器或者电脑本机安装数据库系统,本次系统演示的数据库版本为5.6。 1.创建图书管理数据库library 字符集为:utf8 -utf8_general_ci 2.设计好将要使用的sql语句 视图、存储

    2024年02月06日
    浏览(37)
  • 【计算机毕业设计】图书馆管理系统设计与实现

            以往的图书馆管理事务处理主要使用的是传统的人工管理方式,这种管理方式存在着管理效率低、操作流程繁琐、保密性差等缺点,长期的人工管理模式会产生大量的文本借书与文本数据,这对事务的查询、更新以及维护带来不少困难。随着互联网时代的到来,现如

    2024年02月04日
    浏览(34)
  • 基于微信小程序的图书馆管理系统设计与实现

                                                                         摘要 在当今社会,互联网已经开始成为时代的主流,随着信息技术的发展,人们开始越来越依赖网络。互联网也逐渐成为我们生活中必不可少的一部分,它的出现也重新定义了人们获取信息资源的

    2024年02月12日
    浏览(37)
  • php图书馆管理系统的设计与实现毕业设计-附源码

    摘 要 大数据时代下,数据呈爆炸式地增长。为了迎合信息化时代的潮流和信息化安全的要求,利用互联网服务于其他行业,促进生产,已经是成为一种势不可挡的趋势。在图书馆的要求下,开发一款整体式结构的图书馆管理系统,将复杂的系统进行拆分,能够实现对需求的

    2024年02月08日
    浏览(35)
  • 【数据库课设】图书馆资源管理系统 源码+流程图+结构设计(借还图书 逾期罚款 图书管理 读者管理 信息查询)python实现

    一个管理员编号对应一个密码,且需要有管理员注册密匙。 可以在图书信息表中录入、修改、删除图书。 可以在图书信息表中查询书籍。 可以编辑图书借阅、归还信息。 可以编辑欠款信息。 可以编辑读者信息表。 图书馆注册,获得读者编号。 可以在图书信息表中查阅书籍

    2024年02月10日
    浏览(32)
  • 基于Java的图书馆借阅管理系统的设计与实现--毕业开题报告

    基于Java的图书馆借阅管理系统的设计与实现–开题报告 这个先写一版开题报告,后续有时间给大家提供论文 。 题目:基于Java的图书馆借阅管理系统的设计与实现 图书馆是一个信息系统,它收集、处理、组织、存储、选择、控制、转换重要的信息和知识载体,并将其传递给

    2024年02月03日
    浏览(101)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包