📄 book.h
字号:
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
const int MBN=104;
static int BN=1; //静态图书编号
class Book
{
private:
//图书存储结构
int bNum[MBN][10]; //书号采用二维数组记录,用来方便同类书的操作(最多10本同类书)
char bName[MBN][20];
char bWriter[MBN][20];
int BExcess[MBN]; //同类书剩余量
int BTotal[MBN];//同类书总量
int topn[MBN]; //记录一类图书栈顶标号
int btop;
int bFind[MBN];//临时记录检索到的结果
//图书索引结构
int IbNum[MBN][3]; //1 编号值记录 2图书地址 3记录标识判断当前状态1在架 0不在架 2预借出去
int Ibtop;
char IbName[MBN][20];//书名
int IbAddN[MBN];
char IbWriter[MBN][20];//作者
int IbAddW[MBN];
public:
Book();
~Book();//析构(保存文件)
int H(char n[]); //散列函数
void ReadBook();//读取图书索引文件
int IsEmpty();//查空
int IsOnshelf(int bnum);//核对指定编号图书是在架
void ViewOne(int n);//输出指定编号图书信息
void FiEmpty();//查找结果置空函数
//************************仅管理员操作函数******************************
void AddBook(char bname[],char bwriter[],int btotal);//添加图书
void CleanBook();//清空书库
void ViewAll();//浏览全部图书
//************************可供读者操作函数******************************
//图书检索函数
int FindNum(int n);//书号
int FindName(char bname[],int flag);//书名
int FindWriter(char bwriter[],int flag);//作者
int BorBook(int bn);//借书
int DesBook(int bn);//预借
void ReturnBook(int bnum);//还书
int DestineBook(int n);//预定借阅
};
/////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////Book.cpp
//初始化
Book::Book()
{
btop =-1;
Ibtop =-1;
for(int i=0; i<MBN; i++)
{
bFind[i]=-1;
IbNum[i][0]=-1; //书号
IbNum[i][1]=-1;
IbNum[i][2]=0;//初始为未借出
IbName[i][0] ='\0';
IbWriter[i][0] ='\0';
topn[i]=-1;
for(int j=0;j<10;j++)
{
bNum[i][j]=-1;
}
}
}
//检查空
int Book::IsEmpty()
{
if(btop!=-1) return 1;
else return 0;
}
//给定书号核对图书是否在架 1在架 0不在架 2预借出去
int Book::IsOnshelf(int bnum) //bnum为图书书号
{
if(IbNum[bnum-1][2]==1)
return 1;
else
return 0;
}
//输出指定编号图书信息
void Book::ViewOne(int n)
{
int h=IbNum[n-1][1];
if(h!=-1)
{
cout <<IbNum[n-1][0] <<"\t"
<<bName[h] <<"\t\t"
<<bWriter[h] << "\t\t"
<<BExcess[h] <<"\t"
<<BTotal[h] <<endl;
}
else
cout<<"不存在所要查看的图书信息"<<endl;
}
void Book::FiEmpty()
{
for(int i=0;i<MBN;i++)
{
bFind[i]=-1;
}
}
void Book::ReadBook()
{
int i,j,k;
//图书结构读取
fstream file("Book.txt",ios::in);
file >>btop >>BN;
if(btop !=-1)
{
while(!file.eof())
{
for(i=0; i<=btop; i++)
{
for(j=0; j<10; j++)
file >>bNum[i][j];
file >>topn[i];
file >>bName[i];
file >>bWriter[i];
file >>BExcess[i];
file >>BTotal[i];
}
}
}
file.close();
//书号索引读取
i=0;
ifstream ifile1("IndexNum.txt",ios::in);
ifile1 >>Ibtop;
while(!ifile1.eof())
{
ifile1 >>IbNum[i][0] >>IbNum[i][1] >>IbNum[i][2];
i++;
}
ifile1.close();
//书名索引读取
ifstream ifile2("IndexName.txt",ios::in);
while(!ifile2.eof())
{
ifile2 >>k;
ifile2 >>IbName[k];
ifile2 >>IbAddN[k];
}
ifile2.close();
//作者索引读取
ifstream ifile3("IndexWriter.txt",ios::in);
while(!ifile3.eof())
{
ifile3 >>k;
ifile3 >>IbWriter[k];
ifile3 >>IbAddW[k];
}
ifile3.close();
}
Book::~Book()
{
int i,j;
//图书结构保存
ofstream fout("Book.txt");
fout <<btop <<" "<<BN <<" ";
for ( i=0;i<=btop;i++)
{
for(j=0; j<10; j++)
{
fout <<bNum[i][j] <<" ";
}
fout <<topn[i] <<" "
<<bName[i] <<" "
<<bWriter[i] <<" "
<<BExcess[i] <<" "
<<BTotal[i]<<" ";
}
fout.close();
//书号索引保存
ofstream ifout1("IndexNum.txt",ios::out);
ifout1 <<Ibtop <<" ";
for ( i=0;i<=Ibtop;i++)
{
ifout1 <<IbNum[i][0] <<" "
<<IbNum[i][1] <<" "
<<IbNum[i][2] <<" ";
}
ifout1.close();
//书名索引保存
ofstream ifout2("IndexName.txt",ios::out);
for ( i=0;i<MBN;i++)
{
if(IbName[i][0]!='\0')
{
ifout2 <<i <<" "
<<IbName[i] <<" "
<<IbAddN[i] <<" ";
}
}
ifout2.close();
//作者索引保存
ofstream ifout3("IndexWriter.txt",ios::out);
for ( i=0;i<MBN;i++)
{
if(IbWriter[i][0]!='\0')
{
ifout3 <<i <<" "
<<IbWriter[i] <<" "
<<IbAddW[i] <<" ";
}
}
ifout3.close();
}
int Book::H(char n[])//散列方法
{
return 4*(n[0]-'a');
}
//***************************************************************************管理员操作书函数
//清空图书
void Book::CleanBook()
{
BN=1;
btop =-1;
Ibtop =-1;
for(int i=0; i<MBN; i++)
{
IbNum[i][0]=-1; //书号
IbNum[i][1]=-1;
IbNum[i][2]=0;
IbName[i][0] ='\0';
IbWriter[i][0] ='\0';
topn[i]=-1;
for(int j=0;j<10;j++)
{
bNum[i][j]=-1;
}
}
}
void Book::ViewAll()
{
for(int i=0; i<=Ibtop; i++)
{
cout <<IbNum[i][0] <<"\t"
<<bName[IbNum[i][1]] <<"\t\t"
<<bWriter[IbNum[i][1]] <<"\t\t"
<<BExcess[IbNum[i][1]] <<"\t"
<<BTotal[IbNum[i][1]] <<"\t";
if(IbNum[i][2]==1)
cout<<"在架"<<endl;
else if(IbNum[i][2]==2)
cout<<"预借"<<endl;
else
cout<<"借出"<<endl;
}
}
//图书入库
void Book::AddBook(char bname[],char bwriter[],int btotal)
{
//图书写入
int n=BN;
int i,j;
btop++;
strcpy(bName[btop],bname);
strcpy(bWriter[btop],bwriter);
BExcess[btop] =BTotal[btop] =btotal;
//系统分配书号
for(i=0; i<btotal; i++)
{
bNum[btop][i]=BN;
BN++;
topn[btop]++;
}
//索引建立
//书号索引
for(j=n-1; j<BN-1; j++)
{
IbNum[j][0] =bNum[btop][j-n+1];
IbNum[j][1] =btop;
IbNum[j][2] =1;
}
Ibtop+=BN-n;
//书名检索
int h;//散列位置
h =H(bname);
while(IbName[h][0]!='\0')
{
h++;
if(h>103) h=h-104;
}
strcpy(IbName[h],bname);
IbAddN[h]=btop;
//作者检索
int g;//散列位置
g =H(bwriter);
while(IbWriter[g][0]!='\0')
{
g++;
if(g>103) g=g-104;
}
strcpy(IbWriter[g],bwriter);
IbAddW[g]=btop;
}
//********************************************************************************检索操作函数
//书号检索
int Book::FindNum(int n) //通过 返回IbNum[h][1]为书物理地址 返回-1为没找到
{
int h =IbNum[n-1][1];
if(h!=-1)
{
cout<<"以下为您检索的图书结果~~~\n"
<<"序号"<<"\t"
<<"书名"<<"\t\t"
<<"作者"<<"\t\t"
<<"剩余量"<<"\t"
<<"总量"<<"\t"
<<"当前状态"<<endl;
cout<<"-------------------------------------------------------------------"<<endl;
cout <<IbNum[n-1][0] <<"\t"
<<bName[h] <<"\t\t"
<<bWriter[h] << "\t\t"
<<BExcess[h] <<"\t"
<<BTotal[h] <<"\t";
if(IbNum[h][2]==1)
cout<<"在架"<<endl;
else if(IbNum[h][2]==2)
cout<<"预借"<<endl;
else
cout<<"借出"<<endl;
return h;
}
else
return -1;
}
//书名检索
int Book::FindName(char bname[],int flag)
{
int h=0;//散列地址存储
int n=0;//计算输入书名的字节
int j=0;//bFind[]游标
int fn=0;//找到图书的本数
char ch;
int bn;//读者选择序号
cout<<"以下为您检索的图书结果~~~\n"
<<"序号"<<"\t"
<<"书名"<<"\t\t"
<<"作者"<<"\t\t"
<<"剩余量"<<"\t"
<<"总量"<<endl;
cout<<"-------------------------------------------------------------------"<<endl;
while(bname[n]!='\0')
n++;
for(int i=0; i<n; i++)//控制循环可能
{
int sign=0;//标记
h=H(bname);
//根据地址查找对应书
while(IbName[h][0]!='\0')
{
sign=0;
for(int k=0; k<n-i; k++)
{
if(IbName[h][k]==bname[k])
sign++;//记录匹配字符数
}
if(fn==0)//当前无结果优先输出头字符串完全匹配的结果
{
if(sign==n-i)
{
fn++;
bFind[j++]=IbAddN[h];
cout <<fn <<"\t"
<<bName[IbAddN[h]] <<"\t\t"
<<bWriter[IbAddN[h]] <<"\t\t"
<<BExcess[IbAddN[h]] <<"\t"
<<BTotal[IbAddN[h]] <<endl;
}
h++;
if(h>103)
h=h-104;
}
else
{
if(sign==n-i && IbName[h][n-i]!=bname[n-i])
{
fn++;
bFind[j++]=IbAddN[h];
cout <<fn <<"\t"
<<bName[IbAddN[h]] <<"\t\t"
<<bWriter[IbAddN[h]] <<"\t\t"
<<BExcess[IbAddN[h]] <<"\t"
<<BTotal[IbAddN[h]] <<endl;
}
h++;
if(h>103)
h=h-104;
}
}
}
cout<<"-------------------------------------------------------------------\n"
<<"共找到"<<fn<<"个符合的结果"<<endl;
if(fn==0 || flag==0)
{
cout<<"按任意键返回~~";
ch=getch();
return -1;//没找到
}
else
{
cout<<"是否要借阅上述图书(y/Y确定,其他键回车则返回)";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"请输入您要借阅上述图书的序号";
while(1)
{
cin>>bn;
if(bn>0 && bn<=fn)
{
return bFind[bn-1];
break;
}
else
cout<<"输入错误请按规格正确输入……";
}
}
else
return -1;//没有满足读者要求的图书
}
}
//作者检索
int Book::FindWriter(char bwriter[],int flag)
{
int j=0;//bFind[]游标
char ch;
int bn;//读者选择序号
cout<<"以下为您检索的图书结果~~~\n"
<<"序号"<<"\t"
<<"书名"<<"\t\t"
<<"作者"<<"\t\t"
<<"剩余量"<<"\t"
<<"总量"<<endl;
cout<<"-------------------------------------------------------------------"<<endl;
int h=H(bwriter);
int sign=0;
while(IbWriter[h][0]!='\0')
{
if(strcmp(IbWriter[h],bwriter)==0)
{
sign++;
bFind[j++]=IbAddW[h];
cout <<sign <<"\t"
<<bName[IbAddW[h]] <<"\t\t"
<<bWriter[IbAddW[h]] <<"\t\t"
<<BExcess[IbAddW[h]] <<"\t"
<<BTotal[IbAddW[h]] <<endl;
}
h++;
if(h>103) h=h-104;
}
cout<<"-------------------------------------------------------------------\n"
<<"共找到"<<sign<<"个符合的结果"<<endl;
if(sign==0 || flag==0) //flag为0 则是管理员调用函数
{
cout<<"按任意键返回~~";
ch=getch();
return -1;//没找到
}
else
{
cout<<"是否要借阅上述图书(y/Y确定,其他键回车则返回)";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"请输入您要借阅上述图书的序号";
while(1)
{
cin>>bn;
if(bn>0 && bn<=sign)
{
return bFind[bn-1];
break;
}
else
cout<<"输入错误请按规格正确输入……";
}
}
else
return -1;//没有满足读者要求的图书
}
}
//**************************************************************************对图书借还的操作
//借书
int Book::BorBook(int bn) //返回所借图书的编号 若返回0则图书无库存,操作无法进行
{
if(BExcess[bn]>0)
{
for(int i=0;i<=topn[bn];i++)
{
if((IsOnshelf(bNum[bn][i]))==1)
{
BExcess[bn]--;//剩于量修改+1
IbNum[bNum[bn][i]-1][2] =0;//书号检索中书状态修改 置0表示已借出
return IbNum[bNum[bn][i]-1][0];//返回选定图书编号
}
}
}
return 0;
}
//预借
int Book::DesBook(int bn)
{
if(BExcess[bn]>0)
{
for(int i=0;i<=topn[bn];i++)
{
if((IsOnshelf(bNum[bn][i]))==1)
{
BExcess[bn]--;//剩于量修改+1
IbNum[bNum[bn][i]-1][2] =2;//书号检索中书状态修改 置2表示已被预借
return IbNum[bNum[bn][i]-1][0];//返回选定图书编号
}
}
}
return 0;
}
//还书
void Book::ReturnBook(int bnum) //bnum为图书书号
{
IbNum[bnum-1][2]=1;
BExcess[IbNum[bnum][1]]++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -