求c语言写的文本编辑器
答案:1 悬赏:80 手机版
解决时间 2021-05-08 23:27
- 提问者网友:相思似海深
- 2021-05-08 11:58
要求:每行以回车键结束,数据以文件形式储存,编辑器具有查找,替换,修改数据的功能 !!!注:源代码要加注释(函数功能、入口及出口参数说明、函数调用关系等)
最佳答案
- 五星知识达人网友:杯酒困英雄
- 2021-05-08 13:06
本题的一个完整的c程序如下,win-tc和Dev-c++下运行通过。
#include <stdio.h>
#define MAXLEN 80
#define MAXLINE 200
char buffer[MAXLEN],fname[120];
char *lineptr[MAXLINE];
FILE *fp;
void edit(),replace(),insert(),delete(),quit();
char comch[]="EeRrIiDdQq";
void(*comfun[])()={edit,replace,insert,delete,quit};
int modified=0,
last;
char *chpt;
main()
{
int j;
last=0;
while(1)
{
printf("\nInput a command:[e,r,i,d,q].\n");
gets(buffer);
for(chpt=buffer;*chpt==''||*chpt=='\t';chpt++);
if(*chpt=='\0') continue;
for(j=0;comch[j]!='\0'&&comch[j]!=*chpt;j++);
if(comch[j]=='\0') continue;
chpt++;
(*comfun[j/2])();
fprintf(stdout,"The text is:\n");
for(j=0;j<last;j++)
fputs(lineptr[j],stdout);
}
}
void quit()
{
int c;
if(modified)
{
printf("Save? (y/n)");
while(!(((c=getchar())>='a'&&c<='z')||(c>='A'&&c<='Z')));
if(c=='y'||c=='Y')
save(fname);
}
for(c=0;c<last;c++)
free(lineptr[c]);
exit(0);
}
void insert()
{
int k,m,i;
sscanf(chpt,"%d%d",&k,&m);
if(m<0||m>last||last+k>=MAXLINE)
{
printf("Error!\n");
return;
}
for(i=last;i>m;i--)
lineptr[i+k-1]=lineptr[i-1];
for(i=0;i<k;i++)
{
fgets(buffer,MAXLEN,stdin);
lineptr[m+i]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[m+i],buffer);
}
last+=k;
modified=1;
}
void delete()
{
int i,j,m,n;
sscanf(chpt,"%d%d",&m,&n);
if(m<=0||m>last||n<m)
{
printf("Error!\n");
return;
}
if(n>last)
n=last;
for(i=m;i<=n;i++)
free(lineptr[i-1]);
for(i=m,j=n+1;j<=last;i++,j++)
lineptr[i-1]=lineptr[j-1];
last=i-1;
modified=1;
}
void replace()
{
int k,m,n,i,j;
sscanf(chpt,"%d%d%d",&k,&m,&n);
if(m<=0||m>last||n<m||last-(n-m+1)+k>=MAXLINE)
{
printf("Error!\n");
return;
}
if(n>last)
n=last;
for(i=m;i<=n;i++)
free(lineptr[i-1]);
for(i=m,j=n+1;j<=last;i++,j++)
lineptr[i-1]=lineptr[j-1];
last=i-1;
for(i=last;i>=m;i--)
lineptr[i+k-1]=lineptr[i-1];
for(i=0;i<k;i++)
{
fgets(buffer,MAXLEN,stdin);
lineptr[m+i-1]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[m+i-1],buffer);
}
last+=k;
modified=1;
}
save(char *fname)
{
int i;
FILE *fp;
if((fp=fopen(fname,"w"))==NULL)
{
fprintf(stderr,"Can't open %s.\n",fname);
exit(1);
}
for(i=0;i<last;i++)
{
fputs(lineptr[i],fp);
free(lineptr[i]);
}
fclose(fp);
}
void edit()
{
int i;
FILE *fp;
i=sscanf(chpt,"%s",fname);
if(i!=1)
{
printf("Enter file name.\n");
scanf("%s",fname);
}
if((fp=fopen(fname,"r"))==NULL)
{
fp=fopen(fname,"w");
fclose(fp);
fp=fopen(fname,"r");
}
i=0;
while(fgets(buffer,MAXLEN,fp)==buffer)
{
lineptr[i]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[i++],buffer);
}
fclose(fp);
last=i;
}
#include <stdio.h>
#define MAXLEN 80
#define MAXLINE 200
char buffer[MAXLEN],fname[120];
char *lineptr[MAXLINE];
FILE *fp;
void edit(),replace(),insert(),delete(),quit();
char comch[]="EeRrIiDdQq";
void(*comfun[])()={edit,replace,insert,delete,quit};
int modified=0,
last;
char *chpt;
main()
{
int j;
last=0;
while(1)
{
printf("\nInput a command:[e,r,i,d,q].\n");
gets(buffer);
for(chpt=buffer;*chpt==''||*chpt=='\t';chpt++);
if(*chpt=='\0') continue;
for(j=0;comch[j]!='\0'&&comch[j]!=*chpt;j++);
if(comch[j]=='\0') continue;
chpt++;
(*comfun[j/2])();
fprintf(stdout,"The text is:\n");
for(j=0;j<last;j++)
fputs(lineptr[j],stdout);
}
}
void quit()
{
int c;
if(modified)
{
printf("Save? (y/n)");
while(!(((c=getchar())>='a'&&c<='z')||(c>='A'&&c<='Z')));
if(c=='y'||c=='Y')
save(fname);
}
for(c=0;c<last;c++)
free(lineptr[c]);
exit(0);
}
void insert()
{
int k,m,i;
sscanf(chpt,"%d%d",&k,&m);
if(m<0||m>last||last+k>=MAXLINE)
{
printf("Error!\n");
return;
}
for(i=last;i>m;i--)
lineptr[i+k-1]=lineptr[i-1];
for(i=0;i<k;i++)
{
fgets(buffer,MAXLEN,stdin);
lineptr[m+i]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[m+i],buffer);
}
last+=k;
modified=1;
}
void delete()
{
int i,j,m,n;
sscanf(chpt,"%d%d",&m,&n);
if(m<=0||m>last||n<m)
{
printf("Error!\n");
return;
}
if(n>last)
n=last;
for(i=m;i<=n;i++)
free(lineptr[i-1]);
for(i=m,j=n+1;j<=last;i++,j++)
lineptr[i-1]=lineptr[j-1];
last=i-1;
modified=1;
}
void replace()
{
int k,m,n,i,j;
sscanf(chpt,"%d%d%d",&k,&m,&n);
if(m<=0||m>last||n<m||last-(n-m+1)+k>=MAXLINE)
{
printf("Error!\n");
return;
}
if(n>last)
n=last;
for(i=m;i<=n;i++)
free(lineptr[i-1]);
for(i=m,j=n+1;j<=last;i++,j++)
lineptr[i-1]=lineptr[j-1];
last=i-1;
for(i=last;i>=m;i--)
lineptr[i+k-1]=lineptr[i-1];
for(i=0;i<k;i++)
{
fgets(buffer,MAXLEN,stdin);
lineptr[m+i-1]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[m+i-1],buffer);
}
last+=k;
modified=1;
}
save(char *fname)
{
int i;
FILE *fp;
if((fp=fopen(fname,"w"))==NULL)
{
fprintf(stderr,"Can't open %s.\n",fname);
exit(1);
}
for(i=0;i<last;i++)
{
fputs(lineptr[i],fp);
free(lineptr[i]);
}
fclose(fp);
}
void edit()
{
int i;
FILE *fp;
i=sscanf(chpt,"%s",fname);
if(i!=1)
{
printf("Enter file name.\n");
scanf("%s",fname);
}
if((fp=fopen(fname,"r"))==NULL)
{
fp=fopen(fname,"w");
fclose(fp);
fp=fopen(fname,"r");
}
i=0;
while(fgets(buffer,MAXLEN,fp)==buffer)
{
lineptr[i]=(char *)malloc(strlen(buffer)+1);
strcpy(lineptr[i++],buffer);
}
fclose(fp);
last=i;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