C++ 高校图书管理系统
Posted 流楚丶格念
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 高校图书管理系统相关的知识,希望对你有一定的参考价值。
项目链接
https://download.csdn.net/download/weixin_45525272/12522372
代码
main.cpp
/*********************************************************************
*Target:高校图书馆管理系统的实现
*Author:杨永利
*Date:2021.06.27
*1、2021.06.27 micro·杨 建立项目 无修改
*********************************************************************/
#include"pch.h"
#include"Book.h"
#include"Reader.h"
#include"Register.h"
int main()
{
cout << "初始账号,密码:123 123" << endl;
cout << endl;
MainDesk yourdesk;
if (yourdesk.Register())
{
cout << "感谢使用本产品,再见!";
}
return 0;
}
Book.h
#pragma once
#include "pch.h"
#include <iostream>
#include <string.h>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int MaxBook = 100; //最大的图书量
//const int MaxReader = 100; //最大的读者数
//const int MaxBorrow = 5; //每次最多可以借书的数量
//图书类
class Book
{
public:
//获取书名
char *GetBookName()
{
return BookName;
}
//获取作者
char *GetAuthor()
{
return Author;
}
//获取书籍ID
int GetBookID()
{
return BookID;
}
//获取书的类别
char *GetClassify()
{
return Classify;
}
//获取出版社
char *GetPress()
{
return Press;
}
//获取出版社时间
int GetPublicationTime()
{
return PublicationTime;
}
//获取价格
double GetBookPrice()
{
return BookPrice;
}
//获取标签
int GetTag()
{
return Tag;
}
//设置编号
void SetBookID(int bookID)
{
BookID = bookID;
}
//设置书名
void SetBookName(char BookName[20])
{
strcpy(BookName, BookName);
}
//设置作者
void SetAuthor(char author[20])
{
strcpy(Author, author);
}
//设置书籍类别
void SetClassify(char classify[20])
{
strcpy(Classify, classify);
}
//设置出版社
void SetPress(char press[20])
{
strcpy(Press, press);
}
//设置出版时间
void SetPublicationTime(int publicationtime)
{
PublicationTime = publicationtime;
}
//设置价格
void SetBookPrice(double bookprice)
{
BookPrice = bookprice;
}
//设置是否在架
void SetOnshelf(double onshelf)
{
Onshelf = onshelf;
}
//删除书籍
void Deletebook()
{
cout << "你确定删除书籍吗? Y/N" << endl;
char i;
cin >> i;
if (i == 'y' || i == 'Y')
{
Tag = 1;
}
}
//增加书籍
void AddBook(int bookID, char* bookname, char*auther,
char*classify, char * press, int publicationtime,
double bookprice, int onshelf)
{
Tag = 0;
BookID = bookID;
strcpy(BookName, bookname);
strcpy(Author, auther);
strcpy(Classify, classify);
strcpy(Press, press);
PublicationTime = publicationtime;
BookPrice = bookprice;
Onshelf = onshelf;
}
//借书操作
int BorrowBook()
{
if (Onshelf > 0)
{
Onshelf--; //库存-1
return 1;
}
return 0;
}
//还书操作
void ReturnBook()
{
Onshelf++;
}
//预览书籍信息(打印书籍信息,每本 本本书)
void DisBook()
{
cout << BookID << setw(10) << BookName << setw(10) << Author;
cout << setw(10) << Classify <<setw(10)<< Press <<setw(10) << PublicationTime;
cout << setw(10) << BookPrice << setw(10) << Onshelf << endl;
}
private:
int Tag; //标签 1已删除 0未删除
int BookID; //编号
char BookName[20]; //书名
char Author[20]; //作者
char Classify[20]; //分类
char Press[20]; //出版社
int PublicationTime; //出版时间
double BookPrice; //价格
int Onshelf; //是否在架 .在架>0 .已借出=0
};
//图书库类:实现图书的增删改查等维护功能。
class BookDateBase
{
public:
//构造函数用于读取上一次书籍读取情况
BookDateBase()
{
top = -1;
Book b;
fstream file("book.txt", ios::in);
while (true)
{
file.read((char*)&b, sizeof(b));
if (!file)
{
break; //未读到数据 跳出循环
}
top++;
book[top] = b;
}
file.close();
}
//删除所有书籍
void ClearDate()
{
cout << "确定要删除全部数据吗? Y/N" << endl;
char i;
cin >> i;
if (i == 'y' || i == 'Y')
{
top = -1;
}
}
//增加书籍
int AddBook(int bookID, char* bookname, char*author,
char*classify, char * press, int publicationtime,
double bookprice, int onshelf)
{
Book*p = Query1(bookID); //书籍不存在,可以进货
if (p == 0)
{
top++;
book[top].AddBook(bookID, bookname, author, classify,
press, publicationtime, bookprice, onshelf);
return 1;
}
return 0;
}
//按照编号查找书籍
Book *Query1(int bookid)
{
for (int i = 0; i < top; i++)
{
if (book[i].GetBookID() == bookid && book[i].GetTag() == 0)
{
return &book[i];
}
}
return 0;
}
//按照书名查找书籍
Book *Query2(char bookname[])
{
Book *e;
int r = 0;
for (int i = 0; i < top; i++)
{
if (strcmp(book[i].GetBookName(), bookname) == 0 && book[i].GetTag() == 0)
{
//栏目标签
cout << "编号" << setw(10) << "书名" << setw(10) << "作者" << setw(10);
cout << "分类" << setw(10) << "出版社" << setw(10) << "出版时间" << setw(10);
cout << "价格" << setw(10) << "存量" << endl;
e = &book[i];
e->DisBook();
r++;
}
}
if (r == 0)
{
cout << "查无此书" << endl;
}
return 0;
}
//按照作者查找书籍
Book *Query3(char author[])
{
Book *e;
int r = 0;
for (int i = 0; i < top; i++)
{
if (strcmp(book[i].GetBookName(), author) == 0 && book[i].GetTag() == 0)
{
//栏目标签
cout << "编号" << setw(10) << "书名" << setw(10) << "作者" << setw(10);
cout << "分类" << setw(10) << "出版社" << setw(10) << "出版时间" << setw(10);
cout << "价格" << setw(10) << "存量" << endl;
e = &book[i];
e->DisBook();
r++;
}
}
if (r == 0)
{
cout << "查无此书!" << endl;
}
return 0;
}
//按照出版社查找书籍
Book *Query4(char press[])
{
Book *e;
int r = 0;
for (int i = 0; i < top; i++)
{
if (strcmp(book[i].GetBookName(), press) == 0 && book[i].GetTag() == 0)
{
//栏目标签
cout << "编号" << setw(10) << "书名" << setw(10) << "作者" << setw(10);
cout << "分类" << setw(10) << "出版社" << setw(10) << "出版时间" << setw(10);
cout << "价格" << setw(10) << "存量" << endl;
e = &book[i];
e->DisBook();
r++;
}
}
if (r == 0)
{
cout << "查无此书!" << endl;
}
return 0;
}
//显示图书管的信息
void DispDate()
{
for (int i = 0; i <= top; i++)
{
//显示未删除的书籍信息
if (book[i].GetTag() == 0)
{
book[i].DisBook();
}
}
}
//图书库信息
void bookdate();
//将当前的书籍借还情况写入文件
~BookDateBase()
{
fstream file("book.txt", ios::out);
for (int i = 0; i <= top; i++)
{
if (book[i].GetTag() == 0)
{
file.write((char*)&book[i], sizeof(book[i]));
}
}
file.close();
}
private:
int top; //图书信息存量
Book book[MaxBook]; //图书的最大存量
};
//***BookDateBase类里的图书库信息
void BookDateBase::bookdate()
{
Book *b; //书籍的指针
int bookID; //编号
char bookname[20]; //书名
char author[20]; //作者
char classify[20]; //分类
char press[20]; //出版社
int publicationtime; //出版时间
double bookprice; //价格
int onshelf; //是否在架 .在架>0 .已借出=0
char choice1 = 1 ; //输入指令
char choice2 = 0; //输入指令
char choice3 = 0; //输入指令
char choice4 = 0; //输入指令
while (choice1 != '0')
{
cout << "**************************************************************" << endl;
cout << "** **" << endl;
cout << "**** 图书管理系统 ****" << endl;
cout << "** **" << endl;
cout << "**************************************************************" << endl;
cout << "**************************************************************" << endl;
cout << "** **" << endl;
cout << "** 1.增加图书 2.更改图书 3.删除图书 **" << endl;
cout << "** 4.查询图书 5.显示图书 6.清空图书 **" << endl;
cout << "** **" << endl;
cout << "** 0.返回上一级目录 **" << endl;
cout << "** **" << endl;
cout << "**************************************************************" << endl;
cout << "请选择:0-6" << endl;
cin >> choice1;
switch (choice1)
{
case'1':
{
cout << "请输入增加图书的编号:";
cin >> bookID;
b = Query1(bookID);
if (b != 0)
{
cout << "该书籍已存在无法重复添加!" << endl;
break;
}
cout << "请输入增加书籍的书名:";
cin >> bookname;
cout << "请输入增加书籍的作者:";
cin >> author;
cout << "请输入增加书籍的分类:";
cin >> classify;
cout << "请输入增加书籍的出版社:";
cin >> press;
cout << "请输入增加书籍的出版时间:";
cin >> publicationtime;
cout << "请输入增加书籍的价格:";
cin >> bookprice;
cout << "请输入增加书籍的数量:";
cin >> onshelf;
int t;
t = AddBook(bookID,以上是关于C++ 高校图书管理系统的主要内容,如果未能解决你的问题,请参考以下文章