天天看點

資料結構源碼筆記(C語言):索引檔案建立和查找

//實作索引檔案建立和查找算法

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>


#define MaxRec 100//最多的記錄個數


typedef struct Index //定義索引檔案結構
{
	char num[8];//學号
    long offset;//主檔案中的記錄号
}Index;


typedef struct sdata//定義主檔案結構
{
	char num[8];
	char name[10];
	int sex;
	int age;
	char addr[30];
	char dep[20];
	char spec[20];
}Student;


void DelAll()//清除主檔案和索引檔案的全部記錄
{
	FILE *mfile,*idxfile;
	if((mfile=fopen("main.dat","wb"))==NULL)
	{
		printf(">>不能打開主檔案\n");
		return;
	}

	if((idxfile=fopen("idx.dat","wb"))==NULL)
	{
		printf(">>不能打開索引檔案\n");
		return;
	}
	fclose(mfile);
	fclose(idxfile);
}

void InsertSort(Index r[],int n)//對r[0......n-1]按遞增有序進行直接插入排序
{
	int i,j;
	Index temp;
	for(i=1;i<n;i++)
	{
		temp=r[i];
		j=i-1;
		while(j>=0 && strcmp(temp.num,r[j].num)<0)
		{
			r[j+1]=r[j];//将關鍵字大于r[i].key的記錄後移
			j--;
		}
		r[j+1]=temp;//在j+1處插入r[i]
	}
}

void CreatIdxFile()//建立索引檔案
{
	FILE *mfile ,*idxfile;
	Index idx[MaxRec];
	Student st;
	int n=0,i;
	if((mfile=fopen("main.dat","rb"))==NULL)
	{
		printf(">>不能打開主檔案\n");
		return;
	}
	if((idxfile=fopen("index.dat","wb"))==NULL)
	{
		printf(">>不能建立索引檔案\n");
		return;
	}
	i=0;
	while((fread(&st,sizeof(Student),1,mfile))!=NULL)
	{
		strcpy(idx[i].num,st.num);
		idx[i].offset=++n;
		i++;
	}
	InsertSort(idx,n);//對idx數組按no域值排序
	rewind(idxfile);
	for(i=0;i<n;i++)
		fwrite(&idx[i],sizeof(Index),1,idxfile);
	fclose(mfile);
	fclose(idxfile);
}


void InputMainFile()//添加一個主檔案記錄
{
	FILE *mfile;
	Student st;
	mfile=fopen("main.dat","ab+");
	if(mfile==NULL)
	{
		printf(">>不能建立主檔案\n");
		return;
	}
	printf(">>學号,姓名,性别,年齡,位址,系别,專業:");
	scanf("%s%s%d%d%s%s%s",st.num,st.name,&st.sex,&st.age,st.addr,st.dep,st.spec);
	if(fwrite(&st,sizeof(Student),1,mfile)!=1)
	{
		printf(">> 寫主檔案錯誤\n");
		return;
	}
	fclose(mfile);
}

void OutputMainFile()//輸出主檔案全部記錄
{
	FILE *mfile;
	Student st;
	int i=0;
	mfile=fopen("main.dat","rb");
	if(mfile==NULL)
	{
		printf(">>不能讀主檔案\n");
		return;
	}
	while((fread(&st,sizeof(Student),1,mfile))!=NULL)
	{
		printf(">> 記錄号%d:",++i);
		printf("%s%s%d%d%s%s%s\n",st.num,st.name,st.sex,st.age,st.addr,st.dep,st.spec);
	}
	fclose(mfile);
}

void OutputIdxFile()//輸出索引檔案全部記錄
{
	FILE *idxfile;
	Index irec;
	int i=0;
	idxfile=fopen("index.dat","rb");
	if(idxfile==NULL)
	{
		printf(">>不能讀索引檔案\n");
		return;
	}
	while((fread(&irec,sizeof(Index),1,idxfile))!=NULL)
		printf(">>(學号:記錄号)%s:  %d\n",irec.num,irec.offset);
	fclose(idxfile);
}

void ReadIndexFile(Index idx[MaxRec],int &len)//讀索引檔案資料存入idx數組中
{
	FILE *idxfile;
	int j;
	if((idxfile=fopen("index.dat","rb"))==NULL)
	{
		printf(">>索引檔案不能打開\n");
		return;
	}
	fseek(idxfile,0,2);
	j=ftell(idxfile);
	len=j/sizeof(Index);
	fread(idx,sizeof(Index),len,idxfile);
	fclose(idxfile);
}

