⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createrand.cpp

📁 一个海量图书查询系统
💻 CPP
字号:
//程序随机生成100万条图书记录并形成索引表和哈希表
#include "define.h"

char getintchar()
{
	char temp=48+rand()%10;		//随机数为'0'-'9'
	return temp;
}

char getrandchar()
{
	char temp=97+rand()%26;		//随机数为'a'-'z'
	return temp;
}

void getISBN(char ISBN[11])
{
	for(int i=0;i<10;i++) ISBN[i]=getintchar();   //产生随机数ISBN
	ISBN[10]=NULL;
} 

char getcategory()   //产生随机数category
{
	return getintchar(); 
}

void getbookname(char bkname[61])    //产生随机数bookname
{
	for(int i=0;i<60;i++) bkname[i]=getrandchar();  
	bkname[60]=NULL;
}

void getauthors(char aut[11])    //产生随机数author
{
	for(int i=0;i<10;i++) aut[i]=getrandchar();  
	aut[10]=NULL;
}

//索引表信息交换
void exchange(indexISBN &x,indexISBN &y)  
{
	indexISBN temp;
    temp.serialnumber=x.serialnumber; strcpy(temp.ISBN,x.ISBN);  
    x.serialnumber=y.serialnumber; strcpy(x.ISBN,y.ISBN); 
    y.serialnumber=temp.serialnumber; strcpy(y.ISBN,temp.ISBN); 
}

//快速排序
void QuickSort(indexISBN a[],long low,long high)
{
	long i=low,j=high;
	indexISBN temp;
	temp.serialnumber=a[low].serialnumber; strcpy(temp.ISBN,a[low].ISBN);
	while(i<j)
	{
		while(i<j&&strcmp(temp.ISBN,a[j].ISBN)<=0) j--;
		if(i<j)
		{
	        a[i].serialnumber=a[j].serialnumber; strcpy(a[i].ISBN,a[j].ISBN); 			
			i++;
		}
        while(i<j&&strcmp(a[i].ISBN,temp.ISBN)<0) i++;
		if(i<j)
		{
	        a[j].serialnumber=a[i].serialnumber; strcpy(a[j].ISBN,a[i].ISBN);
			j--;
		}
	}
	a[i].serialnumber=temp.serialnumber; strcpy(a[i].ISBN,temp.ISBN); 
	if(low<i) QuickSort(a,low,i-1);
	if(i<high) QuickSort(a,j+1,high);
}

void Createindex(indexISBN a[])   //产生索引表
{
	long h=0;
    ofstream myoutfile;
	myoutfile.open("indexdata.dat",ios::trunc|ios::binary);     //索引数据记录(已排序)存储文件
	if(!myoutfile)
	{
       cout<<endl<<"  !! ERROR: can not create file: indexdata.dat"<<endl;
	   exit(0);
	} 
    QuickSort(a,0,RECORDNUMBER-1);    //快速排序
//保存排序后的索引数据文件
 	for(h=0;h<RECORDNUMBER;h++) 
	{
		myoutfile.write((char *)&a[h],sizeof(struct indexISBN));  
		if(h<10||h>RECORDNUMBER-10) //数据测试数据,头10条记录和后10条记录
		{
	      cout<<a[h].ISBN<<" ";     
	      cout<<a[h].serialnumber<<endl;
		}
	}
	myoutfile.close();
}

