文本编辑器

本文介绍了一个简单的文本编辑器实现过程,包括初始化、显示文本、处理编辑命令等功能。通过定义特定的数据结构来管理文本行,并实现了基本的编辑操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define LEN sizeof(struct LineTable)
struct LineTable
{
 char *front;   //行起始位置
 int length;    //行长度
 struct LineTable *next;
};
main()
{
 struct LineTable *head,*tail;
 char *ch;
 ch=(char *)malloc(1000*sizeof(char));
 head=(struct LineTable *)malloc(LEN);
 head->next=NULL;
 tail=(struct LineTable *)malloc(LEN);
 tail->next=head->next;
 head->next=tail;
 tail->front=ch;
 tail->length=0;
 Init(tail);
 Display(head);
 printf("/n/n");
 Handle(head);
 Display(head);
 getch();
}
//输出并加入行号
Display(struct LineTable *head)
{
 struct LineTable *p;
 char *pch;
 int i=1;
 p=head->next;
 pch=p->front;
 printf("%-4d  :",i);
 while(*pch!='#')
 {
  if (*pch=='/n')
  {
   p=p->next;
   pch=p->front;
   i++;
   printf("/n%-4d  :",i);
  }
  else if (*pch=='/0')
  {
   p=p->next;
   pch=p->front;
  }
  else
  {
   printf("%c",*pch);
   pch+=sizeof(char);
  }
 }
}
 
Init(struct LineTable *tail)
{
 char ch,*pch;
 struct LineTable *p;
 p=tail;
 pch=p->front;
 do
 {
  ch=getchar();
  *pch=ch;
  pch+=sizeof(char);
  if(ch=='/n')
  {
   p=(struct LineTable *)malloc(sizeof(LEN));
   p->next=tail->next;
   tail->next=p;
   tail=p;
   p->front=pch;
   p->length=0;
  }
  else
  {
   p->length++;
  }
 }while(ch!='#');
 p=(struct LineTable *)malloc(sizeof(LEN));
 p->next=NULL;
 tail->next=p;
 tail=p;
 p->front=pch;
 p->length=0;
}
 
Handle(struct LineTable *head)
{
 //以下是为该文本编辑定制操作语句,日后改用标志字符串方法
 char reedit[]={"reedit"}; //行修改
 char remove[]={"remove"}; //行清除
 //定制操作语句结束
 char str[3][100],ch;
 int i;
 for (i=0;i<3;i++)             //为什么这里用getchar()输入就不管用??????
 scanf("%s",str[i]);
 getch();
 if (!strncmp(str[0],reedit,strlen(reedit))) Reedit(str,head);
 else if (!strncmp(str[0],remove,strlen(remove))) printf("Handle remove");
 getch();
}
 
Reedit(char Handle[3][100],struct LineTable *head)
{
 int pos,i;
 char *ch;
 struct LineTable *p,*tail;
 tail=head->next;
 p=head->next;
 pos=atoi(Handle[1]);
 while(tail->next!=NULL)
 {
  tail=tail->next;
 }
 for (i=1;i<pos;i++)
 {
  p=p->next;
 }
 if(strlen(Handle[2])>p->length)
 {
  struct LineTable *p1;
  int i=0;
  *p->front='/0';
  p1=(struct LineTable *)malloc(sizeof(LEN));
  p1->length=0;
  p1->front=tail->front;
  ch=p1->front;
  p1->next=p->next;
  p->next=p1;
  p=p1;
  while(Handle[2][i]!='/0')
  {
   *ch=Handle[2][i];
   i++;
   ch+=sizeof(char);
  }
  *ch='/n';
  ch+=sizeof(char);
 }
 else
 {
  ch=p->front;
  for(i=1;i<=strlen(Handle[2]);i++)
  {
   *ch=Handle[2][i-1];
   ch+=sizeof(char);
  }
  *ch='/n';
  ch+=sizeof(char);
  p->length=strlen(Handle[2]);
 }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值