C语言课程设计简单的行编辑器文档格式.docx
- 文档编号:20166509
- 上传时间:2023-01-17
- 格式:DOCX
- 页数:22
- 大小:258.30KB
C语言课程设计简单的行编辑器文档格式.docx
《C语言课程设计简单的行编辑器文档格式.docx》由会员分享,可在线阅读,更多相关《C语言课程设计简单的行编辑器文档格式.docx(22页珍藏版)》请在冰豆网上搜索。
/*********************************主函数**************************************/
main()
{FILE*pfile;
pfile=fopen("
Linedit.txt"
"
a+"
);
readfile(lines,pfile);
fclose(pfile);
menu(lines);
}
intreadfile(char(*lines)[MAXC],FILE*pfile){
inti=0;
while(!
feof(pfile)){
fgets(lines[i],MAXC,pfile);
/*if(!
strstr(lines[i],"
\n"
))return1;
*/
i++;
return0;
2.主菜单函数
voidmenu(char(*lines)[MAXC]){
intselection;
do{
system("
cls"
puts("
\t\t***********************MUNU******************\n\n"
\t\t1.Editnewline"
\t\t2.Replaceline"
\t\t3.Deleteline"
\t\t4.Searchline"
\t\t5.Modifydata"
\t\t6.Save"
\t\t0.Exit"
\n\n\t\t********************************************\n"
printf("
Pleaseselectanumber:
[]\b\b"
scanf("
%d"
&
selection);
if(selection<
0||selection>
6){
Invalidselection!
Pleasetryagain"
andselectanumber:
[]\b\b\n"
elsebreak;
}while(true);
switch(selection)
{
case1:
edit(lines);
break;
case5:
modify(lines);
case3:
mydelete(lines);
case4:
search(lines);
case2:
replace(lines);
case6:
savetofile(lines);
case0:
myexit(lines);
}while(true);
3.各功能模块设计
(1)编辑模块
/*****************************编辑*************************************/
voidedit(char(*lines)[MAXC])
inti,index;
/*system("
clrscr();
for(i=0,index=0;
i<
MAXL;
i++)
if(lines[i][0]!
=0)
{
index++;
printf("
%d:
%s"
index,lines[i]);
}
\n\nPleasetypeanewline:
fflush(stdin);
for(i=0;
{
if(lines[i][0]=='
\0'
)
{/*每行第一个字符作为标志位空行可写入*/
fgets(lines[i],MAXC,stdin);
return;
else
continue;
(2)替换模块
/******************************替换****************************************/
voidreplace(char(*lines)[MAXC])
inti;
intindline;
intj,k;
do
*/
/*打印所有*/
i+1,lines[i]);
\n\nWhichlinedoyouprefertoreplace?
(Toquitpleaseinput0)[]\b\b"
scanf("
indline);
if(indline==0)
else
for(j=0,k=0;
j<
j++)
if(lines[j][0]!
=0)k++;
if(k==indline)
\n\nThelinetobereplacedis:
\n%s"
lines[j]);
Pleasetypeyournewline:
memset(lines[j],0,MAXC);
fgets(lines[j],MAXC,stdin);
break;
}
}while(true);
(3)删除模块
删除即在查找到某行的基础上再删掉,其具体流程图可参照查找的流程图
/*******************************删除***************************************/
voidmydelete(char(*lines)[MAXC])
\n\nWhichlinedoyouprefertodelete?
if(indline==0)return;
Thelinetobedeletedis:
\n%s\n"
system("
pause"
(4)查找模块
/**************************查找****************************************/
voidsearch(char(*lines)[MAXC])
\n\nWhichlinedoyouwanttosearch?
Thelinetobesearchedis:
(5)修改模块
删除模块的流程图也是在查找模块的基础上稍加改动,在此不再赘述
其流程图可简单写为
/******************************修改**********************************/
/*在sSrc中用sReplaceStr替换sMatchStr*/
intmodifystr(char*sSrc,char*sMatchStr,char*sReplaceStr)
intStringLen;
charcaNewString[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);
return0;
voidmodify(char(*lines)[MAXC])
inti,indline;
charbuf[MAXC];
intk;
intj;
char*token=0;
charsearch[MAXC],replace[MAXC];
do
='
)
\n\nWhichlinedoyouprefertomodify?
\n\nInputthesearchstringandthereplace"
(Toquitpleaseinputq):
"
memset(buf,0,MAXC);
fgets(buf,MAXC,stdin);
k=strlen(buf)-1;
if(buf[k]=='
\n'
)buf[k]='
;
if(*buf=='
q'
&
*(buf+1)=='
)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);
(6)保存模块
voidsavetofile(char(*lines)[MAXC])
FILE*pfile;
w+"
fputs(lines[i],pfile);
\nThelineshasbeensaved.\n"
(7)退出模块
voidmyexit(char(*lines)[MAXC])
charc;
Savethelinestothefile?
(y/n)"
c=getchar();
if(c=='
n'
)exit
(1);
savetofile(lines);
exit
(1);
五.上机操作
1.主菜单函数
2.编辑模块
3.替换模块
4查找模块.
5.修改模块
6.删除模块
7.保存模块
8.退出模块
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C语言课程设计 简单的行编辑器 语言 课程设计 简单 编辑器
![提示](https://static.bdocx.com/images/bang_tan.gif)