void Createhash(indexISBN a[])   //产生哈希表
{
    long *hashtable=new long[HASHSIZE];    //申请HASH表的存储空间
	if(hashtable==NULL) { cout<<endl<<" 内存申请失败! "<<endl;  exit(0); }
//  indexISBN indexdata;    //保存读入的一条索引记录
    ofstream myoutfile;
	myoutfile.open("hashdata.dat",ios::trunc|ios::binary);   //HASH表的数据记录存储文件
	if(!myoutfile)
	{
       cout<<endl<<"  !! ERROR: can not create file: hashdata.dat "<<endl;
	   exit(0);
	} 
    char temp[5];
    for(int i=0;i<HASHSIZE;i++) hashtable[i]=-1;      //hash表赋初值
	long k=0;
	int j;
	int hashvalue;
	while(k<RECORDNUMBER)  //处理数据,建立HASH表
	{
		for(j=0;j<=3;j++)  temp[j]=a[k].ISBN[j];     //取ISBN中的第0-3共四位数据
    	temp[4]='\0';
	    hashvalue=atoi(temp);    //将字符串转换为记录对应的HASH数值
		if(hashtable[hashvalue]==-1) hashtable[hashvalue]=k;   //记录在索引表中的相对位置
		k++;
	} 
	hashtable[HASHSIZE-1]=k; 
//保存HASH表的数据文件
	int h;
 	for(h=0;h<HASHSIZE;h++) myoutfile.write((char *)&hashtable[h],sizeof(long));  
	myoutfile.close();
//输出前10个数据
	for(h=0;h<10;h++) cout<<"position: "<<h<<"   "<<hashtable[h]<<endl; 
//输出后10个数据
	for(h=0;h<10;h++) cout<<"position: "<<HASHSIZE-1-h<<"   "<<hashtable[HASHSIZE-1-h]<<endl; 
	delete []hashtable;     //释放申请的存储空间
}


int main(int argc, char* argv[])
{
	ofstream outfile;     //定义输出数据的记录文件
	outfile.open("book.dat",ios::trunc|ios::binary);   //以二进制保存数据
	if(!outfile) { cout<<endl<<"  !! ERROR: can not create file: book.dat "<<endl;  exit(0);  } 
    ofstream dataout;     
	dataout.open("datatest.txt",ios::trunc);  //以文本保存数据测试数据,头10条记录和后10条记录
	if(!dataout) {  cout<<endl<<"  !! ERROR: can not create headfile: datatest.txt"<<endl;  exit(0); }  
	indexISBN *storeISBN=new indexISBN[RECORDNUMBER];    //保存ISBN号
	if(storeISBN==NULL) { cout<<endl<<" 内存申请失败! "<<endl;  exit(0); }
	book BUFF;   //保存一条完整的图书信息
    DWORD dwstart,dwend;   
    dwstart=GetTickCount();    //记录开始时间
	srand((unsigned)time(NULL));   //srand()函数产生一个以当前时间开始的随机种子
	cout<<"正在处理数据,请稍候......"<<endl;
    long size=0;
	while(size<RECORDNUMBER) //共产生10000*100条随机图书ISBN号记录,ISBN号可以相同
	{
 	    getISBN(storeISBN[size].ISBN);   //产生一条ISBN号
        storeISBN[size].serialnumber=size;   //记录初始序号
        strcpy(BUFF.ISBN,storeISBN[size].ISBN);
	    BUFF.category=getcategory();
        getbookname(BUFF.bookname);
        getauthors(BUFF.authors);
        outfile.write((char *)&BUFF,sizeof(struct book));     //保存一条记录
		if(size<10||size>RECORDNUMBER-10) //以文本保存数据测试数据,头10条记录和后10条记录
		{
	      dataout<<size<<"  ";     
	      dataout<<BUFF.ISBN<<"  "; 
		  dataout<<BUFF.category<<"  ";
          dataout<<BUFF.bookname<<"  "; 
          dataout<<BUFF.authors<<endl; 
		}
        size=size+1;
	}
	outfile.close();
	dataout.close();
    Createindex(storeISBN);    //产生索引表
    Createhash(storeISBN);   //产生哈希表
    dwend=GetTickCount();   //记录结束时间
	cout<<"成功产生 "<<size<<" 条随机图书记录"<<endl;
    cout<<"共耗时约:  "<<dwend-dwstart<<" 毫秒。"<<endl;
    delete []storeISBN;
	return 0; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -