21xrx.com
2024-06-03 06:21:38 Monday
登录
文章检索 我的文章 写文章
C++图书管理系统简单代码分享
2023-06-22 18:21:53 深夜i     --     --
C++ 图书管理系统 代码分享 简单实现 编程语言

C++是一种流行的面向对象编程语言,因其灵活性和广泛的应用而备受青睐。在编程学习的过程中,图书管理系统是一个非常好的练手项目。本文将与读者分享一份简单的基于C++的图书管理系统代码。

在本系统中,我们将使用面向对象的思想设计程序。系统主要分为三个类:Book类、Reader类和Library类。其中,Book类包含书籍的信息,Reader类包含读者的信息,Library类为整个系统的控制中心,负责管理书籍和读者信息的增删改查等各项操作。

首先,我们定义Book类。在这个类中,我们将书籍的名称、作者、出版社、ISBN号码、价格以及书籍库存信息定义为私有成员变量。同时,我们要定义相应的公有函数来获取和设置这些信息,这些操作称为访问器(accessor functions)和修改器(mutator functions),使得用户可以方便地对书籍信息进行操作。

接下来,我们定义Reader类。这个类非常类似于Book类,只是将书籍信息改为读者信息。在这个类中,我们将读者的姓名、年龄、性别、电话和住址等信息定义为私有成员变量,并同样定义了相应的访问器和修改器函数。通过这个类,我们可以很方便地管理读者信息。

最后,我们定义Library类。这个类作为系统的控制中心,在这个类中我们可以添加书籍、删除书籍、查看书籍详细信息、借书和还书等两项操作。为了实现这些操作,我们需要定义相应的函数。在这个类中,我们还可以进行读者信息管理的操作,例如添加读者、删除读者和查看读者信息等。

以上就是整个图书管理系统的类设计,下面我们来看一下程序具体实现部分。为了简化代码,我们使用了不同的固定数值来代替用户输入的信息。由于这是一个基础版的系统,仅支持最基本的操作。完整代码如下:


#include <iostream>

#include <string>

using namespace std;

class Book{

private:

  string name;

  string author;

  string publisher;

  string ISBN;

  float price;

  int stock;

public:

  Book(string name, string author, string publisher, string ISBN, float price, int stock)

    this->name = name;

    this->author = author;

    this->publisher = publisher;

    this->ISBN = ISBN;

    this->price = price;

    this->stock = stock;

  

  string getName() const

    return name;

  

  string getAuthor() const

    return author;

  

  string getPublisher() const

    return publisher;

  

  string getISBN() const

    return ISBN;

  

  float getPrice() const

    return price;

  

  int getStock() const

    return stock;

  

};

class Reader{

private:

  string name;

  int age;

  char sex;

  string phone;

  string address;

public:

  Reader(string name, int age, char sex, string phone, string address)

    this->name = name;

    this->age = age;

    this->sex = sex;

    this->phone = phone;

    this->address = address;

  

  string getName() const

    return name;

  

  int getAge() const

    return age;

  

  char getSex() const

    return sex;

  

  string getPhone() const

    return phone;

  

  string getAddress() const

    return address;

  

};

class Library{

public:

  Book books[100];

  Reader readers[100];

  int bookNumber;

  int readerNumber;

  Library()

    bookNumber = 0;

    readerNumber = 0;

  

  void addBook(Book book){

    books[bookNumber] = book;

    bookNumber++;

  }

  void removeBook(string ISBN){

    for (int i = 0; i < bookNumber; i++){

      if (books[i].getISBN() == ISBN){

        for (int j = i; j < bookNumber - 1; j++){

          books[j] = books[j + 1];

        }

        bookNumber--;

        break;

      }

    }

  }

  void viewBook(string ISBN) const{

    for (int i = 0; i < bookNumber; i++){

      if (books[i].getISBN() == ISBN){

        cout << "Name: " << books[i].getName() << endl;

        cout << "Author: " << books[i].getAuthor() << endl;

        cout << "Publisher: " << books[i].getPublisher() << endl;

        cout << "ISBN: " << books[i].getISBN() << endl;

        cout << "Price: " << books[i].getPrice() << endl;

        cout << "Stock: " << books[i].getStock() << endl;

        break;

      }

    }

    cout << "No such book in library!" << endl;

  }

  void borrowBook(string ISBN){

    for (int i = 0; i < bookNumber; i++){

      if (books[i].getISBN() == ISBN){

        if (books[i].getStock() > 0){

          books[i].stock--;

          cout << "Borrow successfully!" << endl;

          return;

        } else

          cout << "No stock now!" << endl;

          return;

        

      }

    }

    cout << "No such book in library!" << endl;

  }

  void returnBook(string ISBN){

    for (int i = 0; i < bookNumber; i++){

      if (books[i].getISBN() == ISBN){

        books[i].stock++;

        cout << "Return successfully!" << endl;

        return;

      }

    }

    cout << "No such book in library!" << endl;

  }

  void addReader(Reader reader){

    readers[readerNumber] = reader;

    readerNumber++;

  }

  void removeReader(string name){

    for (int i = 0; i < readerNumber; i++){

      if (readers[i].getName() == name){

        for (int j = i; j < readerNumber - 1; j++){

          readers[j] = readers[j + 1];

        }

        readerNumber--;

        break;

      }

    }

  }

  void viewReader(string name) const{

    for (int i = 0; i < readerNumber; i++){

      if (readers[i].getName() == name){

        cout << "Name: " << readers[i].getName() << endl;

        cout << "Age: " << readers[i].getAge() << endl;

        cout << "Sex: " << readers[i].getSex() << endl;

        cout << "Phone: " << readers[i].getPhone() << endl;

        cout << "Address: " << readers[i].getAddress() << endl;

        break;

      }

    }

    cout << "No such reader in library!" << endl;

  }

};

int main(){

  Library lib;

  Book b1("C++ Primer", "Lippman", "O'Reilly", "978-7-111-32735-7", 89.0, 100);

  Book b2("The Algorithm Design Manual", "S.Skiena", "Springer", "978-7-302-27293-5", 99.0, 50);

  Reader r1("Tom", 20, 'M', "1234567890", "Beijing");

  Reader r2("Lily", 19, 'F', "0987654321", "Shanghai");

  lib.addBook(b1);

  lib.addBook(b2);

  lib.addReader(r1);

  lib.addReader(r2);

  lib.viewBook("978-7-111-32735-7");

  lib.borrowBook("978-7-111-32735-7");

  lib.viewBook("978-7-111-32735-7");

  lib.returnBook("978-7-111-32735-7");

  lib.viewBook("978-7-111-32735-7");

  lib.viewReader("Tom");

  lib.removeBook("978-7-111-32735-7");

  lib.removeReader("Tom");

  return 0;

}

以上代码只是一个基础版的简易图书管理系统,可以根据需求进行功能增加或修改。通过学习这个例子,我们可以更好地理解面向对象编程的思想,同时也可以提高自己的编程技能。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复