源代码知识讲解.docx

上传人:b****2 文档编号:3097600 上传时间:2023-05-05 格式:DOCX 页数:11 大小:16.25KB
下载 相关 举报
源代码知识讲解.docx_第1页
第1页 / 共11页
源代码知识讲解.docx_第2页
第2页 / 共11页
源代码知识讲解.docx_第3页
第3页 / 共11页
源代码知识讲解.docx_第4页
第4页 / 共11页
源代码知识讲解.docx_第5页
第5页 / 共11页
源代码知识讲解.docx_第6页
第6页 / 共11页
源代码知识讲解.docx_第7页
第7页 / 共11页
源代码知识讲解.docx_第8页
第8页 / 共11页
源代码知识讲解.docx_第9页
第9页 / 共11页
源代码知识讲解.docx_第10页
第10页 / 共11页
源代码知识讲解.docx_第11页
第11页 / 共11页
亲,该文档总共11页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

源代码知识讲解.docx

《源代码知识讲解.docx》由会员分享,可在线阅读,更多相关《源代码知识讲解.docx(11页珍藏版)》请在冰点文库上搜索。

源代码知识讲解.docx

源代码知识讲解

#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;

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2