源代码复习过程.docx
- 文档编号:9372072
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:11
- 大小:16.43KB
源代码复习过程.docx
《源代码复习过程.docx》由会员分享,可在线阅读,更多相关《源代码复习过程.docx(11页珍藏版)》请在冰豆网上搜索。
源代码复习过程
源代码
#include
#include
#include
typedefstructpnode//结构体定义用于存放学生信息的节点
{
charname[8];//姓名
charxh[16];//学号
charfh[30];//房号
}personnode;
charfilename[50];//文件名
FILE*fp;//指向文件的指针
voidcreat()//创建新数据文件的函数创建一个二进制文件用于存放学生数据
{
personnode*person;
person=(personnode*)malloc(sizeof(personnode));//为节点分配内存
printf("\n请输入文件名:
\n");
scanf("%s",filename);
if((fp=fopen(filename,"w+"))==NULL)
{
printf("\n你没有输入文件名,不能找到文件夹");
exit(0);
}
printf("\n请输入姓名,学号(相同的长度)和房号,以空格隔开,以#结束\n");
scanf("%s",person->name);
while(strcmp(person->name,"#"))//该循环用于控制学生信息的录入当输入#时学生信息录入完毕
{
scanf("%s%s",person->xh,person->fh);
fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
scanf("%s",person->name);
}
fclose(fp);
}
voidreadfile()//文件读取函数用于打开已有的二进制数据文件
{printf("\npleaseenterthefileroad:
\n");
scanf("%s",filename);//此处输入为文件的路径如c:
\zhang
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\ncan'topenthefile:
\n");
exit(0);
}
fclose(fp);
}
voidoutput()//输出函数用于输出文件的全部信息
{
personnode*person;
longoffset1,offset2;
charname1[8],name2[8],name3[8];
charxh1[16],xh2[16],xh3[16];
charfh1[30],fh2[30],fh3[30];
person=(personnode*)malloc(sizeof(personnode));
if((fp=fopen(filename,"r"))==NULL)
{
printf("\ncan'topenthefile");
exit(0);
}
while(!
feof(fp))//此循环用于对文件数据中关键字学号进行从小到大冒泡排序
{
inta,b,c,d;//a为循环结束判定变量b,c,d用于起泡排序时学号交换的替换
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\ncan'topenthefile");
exit(0);
}
while(!
feof(fp))//从文件头开始遍历
{
while(!
feof(fp))//对两组数据进行排序
{
offset1=ftell(fp);//获取文件内部当前指针位置
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
strcpy(name1,person->name);
strcpy(xh1,person->xh);
strcpy(fh1,person->fh);
if(feof(fp))break;//文件结束跳出循环
offset2=ftell(fp);//获取文件内部下一指针位置
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
strcpy(name2,person->name);
strcpy(xh2,person->xh);
strcpy(fh2,person->fh);
b=strlen(xh1);
c=strlen(xh2);
d=strcmp(xh1,xh2);
if(b==c&&d>0)//如果学号1大于等于学号2则交换全部数据
{
strcpy(name3,name1);
strcpy(name1,name2);
strcpy(name2,name3);
strcpy(xh3,xh1);
strcpy(xh1,xh2);
strcpy(xh2,xh3);
strcpy(fh3,fh1);
strcpy(fh1,fh2);
strcpy(fh2,fh3);
fseek(fp,offset1,SEEK_SET);//将文件指针移动offset1个字节
strcpy(person->name,name1);
strcpy(person->xh,xh1);
strcpy(person->fh,fh1);
fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->xh,person->fh);//排序后写入文件
strcpy(person->name,name2);
strcpy(person->xh,xh2);
strcpy(person->fh,fh2);
fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->xh,person->fh);//排序后写入文件
}//if
fseek(fp,offset2,SEEK_SET);///将文件位置指针从文件头向前移动offset2个字节
}//while对两组数据进行排序
rewind(fp);//指向头文件
while(!
feof(fp))//此循环用于判断学号是否有序
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
strcpy(xh1,person->xh);
if(feof(fp)){a=1;break;}
offset2=ftell(fp);
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
strcpy(xh2,person->xh);
b=strlen(xh1);
c=strlen(xh2);
d=strcmp(xh1,xh2);
if(b==c&&d<=0)a=1;//若a为1则有序
else
{a=0;//a为0则无序
break;
}
fseek(fp,offset2,SEEK_SET);
}//while判断学号是否有序
if(a)break;//a为1说明学号已经有序跳出循环
rewind(fp);
}//while从文件头开始遍历
if(a)break;////若a为1,则学号已有序排列,跳出总循环,输出文件
}
rewind(fp);
printf("\n******************************************************************\n");
printf("%35s\n","thefile(thefilehavaprintfasstudentnumber)");
printf("%-10s%-20s%-50s\n","name","studentnumber","roomnumber");
while(!
feof(fp))//次循环用于输出文件
{
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
printf("%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
}
fclose(fp);
printf("*******************************************************************\n\n");
}
voidsearch1()//按姓名搜索函数
{
intk=0;
charnamekey[8];
personnode*person;
person=(personnode*)malloc(sizeof(personnode));
printf("\npleaseenterthenameheadyouneed:
");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\ncan'topenthefile");
exit(0);
}
while(!
feof(fp))
{
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
if(!
strcmp(namekey,person->name))//比较是否相同相同则输出结果否则输出未找到记录
{
printf("\n\nhaveserched,thecourrentis:
");
printf("%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
k=1;
}
}
if(!
k)printf("\n\nsorry,thereisnotthecurrentofthisspeople\n");
fclose(fp);
}
voidsearch2()//按学号搜索函数
{
intk=0;
charxhkey[16];
personnode*person;
person=(personnode*)malloc(sizeof(personnode));
printf("\npleaseentertheroomnumberyouwanttoserch:
");
scanf("%s",xhkey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\ncan'topenthefile");
exit(0);
}
while(!
feof(fp))
{
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
if(!
strcmp(xhkey,person->xh))////比较是否相同相同则输出结果否则输出未找到记录
{
printf("\n\nhavegotit,thecurrentis:
");
printf("%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
k=1;
}
}
if(!
k)printf("\n\nsorry,thereisnotthecurrentofthisspeople\n");
fclose(fp);
}
voidsearch3()//按房号搜索函数
{
intk=0;
charfhkey[30];
personnode*person;
person=(personnode*)malloc(sizeof(personnode));
printf("\npleaseentertheroomnumberyouwanttoserch:
");
scanf("%s",fhkey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\ncan'topenthefile");
exit(0);
}
while(!
feof(fp))
{
fscanf(fp,"%s%s%s\n",person->name,person->xh,person->fh);
if(!
strcmp(fhkey,person->fh))//比较是否相同相同则输出结果否则输出未找到记录
{
printf("\n\nhavegotit,thecurrentis:
");
printf("%-10s%-20s%-50s\n",person->name,person->xh,person->fh);
k=1;
}
}
if(!
k)printf("\n\nsorry,thereisnotthecurrentofthisspeople\n");
fclose(fp);
}
voidmain()//主函数
{
intm,flag=1;//m用于控制菜单的选择项flag用于控制菜单弹出
while(flag)
{
printf("%33s\n","thefile");
printf("*****************************************************************\n");
printf("\t\t0-------------创建一个新的学生数据文件\n");
printf("\t\t1-------------读取一个已经建立的文件\n");
printf("\t\t2------------按名字进行查询\n");
printf("\t\t3------------按学号进行查询\n");
printf("\t\t4------------按宿舍号进行查询\n");
printf("\t\t5-------------输出按学号进行排序的文件\n");
printf("\t\t6-------------退出\n");
printf("*****************************************************************\n");
printf("\t请选择(0-----------6)\n");
scanf("%d",&m);
switch(m)
{case0:
creat();break;
case1:
readfile();break;
case2:
search1();break;
case3:
search2();break;
case4:
search3();break;
case5:
output();break;
case6:
exit(0);break;
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 源代码 复习 过程