資源簡介
綜合索引文件和查找算法做的學生信息管理程序 用C語言寫的 有源代碼 課程設計報告和可執行的程序
代碼片段和文件信息
/*文件名:student.cpp*/
#include
#include
#include
#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(“index.dat““wb“))==NULL)
{ printf(“>>不能建立主文件\n“);
return;
}
fclose(mfile);
fclose(idxfile);
}
void?InsertSort(Index?R[]int?n) /*對R[0..n-1]按遞增有序進行直接插入排序*/
{
int?ij;
Index?temp;
for(i=1;i { temp=R[i];
j=i-1;
while(j>=0&&strcmp(temp.numR[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=0i;
if((mfile=fopen(“main.dat““rb“))==NULL)
{ printf(“>>不能打開主文件\n“);
return;
}
if((idxfile=fopen(“index.dat““wb“))==NULL)
{ printf(“>>不能建立索引文件\n“);
return;
}
i=0;
while((fread(&stsizeof(Student)1mfile))!=NULL)
{ strcpy(idx[i].numst.num);
idx[i].offset=++n;
i++;
}
InsertSort(idxn);
rewind(idxfile); /*對idx數組按no域值排序*/
for(i=0;i fwrite(&idx[i]sizeof(Index)1idxfile);
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.numst.name&st.sex&st.agest.addrst.depst.spec);
if(fwrite(&stsizeof(Student)1mfile)!=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(&stsizeof(Student)1mfile))!=NULL)
{ printf(“>>記錄號%d:“++i);
printf(“%s??%s?%d?%d?%s?%s?%s\n“st.numst.namest.sexst.agest.addrst.depst.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(&irecsizeof(Index)1idxfile))!=NULL)
printf(“>>(學號:記錄號)?%s:?%d\n“irec.numirec.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(idxfile02);
j=ftell(idxfile); /*j求出文件長度*/
rewind(idxfile);
len=j/sizeof(Index); /*len求出文件的記錄個數*/
fread(idxsizeof(Index)lenidxfile);
fclose(idxfile);
}
- 上一篇:MFC視頻播放器(帶播放列表VC++6.0源代碼)
- 下一篇:c++線程安全日志類
評論
共有 條評論