本文实例为大家分享了C++实现简易图书馆管理系统的具体代码,供大家参考,具体内容如下
思路
在本程序中共有四个类:
book类:此类有书的基本信息:书名,编号,作者,价格等,和基本的get()和set()方法。类图如下:
library类:此类中有一个存放的书的数组,并且可以对书进行,查询,借阅,归还,添加,等相关操作。类图如下:
reader_infor类:此类中有读者的相关信息:名字,学号,年级,借书数量。和基本的get()和set()方法,类图如下:
reader_admin类:此类中有一个存放读者对象的数组,并且可以进行添加读者,修改读者信息等操作。类图如下:
登陆页面如下
读者页面
管理员页面
在本程序过程中的不足:
1.书和读者的存储是通过静态数组实现的,如果用动态数组的话更好一点。
2.还有一些其他点感觉写的不是很好,但还不知道要怎么改。
后面有时间会再完善一下。
代码
“book.h"
#pragma once
#include <iostream>
using namespace std;
class book
{
public:
book();
book(string name, int n_number, int s_number, int id, string author, double price);
//get方法
string get_name();
int get_n_number();
int get_s_number();
int get_id();
string get_author();
double get_price();
//set方法
void set_name(string name);
void set_n_number(int n_number);
void set_s_number(int s_number);
void set_id(int id);
void set_author(string author);
void set_price(double price);
private:
string name;//名称
int n_number;//现有数量
int s_number;//库存数量
int id;//编号
string author;//作者
double price;//价格
};
"book.cpp"
#include "book.h"
book::book()
{
name = " ";
n_number = 0;
s_number = 0;
id = 0;
author = " ";
price = 0;
}
book::book(string name, int n_number, int s_number, int id, string author, double price)
{
this->name = name;
this->n_number = n_number;
this->s_number = s_number;
this->id = id;
this->author = author;
this->price = price;
}
//get方法。
string book::get_name()
{
return name;
}
int book::get_n_number()
{
return n_number;
}
int book::get_s_number()
{
return s_number;
}
int book::get_id()
{
return id;
}
string book::get_author()
{
return author;
}
double book::get_price()
{
return price;
}
//set方法。
void book::set_name(string name)
{
this->name = name;
}
void book::set_n_number(int n_number)
{
this->n_number = n_number;
}
void book::set_s_number(int s_number)
{
this->s_number = s_number;
}
void book::set_id(int id)
{
this->id = id;
}
void book::set_author(string author)
{
this->author = author;
}
void book::set_price(double price)
{
this->price = price;
}
"library.h"
#pragma once
#include"reader_admin.h"
#include "book.h"
using namespace std;
class library
{
public:
library();
int get_m();//返回存放书的总数量
int get_place();//返回查找到书的位置
void set_book1();//存放书籍
void show_book();//显示书籍的相关信息
bool search();//查找书籍
//修改书的相关信息
bool chang_book1();
bool borrow_book(reader_admin&r_ad);//借书
bool return_book(reader_admin& r_ad);//还书
private:
int m;//存放书的总数。
int place;//记录查找到书的位置。
book book1[1000];//一个存放1000本书的数组。
};
librayry.cpp
#include "library.h"
#include "book.h"
//book类作为书籍的基类
//书共有的属性:名称,现有数量,库存数量,编号,作者,价格
library::library()
{
m = 0;
}
int library::get_m()
{
return m;
}
int library::get_place()
{
return place;//返回查找到书的位置。
}
void library::set_book1()
{
cout << "请输入书的名称:" << endl;
string name;
cin >> name;
cout << "请输入书的现存数量:" << endl;
int now;
cin >> now;
cout << "请输入书的库存数量:" << endl;
int s;
cin >> s;
cout << "请输入书的编号:" << endl;
int id;
cin >> id;
cout << "请输入书的作者:" << endl;
string author;
cin >> author;
cout << "请输入书的价格:" << endl;
double price;
cin >> price;
book b(name, now, s, id, author, price);
this->book1[m] = b;
m++;
cout << "恭喜您完成书的存储!" << endl;
}
void library::show_book()
{
cout << "您好,下面将为您显示图书信息!" << endl;
cout << "名称" << " " << "编号" << " " << "现有数量" << " " << "库存数量" << " " << "作者" << " " << "价格" << endl;
for (int i = 0; i < m; i++)
{
cout << book1[i].get_name() << " " << book1[i].get_id() << " " << book1[i].get_n_number() << " " << book1[i].get_s_number() << " " << book1[i].get_author() << " " << book1[i].get_price() << endl;
}
}
bool library::search()
{
int id;
cout << "请输入您借阅书籍的ID:" << endl;
cin >> id;
for (int i = 0; i < m; i++)
{
if (book1[i].get_id() == id)
{
cout << "名称" << " " << "编号" << " " << "现有数量" << " " << "库存数量" << " " << "作者" << " " << "价格" << endl;
cout << book1[i].get_name() << " " << book1[i].get_id() << " " << book1[i].get_n_number() << " " << book1[i].get_s_number() << " " << book1[i].get_author() << " " << book1[i].get_price() << endl;
return true;
}
}
cout << "您查找的书不存在!" << endl;
return false;
}
bool library::borrow_book(reader_admin&r_ad)//借书
{
char c = ' ';
do
{
cout << "请输入您的学号:" << endl;
string r_id;
cin >> r_id;
if (r_ad.sreach_id(r_id) == true)//判断输入的学号是否存在
{
char d = ' ';
do {
int id;
cout << "请输入您借阅书籍的ID:" << endl;
cin >> id;
for (int i = 0; i < m; i++)
{
if (book1[i].get_id() == id)//如果查找的书本存在
{
if (book1[i].get_n_number() == 0)
{
cout << "您找的书已借阅完!" << endl;
return false;
}
else
{
book1[i].set_n_number(book1[i].get_n_number() - 1);//使书本的数量少一。
r_ad.r_borrow();//读者的借阅量加一。
cout << "恭喜您借阅成功!" << endl;
return true;
}
}
}
cout << "您查找的书籍不存在!/已借阅完!" << endl;
cout << "重新输入请按Y!按其他任意键退出!" << endl;
cin >> d;
} while (d == 'Y' || d == 'y');
return false;
}
cout << "您输入的学号不存在!" << endl;
cout << "重新输入请按Y!按其他任意键退出!" << endl;
cin >> c;
} while (c == 'Y' || c == 'y');
return false;
}
bool library::return_book(reader_admin& r_ad)
{
char c = ' ';
do
{
cout << "请输入您的学号:" << endl;
string r_id;
cin >> r_id;
if (r_ad.sreach_id(r_id) == true)//判断输入的学号是否存在
{
char d = ' ';
do {
int id;
cout << "请输入您借阅书籍的ID:" << endl;
cin >> id;
for (int i = 0; i < m; i++)
{
if (book1[i].get_id() == id)//如果查找的书本存在
{
if (book1[i].get_n_number() == 0)
{
}
else
{
book1[i].set_n_number(book1[i].get_n_number() +1);//使书本的数量加一。
r_ad.r_reutrn();//读者的借阅量减一。
cout << "恭喜您借阅成功!" << endl;
return true;
}
}
}
cout << "您查找的书籍不存在!/已借阅完!" << endl;
cout << "重新输入请按Y!按其他任意键退出!" << endl;
cin >> d;
} while (d == 'Y' || d == 'y');
return false;
}
cout << "您输入的学号不存在!" << endl;
cout << "重新输入请按Y!按其他任意键退出!" << endl;
cin >> c;
} while (c == 'Y' || c == 'y');
return false;
}
//修改书的相关信息
bool library::chang_book1()
{
char c = ' ';
char d = ' ';
do
{
cout << "请输入您要修改信息书的id:" << endl;
int id;
cin >> id;
for (int i = 0; i < m; i++)
{
if (book1[i].get_id() == id)
{
do {
cout << "请选择您要修改的信息:" << endl;
cout << "1.name" << "2.id" << "3.n_number" << "4.s_number" << "5.id" << "6.author" << "7.price" << endl;
int n;
cin >> n;
//修改名称
if (n == 1)
{
cout << "请输入您修改的名称:" << endl;
string new_name;
cin >> new_name;
book1[i].set_name(new_name);
cout << "恭喜您修改成功!" << endl;
}
//修改id
if (n == 2)
{
cout << "请输入您修改的id" << endl;
int new_id;
cin >> new_id;
book1[i].set_id(new_id);
cout << "恭喜您修改成功!" << endl;
}
//修改n_number
if (n == 3)
{
cout << "请输入您修改的现有数量" << endl;
int new_n_number;
cin >> new_n_number;
book1[i].set_n_number(new_n_number);
cout << "恭喜您修改成功!" << endl;
}
//修改库存信息
if (n == 4)
{
cout << "请输入您修改的库存数量" << endl;
int new_s_number;
cin >> new_s_number;
book1[i].set_s_number(new_s_number);
cout << "恭喜您修改成功!" << endl;
}
//修改书的编号
if (n == 5)
{
cout << "请输入您修改的编号" << endl;
int new_id;
cin >> new_id;
book1[i].set_id(new_id);
cout << "恭喜您修改成功!" << endl;
}
//修改作者信息
if (n == 6)
{
cout << "请输入您修改的作者" << endl;
string new_name;
cin >> new_name;
book1[i].set_name(new_name);
cout << "恭喜您修改成功!" << endl;
}
//修改价格信息
if (n == 7)
{
cout << "请输入您修改的价格" << endl;
double new_price;
cin >> new_price;
book1[i].set_price(new_price);
cout << "恭喜您修改成功!" << endl;
}
cout << "继续修改请按Y!" << endl;
cin >> d;
} while (d == 'Y' || d == 'y');
}
}
cout << "您查找的书不存在" << endl;
cout << "重新输入请按Y!按其他任意键退出!" << endl;
cin >> c;
} while (c == 'Y' || c == 'y');
return false;
}
"reader_infor.h"
#pragma once
//存储学生信息
#include"book.h"
class reader_infor
{
public:
reader_infor();
reader_infor(string Id, string Name, string Grade, int Number);
//get函数
string get_r_id();//返回学生的学号。
string get_r_name();//返回学生的名字。
string get_r_grade();//返回学生的班级。
int get_r_borrow();//返回学生借阅书籍的数量。
//set函数
void set_r_id(string ID);
void set_r_name(string Name);
void set_r_grade(string Grade);
void set_r_borrow(int Number);
private:
string r_id;//学生的学号
string r_name;//学生的名字
string r_grade;//学生的班级
int r_borrow;//学生借阅书籍的数量
};
"reader_infor.cpp"
#include"reader_infor.h"
//构造函数
reader_infor::reader_infor()
{
r_id = "";
r_name = "";
r_grade = "";
r_borrow = 0;
}
reader_infor::reader_infor(string id, string name, string grade, int number)
{
this->r_id = id;
this->r_name = name;
this->r_grade = grade;
this->r_borrow = number;
}
//get函数
string reader_infor::get_r_id()
{
return this->r_id;
}
string reader_infor::get_r_name()
{
return this->r_name;
}
string reader_infor::get_r_grade()
{
return this->r_grade;
}
int reader_infor::get_r_borrow()
{
return this->r_borrow;
}
//set函数
void reader_infor::set_r_id(string Id)
{
this->r_id = Id;
}
void reader_infor::set_r_name(string Name)
{
this->r_name = Name;
}
void reader_infor::set_r_grade(string Grade)
{
this->r_grade = Grade;
}
void reader_infor::set_r_borrow(int Borrow)
{
this->r_borrow = Borrow;
}
"reader_admin.h
#pragma once
#include"reader_infor.h"
//管理学生信息
class reader_admin
{
public:
reader_admin();
int get_borrow_number();
void set_borrow_number(int i);
void set_reader_infor();//创建学生信息。
void show_reader_infor();//显示学生的信息。
bool chang_reader();//修改读者信息。
bool sreach_id(string id);//查找学生的学号。
void r_borrow();//读者的借阅量+1。
void r_reutrn();//读者的借阅量-1。
private:
int borrow_number;//查找的相关位置。
int reader_number;//记录学生的个数。
reader_infor reader1[1000];//存放学生信息。
};
"reader_admin.cpp
#include"reader_admin.h"
reader_admin::reader_admin()
{
reader_number = 0;
borrow_number = -1;
}
int reader_admin::get_borrow_number()
{
return borrow_number;
}
void reader_admin::set_borrow_number(int i)
{
this->borrow_number = i;
}
//添加读者信息
void reader_admin::set_reader_infor()
{
cout << "请输入学生的id:" << endl;
string r_id;
cin >> r_id;
cout << "请输入学生的姓名:" << endl;
string r_name;
cin >> r_name;
cout << "请输入学生的年级:" << endl;
string r_grade;
cin >> r_grade;
cout << "请输入学生的借书数量:" << endl;
int r_number;
cin >> r_number;
reader_infor reader_new(r_id, r_name, r_grade, r_number);
reader1[reader_number] = reader_new;
reader_number++;//学生数量加1.
}
//显示读者信息
void reader_admin::show_reader_infor()
{
for (int i = 0; i < reader_number; i++)
{
cout << "学生的学号是:" << reader1[i].get_r_id() << endl;
cout << "学生的姓名是:" << reader1[i].get_r_name() << endl;
cout << "学生的年级是:" << reader1[i].get_r_grade() << endl;
cout << "学生的借书数量是:" << reader1[i].get_r_borrow() << endl;
}
}
bool reader_admin::sreach_id(string id)//查找学号是否存在
{
for (int i = 0; i < reader_number; i++)
{
if (reader1[i].get_r_id() == id)
{
borrow_number = i;
return true;
}
}
return false;
}
//借书数量加一
void reader_admin::r_borrow()
{
reader1[borrow_number].set_r_borrow(reader1[borrow_number].get_r_borrow() + 1);
borrow_number = -1;
}
//借书数量减一
void reader_admin::r_reutrn()
{
reader1[borrow_number].set_r_borrow(reader1[borrow_number].get_r_borrow() - 1);
borrow_number = -1;
}
//修改书籍信息
bool reader_admin::chang_reader()
{
char c = ' ';
char d = ' ';
do {
cout << "请输入您要修改信息学生的id" << endl;
string r_id;
cin >> r_id;
if (sreach_id(r_id) == true)
{
do {
cout << "请选择您要修改的信息:" << endl;
cout << "1.学号" << "2.名字" << "3.班级" << "4.借阅书籍数量" << endl;
int i;
cin >> i;
//修改学号
if (i == 1)
{
cout << "请输入您修改的学号" << endl;
string new_r_id;
cin >> new_r_id;
reader1[borrow_number].set_r_id(new_r_id);
cout << "恭喜您修改成功!" << endl;
}
//修改名称
if (i == 2)
{
cout << "请输入您修改的名称" << endl;
string new_r_name;
cin >> new_r_name;
reader1[borrow_number].set_r_name(new_r_name);
cout << "恭喜您修改成功!" << endl;
}
//修改班级
if (i == 3)
{
cout << "请输入您修改的班级" << endl;
string new_r_grade;
cin >> new_r_grade;
reader1[borrow_number].set_r_grade(new_r_grade);
cout << "恭喜您修改成功!" << endl;
}
//修改借阅数量
if (i == 4)
{
cout << "请输入您修改的数量" << endl;
int new_r_number;
cin >> new_r_number;
reader1[borrow_number].set_r_borrow(new_r_number);
cout << "恭喜您修改成功!" << endl;
}
cout << "继续修改信息请按Y!按任意键退出!" << endl;
cin >> d;
} while(d == 'Y' || d == 'y');
borrow_number = -1;//将查找到的位置初始化。
break;//跳出本次修改。
}
cout << "继续修改请按Y!按任意键退出!" << endl;
cin >> c;
} while (c == 'Y' || c == 'y');
return false;
}
test.cpp
#include <string>
#include "book.h"
#include "library.h"
#include"reader_infor.h"
#include"reader_admin.h"
using namespace std;
int main()
{
reader_admin r_ad;
library lib;
int i;
while (1)
{
cout << "************欢迎来到图书馆************" << endl;
cout << "**********请选择您的登陆身份**********" << endl;
cout << " 1. 借阅者登陆 " << endl;
cout << " 2. 管理员登陆 " << endl;
cin >> i;
int j;
if (i == 1)
{
do
{
cout << "********请选择您要进行的操作**********" << endl;
cout << "********1.浏览图书信息**********" << endl;
cout << "********2. 查找 **********" << endl;
cout << "********3. 借书 **********" << endl;
cout << "********4. 还书 **********" << endl;
cout << "********5. 退出 **********" << endl;
cin >> j;
switch (j)
{
case 1:lib.show_book(); break;
case 2:lib.search(); break;
case 3:lib.borrow_book(r_ad); break;
case 4:lib.return_book(r_ad); break;
case 5:break;
}
} while (j == 1 || j == 2 || j == 3 || j == 4);
}
int m;
if (i == 2)
{
do
{
cout << "********请选择您要进行的操作**********" << endl;
cout << "********1. 浏览图书信息 **********" << endl;
cout << "********2. 浏览读者信息 **********" << endl;
cout << "********3. 添加图书信息 **********" << endl;
cout << "********4. 添加读者信息 **********" << endl;
cout << "********5. 修改图书信息 **********" << endl;
cout << "********6. 修改读者信息 **********" << endl;
cout << "********7. 退出 **********" << endl;
cin >> m;
switch (m)
{
case 1:lib.show_book(); break;
case 2:r_ad.show_reader_infor(); break;
case 3:lib.set_book1(); break;
case 4:r_ad.set_reader_infor(); break;
case 5:lib.chang_book1(); break;
case 6:r_ad.chang_reader(); break;
case 7:continue;
}
} while (m==1||m==2||m==3||m==4||m==5||m==6);
}
}
return 0;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。