1、C语言课程设计简单的行编辑器语言程序设计报告姓名:班级:学号:指导教师:课题:简单的行编辑器简单的行编辑器题目要求1.设置一个简单的行编辑器,每行以回车结束。2.数据以文件形式存储。3.编辑器具有查找,替代,修改数据的功能。根据题目要求,数据以文件的形式存储, 所以应提供文件的输入, 输出等操作;还需要文件具有查找,替换,修改数据的功能。根据上面的需求分析,可以将这个系统的设计分为如下七大模块: 编辑,查找,修改,删除,替换,保存,退出。四.详细设计1.主函数为了各模块的独立性,主函数一般设计得比较简单,本系统主要用主函数打开上次保存的数 据和进入主菜单函数流程图打开上次保存文件程序/*mai
2、 n()*z主函数 * 进入主菜单函数FILE *pfiiepfile=fope n( L in edit.txt,a+); readfile(l in es,pfile);fclose(pfile);menu (I in es);int readfile(char (*lines)MAXC,FILE *pfile) int i=0;while(!feof(pfile)fgets(li nesi,MAXC,pfile);/*if(!strstr(li nesi,n) return 1;*/i+; return 0;2.主菜单函数流程图程序void men u(char (*li nes)MAX
3、C) int select ion;dosystem(cls);puts(tt*MUNU* puts(tt1.Edit new line);puts(tt2.Replace line);puts(tt3.Delete line); puts(tt4.Search line);puts(tt5.Modify data);puts(tt6.Save);puts(tt0.Exit);printf(Please select a number: bb); doscanf(%d,&selection); if(selection6) printf(Invalid selection!Please try
4、 again ); printf(and select a number:bbn);else break;while(true);switch(selection)case 1:edit(lines);break;case 5:modify(lines);break;case 3:mydelete(lines);break;case 4:search(lines);break;case 2:replace(lines);break; case 6:savetofile(lines);break;case 0:myexit(lines); while(true);3 各功能模块设计(1) 编辑模
5、块 流程图mt 1=0,0181=0清屏 clisciQ;iMAXLhnesg(U!=O经 7二.八iidBx+,pint旷兀d:pmtfij ynlnPlease type a new luteffliEhtstdin),i=0;iMAXL一-_ 晦MT: /辱 ts(liiue si) ,MAXCtdiicantiKiE,retEm;i+;程序/*编辑 *void edit(char (*li nes)MAXC)int i,i ndex;/*system(cls);*/clrscr();for(i=0,i ndex=O;iMAXL;i+) if(li nesi0!=0)in dex+;pr
6、intf(%d: %s,index,linesi);prin tf(nn Please type a new lin e:n); fflush(stdi n);for(i=0;iMAXL;i+)if(li nesiO=O)空行可写入*/ /*每行第一个字符作为标志位fgets(li nesi,MAXC,stdi n);return;elsecon ti nue;(2) 替换模块流程图niti; mtindEne,血tj,k,II1clisci();i=0;jMAXL:k-H-: | _ k-=indliiw /输由騎选定行pmtf Flease tj/pe youf new line :ln)
7、memseXlinesljl.OXC),fflush( stdin);堆tsOiite即,MAXQ stdii;breek;程序void replace(char (*li nes)MAXC)int i;int indline;int j,k;do/*system(cls); */clrscr();/* 打印所有 */for(i=0;iMAXL;i+)if(linesi0!=0)printf(%d: %s,i+1,linesi);printf(nnWhich line do you prefer to replace?(To quit please input 0) bb);scanf(%d,
8、&indline);if(indline=0)return;elsefor(j=0,k=0;jMAXL;j+)if(linesj0!=0) k+;if(k=indline)printf(nnThe line to be replaced is: n%s,linesj); printf(Please type your new line:n); memset(linesj,0,MAXC);fflush(stdin);fgets(linesj, MAXC, stdin);break;while(true);(3) 删除模块 流程图 删除即在查找到某行的基础上再删掉,其具体流程图可参照查找的流程图查
9、找行删除行do/*system(cls);*/clrscr();for(i=0,i ndex=O;iMAXL;i+)if(li nesi0!=0)in dex+;printf(%d: %s,index,linesi);prin tf(nnWhich line do you prefer to delete?(To quit please in put 0) bb);scan f(%d,&in dli ne);if(i ndli ne=O)return;else for(j=0,k=0;jMAXL;j+)if(linesj0!=0) k+;if(k=in dli ne)printf(The li
10、ne to be deleted is: n%sn,linesj); memset(li nesj,O,MAXC);system(pause);break;while(true);(4) 查找模块流程图查找 */void search(char (*lin es)MAXC)int i,index; int indline; int j,k;do/*system(cls);*/ clrscr();for(i=0,index=0;iMAXL;i+) if(linesi0!=0)index+;printf(%d: %s,index,linesi);printf(nnWhich line do you
11、 want to search?(To quit please input 0) bb);scanf(%d,&indline); if(indline=0)return; elsefor(j=0,k=0;jMAXL;j+)if(linesj0!=0) k+; if(k=indline)printf(The line to be searched is: n%sn,linesj); system(pause);break;while(true);(5)修改模块流程图 删除模块的流程图也是在查找模块的基础上稍加改动,在此不再赘述 其流程图可简单写为查找行修改行程序/* 修尖改 */* 在 sSrc
12、 中用 sReplaceStr 替换 sMatchStr */int modifystr(char *sSrc,char *sMatchStr,char *sReplaceStr)int Strin gLe n;char caNewStri ngMAXC;char *Fi ndPos = strstr(sSrc,sMatchStr);if(!Fi ndPos )| (!sMatchStr)return -1;while(Fi ndPos)memset(caNewStri ng,0,sizeof(caNewStri ng);Strin gLe n=Fin dPos-sSrc;strn cpy(c
13、aNewStri ng,sSrc,Stri ngLe n); strcat(caNewStri ng,sReplaceStr);strcat(caNewStri ng,Fi ndPos+strle n(sMatchStr); strcpy(sSrc,caNewStri ng);Fin dPos = strstr(sSrc,sMatchStr); return 0;void modify(char (*li nes)MAXC)int i,indline;char bufMAXC;int k;int j;char* token=0;char searchMAXC,replaceMAXC;do /*
14、system(cls); */ clrscr();for(i=0;iMAXL;i+) if(linesi0!=0) printf(%d: %s,i+1,linesi);printf(nnWhich line do you prefer to modify?(To quit please input 0) bb);scanf(%d,&indline); if(indline=0) return;else for(j=0,k=0;jMAXL;j+) if(linesj0!=0) k+; if(k=indline) printf(nnThe line to be replaced is: n%s,l
15、inesj);q):);printf(nnInput the search string and the replace );printf(To quit please inputmemset(buf,0,MAXC); fflush(stdin);fgets(buf, MAXC, stdin); k= strlen(buf)-1;if(bufk=n) bufk=0; if(*buf=q&*(buf+1)=0) return;memset(search,0,MAXC); memset(replace,0,MAXC);token =strtok(buf, ); strcpy(search,toke
16、n);token =strtok(NULL, ”); strcpy(replace,toke n);modifystr(l in esj,search,replace); break;while(true);(6)保存模块流程图FILE inti;i=0Xfputsflines i Tpfile),fclose(pfile)fpriiitf(The lilies liasbeensave tUti); 那协诚pa use);程序void savetofile(char (*lin es)MAXC)FILE *pfile;int i;pfile=fope n(Li nedit.txt,w+);f
17、or(i=0;iMAXL;i+)if(li nesiO!=O) fputs(li nesi,pfile);fclose(pfile);prin tf(nThe lines has bee n saved. n); system(pause);(7)退出模块流程图程序void myexit(char (*l in es)MAXC)char c;prin tf(Save the lines to the file?(y/n 门; fflush(stdi n);c=getchar();if(c=n) exit(1);savetofile(l in es);exit(1);五.上机操作1.主菜单函数邑
18、 C;DOCUME-1 ADMINI 1、梟 面TU E XE哄*JC JOCKNU 艮 X ;M 算 J*X;M 耳図 8t1.Edit new line2.Kepiace line Dftlete line 4.Searcli line5-Modif y data6.SausB.ExitPltae seLeet a nunber:f_2.编辑模块3 替换模块Mhiich line do you prefer to replace? CTa quit please input L 吓 C:DOCUME-1 ADMINI-1 桌面TCJE XEX1212Which line do prefer
19、 to* replace? CTo quit please input 11 JThe line to be replaced is: 1234567B9Please type your neu lime: 555556789.4查找模块.Which line do you prefer to* modji f y? To quit please liniput 2 JTlae line to be replaced is987654321Input the search string and the replace 12 i Ths lina to be daletsd is:Press *
20、ny keu to continue * ” *7 保存模块匡 C;DOCUMElADMINI-vl J 面TUFXEiMXlIHiKJfIgSOCXMEKHJtJitJMIXXX 胃 HXXNIUNII X 赳 NKXJMIKXX 宾?CiMXWiMNXX1.Edit new line2.6.SaveB.ExitReplace line 3delete line 4.Search line 5用odify data来:K X Mi K半耳壬 14 岳耳X Jt 胃 KIHt-ME MrSKHMX胃 梵汪水胃:HM:M KN来Please select 帚 number*(61Tlie lines Ttas been saved.Press 凰ny key to continus - &退出模块
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1