C语言简易行编辑器,简单的行编辑器C语言.doc
簡(jiǎn)單的行編輯器C語(yǔ)言
簡(jiǎn)單的行編輯器
【要求】
(1) 設(shè)置一個(gè)簡(jiǎn)單的行編輯器,每行以回車(chē)結(jié)束
(2) 數(shù)據(jù)以文件形式存儲(chǔ)
(3) 編輯器具有查找、替換、修改數(shù)據(jù)的功能
201-7-9。請(qǐng)把所有的注釋信息提取出來(lái)就可以寫(xiě)程序設(shè)計(jì)報(bào)告。
typedef struct LINE {
char text[szLINE]; /*文本內(nèi)容*/
struct LINE * next; /*鏈表指針*/
}L;
/*簡(jiǎn)寫(xiě)無(wú)類(lèi)型整數(shù)*/
typedef unsigned int U; /*定義12個(gè)行編輯命令的標(biāo)準(zhǔn)格式*/
typedef void (*FUNC)(L **, char*); /*定義12個(gè)標(biāo)準(zhǔn)行編輯命令的關(guān)鍵字*/
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*/
/*在鏈表中根據(jù)行號(hào)index調(diào)出指定的行*/
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存在則以帶行號(hào)格式保存文件(如果f==stdout則打印到屏幕上),瀏覽范圍為from到to(行號(hào))。view(lines, 0, 0, 0)表示統(tǒng)計(jì)已加載到內(nèi)存的文本行數(shù)量*/
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*/
/*在當(dāng)前文檔中根據(jù)關(guān)鍵字進(jìn)行搜索,并將搜索結(jié)果打印出來(lái)*/
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*/
/*在一行文本中執(zhí)行替換命令,把所有關(guān)鍵字替換為新關(guān)鍵字*/
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;
/*首先記錄新舊關(guān)鍵字長(zhǎng)度*/
la=strlen(key);
lb=strlen(replacement);
do { /*用指針逐個(gè)比較*/
r = memcmp(x, key, la);
if(r){/*如果關(guān)鍵字不匹配則復(fù)制字符串*/
*y=*x;
x++; y++;
}else{/*如果關(guān)鍵字匹配則替換字符串*/
memcpy(y, replacement, lb);
x += la; y += lb;
}/*end if*/
}while(*x);
/*將替換完成的結(jié)果返回*/
memcpy(string, fine, szLINE);
}/*end rpc*/
/*全文替換*/
void replace(L * lines, char * string, char * replaceme
總結(jié)
以上是生活随笔為你收集整理的C语言简易行编辑器,简单的行编辑器C语言.doc的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c语言变量ppt,C语言程序设计-变量.
- 下一篇: 用c写按键精灵脚本语言,按键精灵之插件编