/*文件名:student.cpp*/
#include<stdio.h>
#include<string.h>
#include<malloc.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("index.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);
rewind(idxfile); /*对idx数组按no域值排序*/
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); /*j求出文件长度*/
rewind(idxfile);
len=j/sizeof(Index); /*len求出文件的记录个数*/
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 /*comp=0的情况*/
return idx[mid].offset;
}
return -1;
}
void FindStudent()
{
FILE *mfile;
char no[10];
Index idx[MaxRec];
Student st;
int i,len;
mfile=fopen("main.dat","rb+");
if(mfile==NULL)
{ printf(">>主文件中没有任何记录\n");
return;
}
ReadIndexFile(idx,len); /*读取索引数组*/
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.addr,st.dep,st.spec);
}
fclose(mfile);
}
void main()
{
int sel;
do
{ printf("1:输入 2:输出文件 3:输出索引文件 4:按学号查找 9:全清 0:退出:");
scanf("%d",&sel);
switch(sel)
{
case 9:
DelAll();
break;
case 1:
InputMainFile();
CreatIdxFile(); /*同步修改索引文件*/
break;
case 2:
OutputMainFile();
break;
case 3:
OutputIdxFile();
break;
case 4:
FindStudent();
break;
}
}while(sel!=0);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
student_info.7z (17个子文件)
student_info
student_info.plg 0B
student_info.dsp 4KB
student.cpp 5KB
index.dat 12B
student_info.ncb 25KB
课程设计报告.doc 141KB
student_info.dsw 549B
main.dat 108B
Debug
student_info.ilk 192KB
student.obj 16KB
index.dat 36B
student_info.exe 172KB
student_info.pdb 449KB
vc60.idb 41KB
main.dat 324B
vc60.pdb 52KB
student_info.pch 217KB
共 17 条
- 1
资源评论
- fjqlldg2020-03-05很实用,在此感谢啦!!☆⌒(*^-゜)v
tzzyw
- 粉丝: 3
- 资源: 41
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功