简单的文本编辑程序

这是一个简单的文本编辑程序,包含基本的文本处理操作

这个程序在数据结构串那一个章节有算法描述,先申请一大块内存,输入数据,然后为内存中的数据进行分页,建立目录索引,建立结束后,以后所有的操作都是基于目录索引进行操作,使操作简单化了

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LENGTH 33000
typedef struct con
{
char *begin;
char *end;
char *line;
int pnumber;
int lnumber;
int clength;
struct con *next;
}Content;
int Initpage(Content *p);
void Read(Content *p);
int Table(Content *p);
int Edit(Content *p);
int Insert(Content *p);
int Search(Content *p);
int Getnext(int next[],char temp[]);
int Get(void);
int Save(Content *p,char *address);
Content *PLsearch(Content *p,int page,int line);
int Print(Content *p);
int Del(Content *p);
int End(Content *p,char *q);
void main(void)
{ char address[100];
Content *p=0,*ph=0;
int flag;
while(!(ph=(Content *)malloc(sizeof(Content))))
   continue;
Initpage(ph);
do
{ p=ph;
    printf("/n/n所有操作以行为单位,并且必须先进行输入!!!");
   printf("/n你想做什么?/n1,读入/n2,编辑/n3,插入/n4,删除/n5,查找/n0,退出/n选择=");
    flag=Get();
getchar();
switch(flag)
{ case 1: Read(p);break;
   case 2: Edit(p);break;
      case 3: Insert(p);break;
      case 4: Del(p);break;
   case 5: Search(p);break;
   case 0: break;
      default :printf("/n你的选择无效,请重新输入/n");
}
}while(flag);
printf("/n你想保存吗?/n1,保存/n0,不保存/n");
flag=Get();
getchar();
if(flag)
{ printf("请输入路径,例如:d:诗集李白的诗.txt/n路径=");
gets(address);
Save(ph,address);
printf("保存完毕,路径是: %s /n谢谢使用,按任意键继续",address);
}
else printf("谢谢使用,按任意键继续");
getchar();
free(ph->begin);
while(ph->next)
{ p=ph;
    ph=ph->next;
    free(p);
}
free(ph);
}
int Initpage(Content *p)
{ char *c=0;
while(!(c=(char *)malloc((LENGTH)*sizeof(char))))
   continue;
p->begin=c;
p->line=c;
p->pnumber=0;
p->lnumber=0;
p->clength=0;
p->next=0;
return 1;
}
void Read(Content *p)
{   FILE *fp=0;
    int flag,i=0;
    char *c=0,address[100],ch;
    Content *ph=0;
    ph=p;
    c=p->begin;
    printf("以什么方式读取?/n1,用键盘输入/n2,从硬盘读取/n0,放弃读取/n");
    flag=Get();
    getchar();
    switch(flag)
   { case 1: printf("用 # 结束输入/n");
             while((ch=getchar())!='#'&&i<LENGTH)
     { c[i]=ch;
            i++;
     }
          p->end=&c[i-1];
          printf("输入结束");
          break;
    case 2: printf("输入读取文件路径,例如:d:诗集李白的诗.txt/n路径=");
         gets(address);
               if(!(fp=fopen(address,"r")))
      { printf("文件:%s 打不开",address);
           return ;
     }
            while(!feof(fp)&&i<LENGTH)
      { c[i]=fgetc(fp);
           i++;
      }
         p->end=&c[i-2];
         printf("/n读取完毕");
         fclose(fp);
         break;
   }
    Table(ph);
    ph=p;
    Print(ph);
}
int Table(Content *p)
{ Content *q=0,*t=0;
char *c=0,*e=0,*h=0;
int i=0,line,page=0;
h=c=p->begin;
e=p->end;
q=p;
while(c<e)
{ line=0;
    while(c<e&&line<20)
    { q->line=c;
      q->pnumber=page;
      q->lnumber=line;
      while(*c!='/n'&&c<e)
      { c++;
        i++;
      }
      c++;
      q->clength=i;
      line++;
      i=0;
      while(!(q->next=t=(Content *)malloc(sizeof(Content))))
    continue;
      q=t;
      q->end=e;
      q->line=0;
      q->next=0;
    }
   page++;
}
return 1;
}
int Edit(Content *p)
{ int page,line,i,j=0;
char temp[1000],ch;
Content *hp=0,*hl=0;
hp=p;
printf("请输入页码/n页码=");
page=Get();
getchar();
printf("请输入行码/n行码=");
line=Get();
getchar();
hl=PLsearch(p,page,line);
if(hl->next==0)
{ Print(hp);
    printf("/n********超出范围,编辑失败!!!*******/n");
return 0;
}
hl=hl->next;
printf("请输入新的行:/n输入=");
for(i=0;(ch=getchar())!='/n';i++)
   temp[i]=ch;
temp[i]=ch;
if(i+1>=hl->clength)
{ strncpy(hp->end+3,temp,i+1);
    hl->line=hp->end+3;
    hl->clength=i;
    hl->end=hp->end+3+i;
    End(hp,hp->end+3+i);
}
else
{ strncpy(hl->line,temp,i+1);
    hl->clength=i;
}
p=hp;
Print(hp);
   return 1;
}
int Insert(Content *p)
{ char temp[1000],ch;
int i,page,line;
Content *hp=0,*hl=0,*s=0,*t=0;
hp=p;
printf("请输入插入的页码/n页码=");
page=Get();
getchar();
printf("请输入插入的行码/n行码=");
line=Get();
getchar();
hl=PLsearch(hp,page,line);
if(hl->next==0||hl->next->next==0)
{ Print(hp);
    printf("/n********超出范围,插入失败!!!*******/n");
return 0;
}
printf("请输入要插入的新行/n新行=");
for(i=0;(ch=getchar())!='/n';i++)
    temp[i]=ch;
temp[i]=ch;
strncpy(hp->end+3,temp,i+1);

while(!(s=(Content *)malloc(sizeof(Content))))
   continue;
s->next=hl->next;
hl->next=s;
s->begin=hp->begin;
s->line=hp->end+3;
s->end=hp->end+3+i;
s->clength=i;
s->lnumber=line-1;
s->pnumber=page;
t=s;
while(s->pnumber==s->next->pnumber)
{ s->lnumber++;
    s=s->next;
}
s->lnumber++;
End(p,t->end);
Print(hp);    
return 1;
}
int Del(Content *p)
{ int page,line;
Content *ph=p,*hl=0,*t=0;
printf("请输入要删除的页码/n页码=");
page=Get();
getchar();
printf("请输入要删除的行码/n行码=");
line=Get();
getchar();
hl=PLsearch(ph,page,line);
if(hl->next==0)
{ Print(ph);
    printf("/n**************超出范围,删除失败!!!*************/n");
return 0;
}
t=hl->next;
ph=hl->next=hl->next->next;
free(t);
while(ph->next&&(ph->pnumber==ph->next->pnumber))
{ ph->lnumber--;
    ph=ph->next;
}
ph->lnumber--;
ph=p;
Print(ph);
return 1;
}
int Search(Content *p)
{ char *c=0,*ec=0,temp[1000];
int i,j,k,l,m=1,n=1,next[1000];
Content *q=0;
q=p;
ec=p->end;
printf("请输入查找的内容:/n查找内容=");
gets(temp);
l=strlen(temp);
Getnext(next,temp);
while(q->next)
{ i=0;
    j=0;
   c=q->line;
   while(i<q->clength&&j<l)
   { if(j==0||c[i]==temp[j])
   { i++;
     j++;
   }
       else
   {    if(next[j]!=-1)
                 j=next[j];
               else
                {j=0;
                 i++;
                 }
   }
          }
      if(j>=l)
   {
     if(n)
   printf("**************** 找到了,整行如下 :******************/n");
   printf("/n%d页,%d行: ",q->pnumber,q->lnumber);
   for(k=0;k<q->clength;k++)
    putchar(c[k]);
   n=0;
   m=0;
   }
   q=q->next;
}
if(m)
printf("***********找不到:** %s ******",temp);
return 1;
}
int Get(void)
{ int m;
while(scanf("%d",&m)!=1)
{ while(getchar()!='/n')
    continue;
    printf("请输入一个整数/n");
}
return m;
}
int Save(Content *p,char *address)
{
FILE *fp=0;
int i;
char *c=0;
if(!(fp=fopen(address,"w")))
{ printf("打不开 :%s",address);
    return 0;
}
   while(p->next)
{ c=p->line;
   for(i=0;i<=p->clength;i++)
   fputc(c[i],fp);
      p=p->next;
}
fclose(fp);
   return 1;
}
Content *PLsearch(Content *p,int page,int line)
{ int i=0,j=0;
Content *q=0;
q=p;
while(q->next&&i<page)
{ if(q->pnumber<q->next->pnumber)
{    i++;
      if(i==page)
    break;
}
   q=q->next;
}
if(line==0)
   return q;
else
{
   while(q->next)
{ if(q->lnumber<q->next->lnumber)
{ j++;
   if(j==line)
    break;
}
   q=q->next;
}
return q;
}
}
int End(Content *p,char *q)
{ Content *hp=0;
hp=p;
while(hp->next)
{ hp->end=q;
    hp=hp->next;
}
return 1;
}
int Print(Content *p)
{ int i;
Content *ph=0;
char *c=0;
ph=p;
while(ph->next)
{ c=ph->line;
   printf("/n%d页,%d行 ",ph->pnumber,ph->lnumber);
   for(i=0;i<ph->clength;i++)
     putchar(c[i]);
      ph=ph->next;
}

return 1;
}
int Getnext(int next[],char temp[])
{ int i=0,j=-1,l;
l=strlen(temp);
next[0]=-1;
while(i<l&&j<l)
   { if(j==-1||temp[i]==temp[j])
      { i++;
        j++;
      
       if(temp[i]!=temp[j])
         next[i]=j;
       else next[i]=next[j];
       }
     else
       j=next[j];
   }

return 1;
}                          

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值