int SearchNum(Index idx[],int len,char no[])//在索引檔案中查找no對應的記錄号
{
	int mid,low ,high,comp;
	low=0;high=len-1;
	while(low<=high)
	{
		mid=(low+high)/2;
		comp=strcmp(idx[mid].num,no);
		if(comp>0)
			high=mid-1;
		else if(comp<0)
			low=mid+1;
		else 
			return idx[mid].offset;
	}
	return -1;
}

void FindStudent()
{
	FILE *mfile;
	char no[8];
	Index idx[MaxRec];
	Student st;
	int i,len;
	mfile=fopen("main.dat","rb+");
	if(mfile==NULL)
	{
		printf(">>主檔案中沒有任何記錄\n");
		return;
	}
	ReadIndexFile(idx,len);//讀索引數組idx
	printf("輸入學号:");
	scanf("%s",no);
	i=SearchNum(idx,len,no);//在idx中查找
	if(i==-1)
		printf(">>學号 %s 不存在\n",no);
	else
	{
		fseek(mfile,(i-1)*sizeof(Student),SEEK_SET);//由序号直接跳到主檔案的這個記錄
		fread(&st,sizeof(Student),1,mfile);
		printf(">>%s%s%d%d%s%s%s\n",st.num,st.name,st.sex,st.age,st.age,st.dep,st.spec);
	}
	fclose(mfile);
}

int main()
{
	int sel;
	do
	{
		printf("1:輸入 2:輸入主檔案 3:輸出索引檔案 4:按學号查找 9:全清  0:退出:");
        scanf("%d",&sel);
		switch(sel)
		{
			case 9:
				DelAll();
			case 1:
				InputMainFile();
				CreatIdxFile();
				break;
			case 2:
				OutputMainFile();
				break;
			case 3:
				OutputIdxFile();
				break;
			case 4:
				FindStudent();
				break;
		}
	}while(sel!=0);
	return 0;
}

           

資料結構源碼筆記(C語言描述)彙總:

資料結構源碼筆記(C語言):英文單詞按字典序排序的基數排序

資料結構源碼筆記(C語言):直接插入排序

資料結構源碼筆記(C語言):直接選擇排序

資料結構源碼筆記(C語言):置換-選擇算法

資料結構源碼筆記(C語言):Huffman樹字元編碼

資料結構源碼筆記(C語言):Josephus問題之順序表

資料結構源碼筆記(C語言):Josephus問題之循環連結表

資料結構源碼筆記(C語言):多項式合并

資料結構源碼筆記(C語言):二叉樹之葉子結點旋轉銷毀

資料結構源碼筆記(C語言):哈夫曼樹

資料結構源碼筆記(C語言):集合的位向量表示

資料結構源碼筆記(C語言):連結隊列

資料結構源碼筆記(C語言):連結棧

資料結構源碼筆記(C語言):線性表的單連結清單示

資料結構源碼筆記(C語言):線性表的順序表示

資料結構源碼筆記(C語言):棧的基本操作

資料結構源碼筆記(C語言):中綴表達式

資料結構源碼筆記(C語言):希爾插入排序

資料結構源碼筆記(C語言):索引檔案建立和查找

資料結構源碼筆記(C語言):冒泡排序

資料結構源碼筆記(C語言):快速排序

資料結構源碼筆記(C語言):可變長度字元串的快速排序

資料結構源碼筆記(C語言):基數排序

資料結構源碼筆記(C語言):二路歸并排序

資料結構源碼筆記(C語言):堆排序

資料結構源碼筆記(C語言):二叉樹搜尋樹Kruskal

資料結構源碼筆記(C語言):二叉搜尋樹Prim

資料結構源碼筆記(C語言):最短路徑弗洛伊德算法

資料結構源碼筆記(C語言):深度、廣度優先生成樹

資料結構源碼筆記(C語言):鄰接矩陣轉化鄰接表

資料結構源碼筆記(C語言):統計字元串中出現的字元及其次數

資料結構源碼筆記(C語言):順序查找

資料結構源碼筆記(C語言):哈希表的相關運算算法

資料結構源碼筆記(C語言):分塊法查找

資料結構源碼筆記(C語言):二分查找

資料結構源碼筆記(C語言):二叉樹周遊

資料結構源碼筆記(C語言):二叉平衡樹的相關操作算法

資料結構源碼筆記(C語言):二叉排序樹的基本操作算法

資料結構源碼筆記(C語言):B樹的相關運算算法

繼續閱讀