?? 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 + -