C语言课程设计 简单的行编辑器
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
语言程序设计报告
姓名:
班级:
学号:
指导教师:
课题:简单的行编辑器
简单的行编辑器
一.题目要求
1.设置一个简单的行编辑器,每行以回车结束。
2.数据以文件形式存储。
3.编辑器具有查找,替代,修改数据的功能。
二.需求分析
根据题目要求,数据以文件的形式存储,所以应提供文件的输入,输出等操作;还需要文件具有查找,替换,修改数据的功能。
三.总体设计
根据上面的需求分析,可以将这个系统的设计分为如下七大模块:
编辑,查找,修改,,删除,替换,保存,退出。
四.详细设计
1.主函数
为了各模块的独立性,主函数一般设计得比较简单,本系统主要用主函数打开上次保存的数据和进入主菜单函数
流程图
程序
main()
pfile=fopen("Linedit.txt","a+"); readfile(lines,pfile); fclose(pfile); menu(lines); }
int readfile(char (*lines)[MAXC],FILE *pfile) { int i=0;
while(!feof(pfile)){
fgets(lines[i],MAXC,pfile);
/*if(!strstr(lines[i],"\n")) return 1;*/ i++; }
return 0; }
2. 主菜单函数 流程图
程序
void menu(char (*lines)[MAXC]){ int selection; do{
system("cls");
puts("\t\t***********************MUNU******************\n\n"); puts("\t\t1.Edit new line");
puts("\t\t2.Replace line");
puts("\t\t3.Delete line");
puts("\t\t4.Search line");
puts("\t\t5.Modify data");
puts("\t\t6.Save");
puts("\t\t0.Exit");
puts("\n\n\t\t********************************************\n");
printf("Please select a number:[ ]\b\b");
do{
scanf("%d",&selection);
if(selection<0||selection>6) {
printf("Invalid selection!Please try again ");
printf("and select a number:[]\b\b\n");
}
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)编辑模块
流程图
程序
/*****************************编辑*************************************/ void edit(char (*lines)[MAXC])
{
int i,index;
/*system("cls");*/
clrscr();
for(i=0,index=0;i<MAXL;i++)
if(lines[i][0]!=0)
{
index++;
printf("%d: %s",index,lines[i]);
}
printf("\n\nPlease type a new line:\n");
fflush(stdin);
for(i=0;i<MAXL;i++)
{
if(lines[i][0]=='\0')
{ /*每行第一个字符作为标志位空行可写入*/
fgets(lines[i],MAXC,stdin);
return;
}
else
continue;
}
}
(2)替换模块
流程图
程序
/******************************替换****************************************/ void replace(char (*lines)[MAXC])
{
int i;
int indline;
int j,k;
do
{
/*system("cls"); */
clrscr();
/*打印所有*/
for(i=0;i<MAXL;i++)
if(lines[i][0]!=0)
printf("%d: %s",i+1,lines[i]);
printf("\n\nWhich line do you prefer to replace?(To quit please input 0)[ ]\b\b");
scanf("%d",&indline);
if(indline==0)
return;
else
{
for(j=0,k=0;j<MAXL;j++)
{
if(lines[j][0]!=0) k++;
if(k==indline)
{
printf("\n\nThe line to be replaced is: \n%s",lines[j]);
printf("Please type your new line:\n");
memset(lines[j],0,MAXC);
fflush(stdin);
fgets(lines[j], MAXC, stdin);
break;
}
}
}
}while(true);
}
(3)删除模块
流程图
删除即在查找到某行的基础上再删掉,其具体流程图可参照查找的流程图
程序
/*******************************删除***************************************/ void mydelete(char (*lines)[MAXC])
{
int i,index;
int indline;
int j,k;
do
{
/*system("cls");*/
clrscr();
for(i=0,index=0;i<MAXL;i++)
if(lines[i][0]!=0)
{
index++;
printf("%d: %s",index,lines[i]);
}
printf("\n\nWhich line do you prefer to delete?(To quit please input 0)[ ]\b\b");
scanf("%d",&indline);
if(indline==0)return;
else
for(j=0,k=0;j<MAXL;j++)
{
if(lines[j][0]!=0) k++;
if(k==indline)
{
printf("The line to be deleted is: \n%s\n",lines[j]);
memset(lines[j],0,MAXC);
system("pause");
break;
}
}
}while(true);
}
(4)查找模块
流程图
程序
/**************************查找****************************************/ void search(char (*lines)[MAXC])
{
int i,index;
int indline;
int j,k;
do
{
/*system("cls");*/
clrscr();
for(i=0,index=0;i<MAXL;i++)
if(lines[i][0]!=0)
{
index++;
printf("%d: %s",index,lines[i]);
}
printf("\n\nWhich line do you want to search?(To quit please input 0)[ ]\b\b");
scanf("%d",&indline);
if(indline==0)return;
else
for(j=0,k=0;j<MAXL;j++)
{
if(lines[j][0]!=0) k++;
if(k==indline)
{
printf("The line to be searched is: \n%s\n",lines[j]);
system("pause");
break;
}
}
}while(true);
}
(5)修改模块
流程图
删除模块的流程图也是在查找模块的基础上稍加改动,在此不再赘述
其流程图可简单写为
程序
/******************************修改**********************************/ /*在sSrc中用sReplaceStr替换sMatchStr */
int modifystr(char *sSrc,char *sMatchStr,char *sReplaceStr)
{
int StringLen;
char caNewString[MAXC];
char *FindPos = strstr(sSrc,sMatchStr);
if((!FindPos)||(!sMatchStr))
return -1;
while(FindPos)
{
memset(caNewString,0,sizeof(caNewString));
StringLen=FindPos-sSrc;
strncpy(caNewString,sSrc,StringLen);
strcat(caNewString,sReplaceStr);
strcat(caNewString,FindPos+strlen(sMatchStr));
strcpy(sSrc,caNewString);
FindPos = strstr(sSrc,sMatchStr);
}
return 0;
}
void modify(char (*lines)[MAXC])
{
int i,indline;
char buf[MAXC];
int k;
int j;
char* token=0;
char search[MAXC],replace[MAXC];
do
{
/*system("cls"); */
clrscr();
for(i=0;i<MAXL;i++)
if(lines[i][0]!='\0')
printf("%d: %s",i+1,lines[i]);
printf("\n\nWhich line do you prefer to modify?(To quit please input 0)[ ]\b\b");
scanf("%d",&indline);
if(indline==0)
return;
else
{
for(j=0,k=0;j<MAXL;j++)
{
if(lines[j][0]!=0) k++;
if(k==indline)
{
printf("\n\nThe line to be replaced is: \n%s",lines[j]);
printf("\n\nInput the search string and the replace ");printf("(To quit please input q):");
memset(buf,0,MAXC);
fflush(stdin);
fgets(buf, MAXC, stdin);
k= strlen(buf)-1;
if(buf[k]=='\n') buf[k]='\0';
if(*buf=='q'&&*(buf+1)=='\0') return;
memset(search,0,MAXC);
memset(replace,0,MAXC);
token =strtok(buf, " ");
strcpy(search,token);
token =strtok(NULL, " ");
strcpy(replace,token);
modifystr(lines[j],search,replace);
break;
}
}
}
}while(true);
}
(6)保存模块
流程图
程序
void savetofile(char (*lines)[MAXC])
{
FILE *pfile;
int i;
pfile=fopen("Linedit.txt","w+");
for(i=0;i<MAXL;i++)
if(lines[i][0]!='\0')
fputs(lines[i],pfile);
fclose(pfile);
printf("\nThe lines has been saved.\n");
system("pause");
}
(7)退出模块
流程图
程序
void myexit(char (*lines)[MAXC])
{
char c;
printf("Save the lines to the file?(y/n)");
fflush(stdin);
c=getchar();
if(c=='n') exit(1);
savetofile(lines);
exit(1);
}
五.上机操作
1.主菜单函数
2.编辑模块
3.替换模块
4查找模块.
5.修改模块
6.删除模块
7.保存模块
8.退出模块。