行编辑器c语言,简单的行编辑器(C语言).doc

简单的行编辑器(C语言)

简单的行编辑器

【要求】

(1) 设置一个简单的行编辑器,每行以回车结束

(2) 数据以文件形式存储

(3) 编辑器具有查找、替换、修改数据的功能

201-7-9。请把所有的注释信息提取出来就可以写程序设计报告。

typedef struct LINE {

char text[szLINE]; /*文本内容*/

struct LINE * next; /*链表指针*/

}L;

/*简写无类型整数*/

typedef unsigned int U; /*定义12个行编辑命令的标准格式*/

typedef void (*FUNC)(L **, char*); /*定义12个标准行编辑命令的关键字*/

char keywords[CMDS][8]={

"quit", "help", "load", "save",

"view", "count", "append", "insert",

"erase", "edit", "lookup", "replace"

}; /*end keywords*/

/*清空链表操作*/

void clear(L ** lines)

{ L *a =0, *b=0;

if(!lines) return ;

a = *lines;

while(a) {

b=a->next ;

free(a);

a=b;

} /*end while*/

*lines=0;

} /*end clear*/

/*在链表中根据行号index调出指定的行*/

L *locate(L *lines, U index)

{ L *t=lines; U i = 0;

if(!t) return 0;

if(index == 0) return t;

for(i=0; i

t=t->next;

if(!t) return 0;

}/*next*/

return t;

}/*end locate*/

/*浏览命令,如果f存在则以带行号格式保存文件(如果f==stdout则打印到屏幕上),浏览范围为from到to(行号)。view(lines, 0, 0, 0)表示统计已加载到内存的文本行数量*/

int view(L * lines, FILE * f, U from, U to)

{L *t=lines; U index=0;

while(t) {

index ++;

if(f && index >= from && index <= to) fprintf(f, "%d: %s", index, t->text);

t=t->next;

}/*end while*/

return index;

}/*end view*/

/*在当前文档中根据关键字进行搜索,并将搜索结果打印出来*/

void lookup(L * lines, char * string)

{L *t=0; U index = 0;

if(!string) return ;

t=lines;

while(t) {

index ++;

if(strstr(t->text , string)) printf("%d: %s", index, t->text );

t=t->next;

}/*end while*/

}/*end lookup*/

/*在一行文本中执行替换命令,把所有关键字替换为新关键字*/

void rpc(char * string, char * key, char * replacement)

{ char fine[szLINE], *x=0, *y=0, *z=0;

int la=0, lb=0, r=0;

if(!string || !key || !replacement) return ;

memset(fine, 0, szLINE);

x=string; y=fine;

/*首先记录新旧关键字长度*/

la=strlen(key);

lb=strlen(replacement);

do { /*用指针逐个比较*/

r = memcmp(x, key, la);

if(r){/*如果关键字不匹配则复制字符串*/

*y=*x;

x++; y++;

}else{/*如果关键字匹配则替换字符串*/

memcpy(y, replacement, lb);

x += la; y += lb;

}/*end if*/

}while(*x);

/*将替换完成的结果返回*/

memcpy(string, fine, szLINE);

}/*end rpc*/

/*全文替换*/

void replace(L * lines, char * string, char * replaceme

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值