?? bookfind.h
字號:
#include "define.h"
class BookFind
{
private:
book data; //當前處理的圖書記錄
int success; //折半查找標志
long *HashData; //申請HASH表的存儲空間
indexISBN *IndexData; //保存ISBN索引表
public:
BookFind(void);
~BookFind(void);
book operator=(book c); //運算符重載
void DisplayData(void) const; //顯示當前處理的圖書記錄
int SeqFind(char ISBN[11]); //順序查詢與輸入的ISBN相同的所有ISBN(隨機產生的ISBN可能相同),并顯示。
int IndexSeqFind(char ISBN[11]); //索引順序查詢與輸入的ISBN相同的所有ISBN(隨機產生的ISBN可能相同),并顯示。
long BiSearch(char ISBN[11],long start,long end); //在索引表中折半查找ISBN號,成功則success=1,返回其位置(在索引表中的相對位置)
int IndexBinFind(char ISBN[11]); //索引折半查詢與輸入的ISBN相同的所有ISBN(隨機產生的ISBN可能相同),并顯示。
int HashIndexBinFind(char ISBN[11]); //哈希索引折半查詢與輸入的ISBN相同的所有ISBN(隨機產生的ISBN可能相同),并顯示。
};
book BookFind::operator=(book c)
{
book x;
strcpy(x.ISBN,c.ISBN);
x.category=c.category;
strcpy(x.bookname,c.bookname);
strcpy(x.authors,c.authors);
return x;
}
BookFind::BookFind(void) //構造函數
{
success=0;
IndexData=new indexISBN[RECORDNUMBER]; //保存ISBN索引表
if(IndexData==NULL) { cout<<endl<<" 索引表內存申請失敗! "<<endl; exit(0); }
ifstream indexfile; //定義輸入的索引文件
indexfile.open("indexdata.dat",ios::nocreate|ios::binary);
if(!indexfile)
{
cout<<endl<<" !! ERROR:Cannot open file 'indexdata.dat'. "<<endl;
exit(0);
}
long size=0;
indexISBN tempINDEX; //臨時變量
while(indexfile.good()&&size<RECORDNUMBER) //讀索引數據文件信息
{
indexfile.read((char *)&tempINDEX,sizeof(struct indexISBN));
IndexData[size++]=tempINDEX;
}
indexfile.close();
HashData=new long[HASHSIZE]; //申請HASH表的存儲空間
if(HashData==NULL) { cout<<endl<<" HASH表內存申請失敗! "<<endl; exit(0); }
ifstream hashfile;
hashfile.open("hashdata.dat",ios::binary); //打開HASH表文件hashdata.dat
if(!hashfile)
{
cout<<endl<<" !! ERROR:Cannot open file: hashdata.dat "<<endl;
exit(0);
}
int hashnum=0;
while(hashfile.good()&&hashnum<HASHSIZE) //將HASH表讀入內存
{
hashfile.read((char *)&HashData[hashnum],sizeof(long));
hashnum=hashnum+1;
}
hashfile.close();
}
BookFind::~BookFind(void) //統一保存文件頭信息
{
delete []IndexData;
delete []HashData;
}
void BookFind::DisplayData(void) const
{
cout<<data.ISBN<<' '<<data.category<<' '<<data.bookname<<' '<<data.authors<<endl;
}
int BookFind::SeqFind(char ISBN[11])
{ //尋找元素序列號為ISBN的所有圖書記錄,因為可能重復,故需要全程搜索
int snum=0;
ifstream myinfile;
myinfile.open("book.dat",ios::binary);
if(myinfile)
{
long currptr=0; //圖書記錄的起始下標
cout<<endl<<"順序查詢ISBN為: "<<ISBN<<" 的圖書的,請稍候......"<<endl;
DWORD dwstart,dwend;
dwstart=GetTickCount(); //記錄開始時間
while(myinfile.good()&&currptr<RECORDNUMBER)
{
myinfile.read((char *)&data,sizeof(book));
if(strcmp(data.ISBN,ISBN)==0) { snum++; cout<<"第"<<snum<<"條記錄:"; DisplayData(); } //找到并顯示
currptr++;
}
myinfile.close();
dwend=GetTickCount(); //記錄結束時間
cout<<"共耗時約: "<<dwend-dwstart<<" 毫秒。"<<endl;
cout<<"共找到 "<<snum<<" 條圖書記錄!"<<endl;
}
else
{
cout<<endl<<" !! ERROR:Cannot open file 'book.dat'. "<<endl; exit(0);
}
return snum;
}
int BookFind::IndexSeqFind(char ISBN[11])
{
int snum=0;
//在索引表中順序查詢
ifstream myinfile;
myinfile.open("book.dat",ios::binary);
if(!myinfile) {cout<<endl<<" !! ERROR:Cannot open file 'book.dat'. "<<endl; exit(0); };
cout<<endl<<"索引順序查詢ISBN為: "<<ISBN<<" 的圖書的,請稍候......"<<endl;
DWORD dwstart,dwend;
dwstart=GetTickCount(); //記錄開始時間
long size=0;
while(size<RECORDNUMBER)
{
if(strcmp(IndexData[size].ISBN,ISBN)==0) //在索引表中存在
{
myinfile.seekg(IndexData[size].serialnumber*sizeof(struct book)); //定位數據在book中的位置
myinfile.read((char *)&data,sizeof(struct book)); //讀入數據
snum++;
cout<<"第"<<snum<<"條記錄:"; DisplayData(); //找到并顯示
}
if(strcmp(IndexData[size].ISBN,ISBN)>0) break;
size++;
}
myinfile.close();
dwend=GetTickCount(); //記錄結束時間
cout<<"共耗時約: "<<dwend-dwstart<<" 毫秒。"<<endl;
cout<<"共找到 "<<snum<<" 條圖書記錄!"<<endl;
return snum;
}
long BookFind::BiSearch(char ISBN[11],long start,long end)
//折半查找ISBN號,成功則success=1,返回其位置(在索引表中的相對位置)。
{
success=0;
long low=start, high=end-1;
long mid;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(IndexData[mid].ISBN,ISBN)==0) { success=1; return mid; }
else if(strcmp(IndexData[mid].ISBN,ISBN)<0) low=mid+1;
else high=mid-1;
}
return high;
}
int BookFind::IndexBinFind(char ISBN[11])
{ //在索引表中折半查詢
long Findnumber=0; //找到的圖書計數
ifstream mydatafile;
mydatafile.open("book.dat",ios::binary); //打開數據源文件
if(!mydatafile)
{
cout<<endl<<" !! ERROR:Cannot open file 'book.dat'. "<<endl;
exit(0);
}
cout<<endl<<"索引折半查詢ISBN為: "<<ISBN<<" 的圖書的,請稍候......"<<endl;
DWORD dwstart,dwend;
dwstart=GetTickCount(); //記錄開始時間
long Fpos=BiSearch(ISBN,0,RECORDNUMBER); //Fpos為在索引表中的相對位置
if(success==1)
{
long Fup=Fpos-1;
long Fdown=Fpos+1; //在找到點Fpos分別向上和向下查詢是否有相同的ISBN
if(IndexData[Fpos].serialnumber>-1&&IndexData[Fpos].serialnumber<RECORDNUMBER) //保證數據的正確性
{
mydatafile.seekg(IndexData[Fpos].serialnumber*sizeof(struct book)); //定位數據在book中的位置
mydatafile.read((char *)&data,sizeof(struct book)); //讀入數據
cout<<"第"<<Findnumber<<"條記錄:"; DisplayData(); //找到并顯示
Findnumber=Findnumber+1; //找到的書計數加1
}
while(Fup>=0&&strcmp(IndexData[Fup].ISBN,ISBN)==0) //向上查詢
{
if(IndexData[Fup].serialnumber>-1&&IndexData[Fup].serialnumber<RECORDNUMBER) //保證數據的正確性
{
mydatafile.seekg(IndexData[Fpos].serialnumber*sizeof(struct book)); //定位數據在book中的位置
mydatafile.read((char *)&data,sizeof(struct book)); //讀入數據
cout<<"第"<<Findnumber<<"條記錄:"; DisplayData(); //找到并顯示
Findnumber=Findnumber+1; //找到的書計數加1
}
Fup=Fup-1; //繼續向上
}
while(Fdown<RECORDNUMBER&&strcmp(IndexData[Fdown].ISBN,ISBN)==0) //向下查詢
{
if(IndexData[Fdown].serialnumber>-1&&IndexData[Fdown].serialnumber<RECORDNUMBER) //保證數據的正確性
{
mydatafile.seekg(IndexData[Fpos].serialnumber*sizeof(struct book)); //定位數據在book中的位置
mydatafile.read((char *)&data,sizeof(struct book)); //讀入數據
cout<<"第"<<Findnumber<<"條記錄:"; DisplayData(); //找到并顯示
Findnumber=Findnumber+1; //找到的書計數加1
}
Fdown=Fdown+1; //繼續向下
}
}
mydatafile.close();
dwend=GetTickCount(); //記錄結束時間
cout<<"共耗時約: "<<dwend-dwstart<<" 毫秒。"<<endl;
cout<<"共找到 "<<Findnumber<<" 條圖書記錄!"<<endl;
return Findnumber;
}
int BookFind::HashIndexBinFind(char ISBN[11])
{ //在哈希表查詢和索引表中折半查詢
long Findnumber=0; //找到的圖書計數
ifstream mydatafile;
mydatafile.open("book.dat",ios::binary); //打開數據源文件
if(!mydatafile)
{
cout<<endl<<" !! ERROR:Cannot open file 'book.dat'. "<<endl;
exit(0);
}
cout<<endl<<"哈希索引折半查詢ISBN為: "<<ISBN<<" 的圖書的,請稍候......"<<endl;
DWORD dwstart,dwend;
dwstart=GetTickCount(); //記錄開始時間
char temp[5];
int j;
int hashvalue;
for(j=0;j<=3;j++) temp[j]=ISBN[j]; //取ISBN中的第0-3共四位數據
temp[4]='\0';
hashvalue=atoi(temp); //將字符串轉換為記錄對應的HASH數值
long Fpos=BiSearch(ISBN,HashData[hashvalue],HashData[hashvalue+1]); //Fpos為在索引表中的相對位置
if(success==1)
{
long Fup=Fpos-1;
long Fdown=Fpos+1; //在找到點Fpos分別向上和向下查詢是否有相同的ISBN
if(IndexData[Fpos].serialnumber>-1&&IndexData[Fpos].serialnumber<RECORDNUMBER) //保證數據的正確性
{
mydatafile.seekg(IndexData[Fpos].serialnumber*sizeof(struct book)); //定位數據在book中的位置
mydatafile.read((char *)&data,sizeof(struct book)); //讀入數據
cout<<"第"<<Findnumber<<"條記錄:"; DisplayData(); //找到并顯示
Findnumber=Findnumber+1; //找到的書計數加1
}
while(Fup>=0&&strcmp(IndexData[Fup].ISBN,ISBN)==0) //向上查詢
{
if(IndexData[Fup].serialnumber>-1&&IndexData[Fup].serialnumber<RECORDNUMBER) //保證數據的正確性
{
mydatafile.seekg(IndexData[Fpos].serialnumber*sizeof(struct book)); //定位數據在book中的位置
mydatafile.read((char *)&data,sizeof(struct book)); //讀入數據
cout<<"第"<<Findnumber<<"條記錄:"; DisplayData(); //找到并顯示
Findnumber=Findnumber+1; //找到的書計數加1
}
Fup=Fup-1; //繼續向上
}
while(Fdown<RECORDNUMBER&&strcmp(IndexData[Fdown].ISBN,ISBN)==0) //向下查詢
{
if(IndexData[Fdown].serialnumber>-1&&IndexData[Fdown].serialnumber<RECORDNUMBER) //保證數據的正確性
{
mydatafile.seekg(IndexData[Fpos].serialnumber*sizeof(struct book)); //定位數據在book中的位置
mydatafile.read((char *)&data,sizeof(struct book)); //讀入數據
cout<<"第"<<Findnumber<<"條記錄:"; DisplayData(); //找到并顯示
Findnumber=Findnumber+1; //找到的書計數加1
}
Fdown=Fdown+1; //繼續向下
}
}
mydatafile.close();
dwend=GetTickCount(); //記錄結束時間
cout<<"共耗時約: "<<dwend-dwstart<<" 毫秒。"<<endl;
cout<<"共找到 "<<Findnumber<<" 條圖書記錄!"<<endl;
return Findnumber;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -