图书管理系统

效果图:



内容

Book 头文件(抽象类)

Book.h

#pragma once
#include<iostream>
using namespace std;
#include<cstring>class Book
{public:virtual void showInfo() = 0;virtual string GetDeptName() = 0;int m_ID;string m_name;int m_DeptId;double m_price;int m_num;
};

BookManager 头文件(管理系统功能)

#pragma once
#include<iostream>
using namespace std;
#include"Book.h"
#include"ScBook.h"
#include"Material.h"
#include"Novel.h"
#include<fstream>class BookManager
{public:BookManager();void Show_Menu();void exitSystem();int m_BookNum;Book** m_BookArry;bool m_FileIsEmpty;void Add();void BorrowOrReturn();void Save();int get_BookNum();void init_Book();void Show_Book();void Del_Book();int IsExist(string name);void Mod_Book();void Find_Book();void Sort_Book();void Clam_File();~BookManager();
};

BookManager.cpp

#include"BookManager.h"BookManager::BookManager()
{//初始化属性ifstream ifs;ifs.open("系统.txt", ios::in);if (!ifs.is_open()) //文件不存在{cout << "文件不存在 \n";this->m_BookNum = 0;this->m_FileIsEmpty = true;this->m_BookArry = NULL;ifs.close();return;}//文件存在char ch;ifs >> ch;if (ifs.eof())//判断文件是否为空(返回真){cout << "文件为空! \n";this->m_BookNum = 0;this->m_FileIsEmpty = true;this->m_BookArry = NULL;ifs.close();return;}//当文件存在且文件有内容int num = this->get_BookNum();cout << "图书人数为:" << num << endl;this->m_BookNum = num;//根据图书数创建数组//开辟空间this->m_BookArry = new Book * [this->m_BookNum];//文件数据放入数组中this->init_Book();}void BookManager::Add()
{cout << "请输入添加图书的数量: \n";int addNum = 0;cin >> addNum;if (addNum > 0){//添加int newSize = this->m_BookNum + addNum;//开辟新空间Book** newSpace = new Book * [newSize];if (this->m_BookArry != NULL){for (int i = 0; i < this->m_BookNum; ++i){newSpace[i] = this->m_BookArry[i];}}//添加新数据for (int i = 0; i < addNum; ++i){int id;string name;int dSelect;double price;int num;cout << "请输入第 " << i + 1 << " 个新图书的编号: \n";cin >> id;cout << "请输入第 " << i + 1 << " 个新图书名: \n";cin >> name;cout << "请选择该图书的类别:\n" << "1、教材 \n"<< "2、小说 \n"<< "3、科普 \n";cin >> dSelect;cout << "请输入第 " << i + 1 << " 个新图书的价格: \n";cin >> price;cout << "请输入第 " << i + 1 << " 个新图书的库存: \n";cin >> num;Book* Book = NULL;switch (dSelect){case 1:Book = new Material(id, name, 1, price, num);break;case 2:Book = new Novel(id, name, 2, price, num);break;case 3:Book = new ScBook(id, name, 3, price, num);break;default:break;}//创建的指针保存在数组中newSpace[this->m_BookNum + i] = Book;}//释放原有的空间delete[]this->m_BookArry;//改变空间指向this->m_BookArry = newSpace;//更改图书不为空标志this->m_FileIsEmpty = false;//改变指针长度this->m_BookNum = newSize;//添加到文件中cout << "成功添加 " << addNum << " 名新图书 \n";this->Save();}else{cout << "输入数据有误! \n";}system("pause");system("cls");
}void BookManager::BorrowOrReturn()
{if (this->m_FileIsEmpty){cout << "文件不存在或记录为空! \n";system("pause");system("cls");}else{cout << "1.借书 \n";cout << "2.还书 \n";int select = 0;cin >> select;cout << "请输入书名: \n";string name;cin >> name;int ret = this->IsExist(name);if (ret != -1){this->m_BookArry[ret]->showInfo();if (select == 1){if (this->m_BookArry[ret]->m_num != 0){this->m_BookArry[ret]->m_num--;cout << "借书成功! \n";}elsecout << "该书已被借完! \n";}else if (select == 2){this->m_BookArry[ret]->m_num++;cout << "还书成功! \n";}elsecout << "错误操作! \n";this->m_BookArry[ret]->showInfo();}elsecout << "该书不属于图书馆! \n";}this->Save();system("pause");system("cls");
}void BookManager::Show_Menu()
{cout << "—————————————————— \n"<< "————欢迎使用图书管理系统———— \n"<< "————— 0.退出管理系统 ————— \n"<< "————— 1.增加图书信息 ————— \n"<< "————— 2. 借书/还书   ————— \n"<< "————— 3.显示图书信息 ————— \n"<< "————— 4.删除离库图书 ————— \n"<< "————— 5.修改图书信息 ————— \n"<< "————— 6.查找图书信息 ————— \n"<< "————— 7.按照编号排序 ————— \n"<< "————— 8.清空所有文档 ————— \n";cout << endl;
}void BookManager::exitSystem()
{cout << "欢迎下次使用 \n";system("pause");exit(0);//不管是在哪个部分调用这个函数 退出程序 0 - 退出参数
}
BookManager::~BookManager()
{if (this->m_BookArry != NULL){delete[]this->m_BookArry;this->m_BookArry = NULL;}
}//保存文件
void BookManager::Save()
{ofstream ofs;ofs.open("系统.txt", ios::out);for (int i = 0; i < this->m_BookNum; ++i){ofs << this->m_BookArry[i]->m_ID << " "<< this->m_BookArry[i]->m_name << " "<< this->m_BookArry[i]->m_DeptId << " "<< this->m_BookArry[i]->m_price << " "<< this->m_BookArry[i]->m_num << endl;}ofs.close();
}
//统计人数
int BookManager::get_BookNum()
{ifstream ifs;ifs.open("系统.txt", ios::in);int id;string name;int did;double price;int number;int num = 0;while (ifs >> id && ifs >> name && ifs >> did && ifs >> price && ifs >> number){num++;}ifs.close();return num;
}void BookManager::init_Book()
{ifstream ifs;ifs.open("系统.txt", ios::in);int id;string name;int did;double price;int number;int index = 0;while (ifs >> id && ifs >> name && ifs >> did && ifs >> price && ifs >> number){Book* Book = NULL;if (did == 1)Book = new Material(id, name, did, price, number);else if (did == 2)Book = new Novel(id, name, did, price, number);elseBook = new ScBook(id, name, did, price, number);this->m_BookArry[index] = Book;index++;}ifs.close();
}void BookManager::Show_Book()
{if (this->m_FileIsEmpty)cout << "文件不存在或已为空! \n";else{for (int i = 0; i < m_BookNum; ++i){this->m_BookArry[i]->showInfo();}}system("pause");system("cls");
}int BookManager::IsExist(string name)
{int index = -1;for (int i = 0; i < m_BookNum; ++i){if (this->m_BookArry[i]->m_name.find(name) != string::npos){index = i;break;}}return index;
}void BookManager::Del_Book() //数组数据前移(找到位置的后一位)
{if (this->m_FileIsEmpty)cout << "文件不存在或文件为空! \n";else{//按照图书的姓名删除cout << "请输入想要删除的图书名字: \n";string name = "0";cin >> name;int index = this->IsExist(name);if (index != -1){//m_BookNum = 1时不进入循坏for (int i = 0; i < this->m_BookNum - 1; ++i){//数据前移this->m_BookArry[i] = this->m_BookArry[i + 1];}//数量更新this->m_BookNum--;//数据同步到文件中this->Save();cout << "删除成功! \n";}elsecout << "删除失败,未找到该图书! \n";}system("pause");system("cls");
}void BookManager::Mod_Book()
{if (this->m_FileIsEmpty)cout << "文件不存在或记录为空! \n";else{cout << "请输入修改图书的名字: \n";string name = "0";cin >> name;int ret = this->IsExist(name);if (ret != -1){//查找delete this->m_BookArry[ret];int newId = 0;string newName = "";int dSelect = 0;double price;int number;this->m_BookArry[ret]->showInfo();cout << "请输入新图书的编号: \n";cin >> newId;cout << "请输入新图书名: \n";cin >> newName;cout << "请选择该图书的类别: \n"<< "1、教材 \n"<< "2、小说 \n"<< "3、科普 \n";cin >> dSelect;cout << "请输入新图书的价格: \n";cin >> price;cout << "请输入新图书的库存: \n";cin >> number;Book* Book = NULL;switch (dSelect){case 1:Book = new Material(newId, newName, dSelect, price, number);case 2:Book = new Novel(newId, newName, dSelect, price, number);case 3:Book = new ScBook(newId, newName, dSelect, price, number);default:break;}//更改数据到数组中this->m_BookArry[ret] = Book;cout << "修改成功! \n";//保存到文件中this->Save();}else{cout << "修改失败,查无此人 \n";}}system("pause");system("cls");
}void BookManager::Find_Book()
{if (this->m_FileIsEmpty)cout << "文件不存在或记录为空! \n";else{cout << "请输入查找的方式: \n";cout << "1.按图书编号查找 \n";cout << "2.按图书姓名查找 \n";int select = 0;cin >> select;if (select == 2){string name;cout << "请输入查找的图书姓名: \n";cin >> name;int ret = 1;ret = this->IsExist(name);if (ret != -1){cout << "查找成功!该图书信息如下: \n";this->m_BookArry[ret]->showInfo();}elsecout << "查找失败,查无此书 \n";}else if (select == 1){int id = 0;cout << "请输入查找的图书编号: \n";cin >> id;bool flag = false;for (int i = 0; i < this->m_BookNum; ++i){if (this->m_BookArry[i]->m_ID == id){cout << "查找成功,图书编号为: "<< this->m_BookArry[i]->m_ID<< "号图书信息如下: \n";flag = true;this->m_BookArry[i]->showInfo();}}if (flag == false){cout << "查找失败,查无此人! \n";}}elsecout << "输入选项有误! \n";}system("pause");system("cls");
}void BookManager::Sort_Book()
{if (this->m_FileIsEmpty){cout << "文件不存在或记录为空! \n";system("pause");system("cls");}else{cout << "请选择排序方式: \n";cout << "1.按图书编号进行升序 \n";cout << "2.按图书编号进行降序 \n";int select = 0;cin >> select;for (int i = 0; i < m_BookNum; ++i){int minOrMax = i;for (int j = i + 1; j < m_BookNum; ++j){if (select == 1)//升序{if (m_BookArry[minOrMax]->m_ID > m_BookArry[j]->m_ID)minOrMax = j;}else  //降序{if (m_BookArry[minOrMax]->m_ID < m_BookArry[j]->m_ID)minOrMax = j;}}if (i != minOrMax){Book* temp = m_BookArry[i];m_BookArry[i] = m_BookArry[minOrMax];m_BookArry[minOrMax] = temp;}}cout << "排序成功!排序后结果为: \n";this->Save();this->Show_Book();}
}void BookManager::Clam_File()
{cout << "确认清空? \n";cout << "1. 确认 \n";cout << "2. 返回 \n";int select = 0;cin >> select;if (select == 1){//trunc 如果存在文件 则删除并重建ofstream ofs("系统.txt", ios::trunc);ofs.close();//清空内容if (this->m_BookArry != NULL){for (int i = 0; i < this->m_BookNum; ++i){if (this->m_BookArry[i] != NULL)delete this->m_BookArry[i];}this->m_BookNum = 0;delete[]this->m_BookArry;this->m_BookArry = NULL;this->m_FileIsEmpty = true;}cout << "清空成功! \n";}system("pause");system("cls");
}

mian函数

#include<iostream>
using namespace std;
#include"BookManager.h"
#include"Book.h"
#include"Novel.h"//提供功能接口
int main()
{BookManager book;int choice = 0;while (true){book.Show_Menu();cout << "请输入您的选择:" << endl;cin >> choice;switch (choice){case 0: //退出系统book.exitSystem();break;case 1: //添加图书book.Add();break;case 2:book.BorrowOrReturn();break;case 3: //显示图书book.Show_Book();break;case 4: //删除图书//测试 要封装//{int ret = wm.IsExist("1");//if (ret != -1)//  cout << "图书存在" << endl;//else//   cout << "图书不存在" << endl;//}book.Del_Book();break;case 5: //修改图书book.Mod_Book();break;case 6: //查找图书book.Find_Book();break;case 7://排序book.Sort_Book();break;case 8: //清空文件book.Clam_File();break;default:system("cls"); // !!!(输入其他的)刷新屏幕break;}}return 0;
}

图书分类

1、教材

Material.h

#pragma once
#include<iostream>
using namespace std;
#include<cstring>
#include"Book.h"class Material : public Book
{public:Material(int id, string name, int did ,double price,int num);void showInfo();string GetDeptName();
};

Material.cpp

#include"Material.h"Material::Material(int id, string name, int did, double price, int num)
{this->m_ID = id;this->m_name = name;this->m_DeptId = did;this->m_price = price;this->m_num = num;
}//显示个人信息
void Material::showInfo()
{cout << "图书编号: " << this->m_ID<< "\t 图书名:" << this->m_name<< "\t 图书分类: " << this->GetDeptName()<< "\t 图书价格:" <<this->m_price<<"\t 图书库存:"<< this->m_num << endl;
}//获取类别名称
string Material::GetDeptName()
{return string("教材");
}

2、小说

Novel.h

#pragma once
#include<iostream>
using namespace std;
#include<cstring>
#include"Book.h"class Novel : public Book
{public:Novel(int id, string name, int did, double price, int num);void showInfo();string GetDeptName();
};

Novel.cpp

#include"Novel.h"Novel::Novel(int id, string name, int did, double price, int num)
{this->m_ID = id;this->m_name = name;this->m_DeptId = did;this->m_price = price;this->m_num = num;
}//显示个人信息
void Novel::showInfo()
{cout << "图书编号: " << this->m_ID<< "\t 图书名:" << this->m_name<< "\t 图书分类: " << this->GetDeptName()<< "\t 图书价格:" << this->m_price<< "\t 图书库存:" << this->m_num << endl;
}//获取类别名称
string Novel::GetDeptName()
{return string("小说");
}

3、科普

ScBook.h

#pragma once
#include<iostream>
using namespace std;
#include<cstring>
#include"Book.h"class ScBook : public Book
{public:ScBook(int id, string name, int did, double price, int num);void showInfo();string GetDeptName();};

ScBook.cpp

#include"ScBook.h"ScBook::ScBook(int id, string name, int did, double price, int num)
{this->m_ID = id;this->m_name = name;this->m_DeptId = did;this->m_price = price;this->m_num = num;
}//显示个人信息
void ScBook::showInfo()
{cout << "图书编号: " << this->m_ID<< "\t 图书名:" << this->m_name<< "\t 图书分类: " << this->GetDeptName()<< "\t 图书价格:" << this->m_price<< "\t 图书库存:" << this->m_num << endl;
}//获取类别名称
string ScBook::GetDeptName()
{return string("科普");
}

注意

系统分头文件和cpp文件会使得整个系统可读性更高。

C++:图书管理系统相关推荐

  1. Java项目:图书管理系统(java+SSM+jsp+mysql+maven)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 功能包括(管理员和学生角色): 管理员和学生登录,图书管理,图书添加删除修改,图书 借阅,图书归还,图书查看,学生管理,借还管 ...

  2. 图书管理系统5W1H

    Who 图书管理员 When 图书管理员在图书馆借阅期间管理用户的借书还书,非借阅时间管理后台图书.管理用户信息 Where 借书台.办公室 What 一个图书管理系统,能实现图书的借书还书操作.管理 ...

  3. 【Java】阶段性总结练习------图书管理系统实现

    在学习了面向对象编程语法知识以及简单数据结构–顺序表等等知识点以后,现在我们就可以运用这些知识来综合实现一个 图书管理系统 ,来检验前面知识的掌握程度以及练习代码的实际编写能力,让自己对Java开发更 ...

  4. 图书管理系统前景与范围文档

    一.业务需求 1.应用背景 目前,学院的图书管理仍是传统的人工管理方式,该方式效率低.保密性差, 一方面教师.学生对学院图书室藏书情况不了解,图书室究竟有哪些图书也不知道,图书室的读者信息和借阅信息也 ...

  5. ASP.NET MVC CODE FIRST 图书管理系统 数据库

    ASP .NET MVC CODE FIRST 图书管理系统 本项目最后更新于2018-7-4,可能会因为没有更新而失效.如已失效或需要修正,请提issue! 我使用VS2017进行开发,框架是.NE ...

  6. 面对对象编程——用Python写一个图书管理系统

    问题描述 图书管理系统1.查询图书2.增加图书3.借阅图书4.归还图书5.退出系统 代码如下 # 书:书名,作者,状态,位置 # 管理系统: class Book(object):def __init ...

  7. s1考试 图书管理系统 结构体版

    #include <iostream> #include <string> #include <cstdio> #include <cstdlib> # ...

  8. C语言课程设计—图书管理系统

    这是本人大一第二学期初C语言课程设计的作品,嘿嘿,本来以为已经找不到原稿了,今天无意中居然在QQ网络硬盘中找到了当初的teta版,公布于此,以作纪念. C源码例如以下: #include<std ...

  9. 图书管理系统python怎么保存用户注册信息_Python实现图书管理系统

    Python实现图书管理系统 功能描述 1.界面分为两个部分,分别是(1)登录注册界面(2)图书管理系统界面 2.用户名和密码提前存储在列表中,输入用户名或密码错误提示重新输入,未注册的需要先注册帐号 ...

  10. 图书管理系统python源代码-Python实现图书管理系统

    本文实例为大家分享了python实现图书管理系统的具体代码,供大家参考,具体内容如下 import mysql.connector import sys, os import time import ...

最新文章

  1. 快速幂 + 矩阵快速幂
  2. 复杂人机智能系统功能分配方法综述
  3. python读取文件夹图片_读取文件夹里的图片,并且与标签对应
  4. 开发日记-20190428
  5. python字频统计软件_python结巴分词以及词频统计实例
  6. 在linux安装requests库命令,在Linux--Ubuntu18.04环境下安装requests库
  7. oracle 布尔转换java布尔_java 布尔值一种赋值方法
  8. 阿里巴巴十周年庆(预告)
  9. 从Client应用场景介绍IdentityServer4(二)
  10. WIN7下的ORACLE精简版客户端(ORACLE Instant Client)安装与配置指南
  11. ad6怎么画电阻_光敏电阻传感器的使用
  12. Python语音基础操作--2.4语音信号生成
  13. MSP430初学three
  14. [iOS]仿微博视频边下边播之封装播放器
  15. 人工智能AI讲师NLP培训讲师叶梓:人工智能之最新NLP自然语言处理技术与实战-23
  16. 自动化测试工程师_所以你想成为一名测试自动化工程师
  17. 销售使用企业微信SCRM管理系统有什么好处
  18. 浙江环宇集团“营改增”项目启动会成功举办
  19. PHP中空格占位数吗,html内的空格占位
  20. build path功能详解 在项目上右键》Build path》Config build path

热门文章

  1. [leetcode] Generate Parenthese
  2. [BZOJ3275]Number(最小割)
  3. hdu 5575 Discover Water Tank 左偏树
  4. oracle 计算日期季度,Oracle日期查询:季度、月份、星期等时间信息
  5. SCU - 4438(字符串hash)
  6. USB3.0、PCIe、PCI等各总线速度对比与介绍
  7. 生物信息学练习题-三阳
  8. Activiti获取批注信息
  9. C++头文件和源文件的关系
  10. myeclipse中启动tomcat是报错如下: Caused by: java.lang.NoClassDefFoundError: Lorg/springframework/web/contex