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

上传人:jiups****uk12 文档编号:38434479 上传时间:2018-05-01 格式:DOC 页数:9 大小:54.50KB
返回 下载 相关 举报
简单的行编辑器(C语言)_第1页
第1页 / 共9页
简单的行编辑器(C语言)_第2页
第2页 / 共9页
简单的行编辑器(C语言)_第3页
第3页 / 共9页
简单的行编辑器(C语言)_第4页
第4页 / 共9页
简单的行编辑器(C语言)_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《简单的行编辑器(C语言)》由会员分享,可在线阅读,更多相关《简单的行编辑器(C语言)(9页珍藏版)》请在金锄头文库上搜索。

1、简单的行编辑器 【要求】 (1) 设置一个简单的行编辑器,每行以回车结束 (2) 数据以文件形式存储 (3) 编辑器具有查找、替换、修改数据的功能 2011-7-9。请把所有的注释信息提取出来就可以写程序设计报告。#include /*标准文件流操作,这里使用了 open/fclose/fprintf/printf/scanf/gets 函数*/ #include /*标准系统库,这里使用了 malloc/free/exit*/ #include /*标准字符串库,这里使用 strlen/strcpy/memcpy/memset*/ #define szLINE 252 /*定义一行字符串最长

2、为 252 字节*/ #define CMDS 12 /*定义 12 个标准行编辑命令*/ /*采用链表存储文本*/ typedef struct LINE char textszLINE; /*文本内容*/ struct LINE * next; /*链表指针*/ L; /*简写无类型整数*/ typedef unsigned int U; /*定义 12 个行编辑命令的标准格式*/ typedef void (*FUNC)(L *, char*); /*定义 12 个标准行编辑命令的关键字*/ char keywordsCMDS8=“quit“, “help“, “load“, “save

3、“, “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

4、) L *t=lines; U i = 0;if(!t) return 0;if(index = 0) return t;for(i=0; inext;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

5、; while(t) index +; if(f 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*/ /

6、*end lookup*/ /*在一行文本中执行替换命令,把所有关键字替换为新关键字*/ void rpc(char * string, char * key, char * replacement) char fineszLINE, *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 /*

7、用指针逐个比较*/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 * replacement) L *t=0;U index

8、=0; if(!string | !lines | !replacement) return ; t=lines; while(t) index +; if(strstr(t-text , string) printf(“BEFORE %d: %s“, index, t-text ); rpc(t-text, string, replacement); printf(“AFTER %d: %s“,index,t-text); /*end if*/ t=t-next; /*end while*/ /*end replace*/ /*根据行号插入一行新文本,如果行号小于零则将文本追加至链表尾*/

9、void insert(L * lines, char * line, int index) L *t=0, *s=0; int i=0;if(!lines | !line) return ;/*首先为新文本分配一个链表节点*/t=(L*)malloc(sizeof(L);memset(t, 0, sizeof(L);strcpy(t-text , line);if(index = 0 | !*lines) /*如果链表为空则以新节点为起点定义链表*/t-next=*lines;*lines=t;return ;/*end if*/s=*lines;if(index 0)/*如果行号为正整数,

10、则将链表指针指向行号之前*/for(i=0;inext ) break;s = s-next ;/*next*/else/*否则链表指针指向表尾*/while(s-next ) s = s-next ;/*end if*/*完成链表插入操作*/if(s-next ) t-next = s-next ;s-next = t; /*end insert*/ /*根据行号删除一行文本*/ void erase(L * lines, U index) L *a=0, *b=0, *c=0;if(!lines) return ;/*index -1 表示目标行,index -2 表示目标行的前一行*/a

11、=locate(*lines, index-2);b=locate(*lines, index-1);if(!b) return ;if(a) /*如果前一行存在则删除目标行*/a-next=b-next;else/*否则表示表头删除*/*lines = b-next ;/*end if 释放内存*/free(b); /*end erase*/ /*根据行号和新录入文本替换原有行*/ void edit(L * lines, char * line, U index) L * t=locate(lines,index-1);if(!t)return ;if(line) strcpy(t-tex

12、t , line); /*end edit*/ /*将文件整个装入链表*/ int load(L * lines, char * file) FILE * f=0; char lineszLINE=“; int total = 0; if(!lines | !file) return 0; clear(lines);/*首先清空链表*/ /*打开文件*/ f=fopen(file, “r“); if(!f) fprintf(stderr, “%s is bad.n“, file); return 0;/*end if*/ /*逐行读入内存并插入表尾*/ while(!feof(f) memse

13、t(line, 0, szLINE); fgets(line, szLINE - 1, f); insert(lines, line, -1); total +; /*end while*/ fclose(f); fprintf(stderr, “%s %d lines loaded.n“, file, total); /*返回总行数*/ return total; /*end load*/ /*将链表保存到指定的文本文件*/ int save(L * lines, char * file) FILE *f=0; L *t=lines;int total=0;if(!lines | !file

14、) return 0;/*打开文件*/f=fopen(file, “w“);if(!f) fprintf(stderr, “%s is bad.n“, file);return 0;/*end if*/t=lines;while(t) /*逐个文件写入*/fprintf(f,“%s“, t-text );t=t-next ;total +;/*end while*/fclose(f);fprintf(stderr, “%s %d lines saved.n“, file, total);/*返回总行数*/return total; /*save*/ /*执行加载文本文件命令*/ void ex

15、ec_load(L * lines, char * line) char cmdszLINE = “, fileszLINE = “;/*分析命令行,提取文件名*/sscanf(line, “%s %s“, cmd, file);/*执行加载命令*/load(lines, file); /*end exec_load*/*执行文本保存命令*/ void exec_save(L * lines, char * line) char cmdszLINE = “, fileszLINE = “;/*分析命令行,提取文件名*/sscanf(line, “%s %s“, cmd, file); /*执行

16、保存命令*/save(*lines, file); /*end exec_save*/ /*执行文本查看命令*/ void exec_view(L * lines, char * line) char cmdszLINE = “; U from = 0, to = 0;/*分析命令行,提取目标要查看的起始行号和终止行号*/sscanf(line, “%s %u %u“, cmd, /*如果起始行号和终止行号大小相反,则根据起始行号显示一页*/if(to 1) a=strcmp(argv1, “-help“);b=strcmp(argv1, “/h“);if(a elseexec_help(0, 0)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号