c语言 简单行编辑程序

Python017

c语言 简单行编辑程序,第1张

你的这个要求是绝对不可能有人满足你的。因为你的这个要求已经远远不只是一个最简单的 C 语言源程序代码了,它至少可以看成是一个 C 语言大作业(是一个行编辑程序,类似 UNIX/Linux 系统的 vi 程序了)。

而且了,任何一个编程人员,即使他们的编程水平再高,但是他们也毕竟不是神,看到你的需求之后马上就能够直接编写出全部、且正确的程序源代码,还必须要保证能够马上运行出正确的结果。

因为在整个编写程序的过程中,在程序代码调试上所花费的时间通常要比编写程序代码所花费的时间多得多。

而且再说了,编写任何程序都必须要在自己的电脑上安装一个程序开发、调试环境,通过自己亲自上机编写程序、且通过自己的艰苦调试程序,最终调试出程序的正确运行结果。

以上就是我个人多年编程的亲身体会。

/*

c语言程序设计 简单的行编辑器

【要求】

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

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

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

【备注】完全原创,编写时间:2010-7-13。请把所有的注释信息提取出来就可以写程序设计报告。

*/

#include /*标准文件流操作,这里使用了fopen/fclose/fprintf/printf/scanf/gets函数*/

#include /*标准系统库,这里使用了malloc/free/exit*/

#include /*标准字符串库,这里使用strlen/strcpy/memcpy/memset*/

#define szLINE 252 /*定义一行字符串最长为252字节*/

#define CMDS 12 /*定义12个标准行编辑命令*/

/*采用链表存储文本*/

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 = linesU i = 0

if(!t) return 0

if(index == 0) return t

for(i = 0i <indexi++) {

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 = linesU index = 0

while(t) {

index ++

if(f &&index >= from &&index text)

t = t->next

}/*end while*/

return index

}/*end view*/

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

void lookup(L * lines, char * string)

{

L * t = 0U 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 = stringy = 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 += lay += lb

}/*end if*/

}while(*x)

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

memcpy(string, fine, szLINE)

}/*end rpc*/

/*全文替换*/

void replace(L * lines, char * string, char * replacement)

{

L * t = 0U index = 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*/

/*根据行号插入一行新文本,如果行号小于零则将文本追加至链表尾*/

void insert(L ** lines, char * line, int index)

{

L * t = 0, * s = 0int 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)/*如果行号为正整数,则将链表指针指向行号之前*/

for(i = 0i <index-2i++) {

if(!s->next ) 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 = 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->text , line)

}/*end edit*/

/*将文件整个装入链表*/

int load(L ** lines, char * file)

{

FILE * f = 0char line[szLINE]=""

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)) {

memset(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 = 0L * t = lines

int total = 0

if(!lines || !file) 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 exec_load(L ** lines, char * line)

{

char cmd[szLINE] = "", file[szLINE] = ""

/*分析命令行,提取文件名*/

sscanf(line, "%s %s", cmd, file)

/*执行加载命令*/

load(lines, file)

}/*end exec_load*/

/*执行文本保存命令*/

void exec_save(L ** lines, char * line)

{

char cmd[szLINE] = "", file[szLINE] = ""

/*分析命令行,提取文件名*/

sscanf(line, "%s %s", cmd, file)

/*执行保存命令*/

save(*lines, file)

}/*end exec_save*/

/*执行文本查看命令*/

void exec_view(L ** lines, char * line)

{

char cmd[szLINE] = ""U from = 0, to = 0

/*分析命令行,提取目标要查看的起始行号和终止行号*/

sscanf(line, "%s %u %u", cmd, &from, &to)

/*如果起始行号和终止行号大小相反,则根据起始行号显示一页*/

if(to <from) to = from + 24

/*执行查看命令*/

view(*lines, stdout, from, to)

}/*end exec_view*/

/*执行行数统计命令*/

void exec_count(L ** lines, char * line)

{

fprintf(stderr, "%d lines in mem.\n", view(*lines, 0, 0, 0))

}/*end count*/

/*执行文本追加命令*/

void exec_append(L ** lines, char * line)

{

char text[szLINE] = ""

/*在命令之后另起新行用于录入文本*/

gets(text)strcat(text, "\n")

/*执行文本追加命令*/

insert(lines, text, -1)

}/*end exec_append*/

/*执行文本插入命令*/

void exec_insert(L ** lines, char * line)

{

char cmd[szLINE] = "", text[szLINE] = ""U index = 0

/*从命令行提取目标插入点的行号*/

sscanf(line, "%s %d", cmd, &index)

/*在命令之后另起新行用于录入文本*/

gets(text)strcat(text, "\n")

/*执行文本插入命令*/

insert(lines, text, index)

}/*end insert*/

/*执行文本删除命令*/

void exec_erase(L ** lines, char * line)

{

char cmd[szLINE] = ""U index = 0

/*从命令行提取目标行号*/

sscanf(line, "%s %d", cmd, &index)

/*执行文本删除命令*/

erase(lines, index)

}/*end erase*/

/*执行文本编辑命令*/

void exec_edit(L ** lines, char * line)

{

char cmd[szLINE] = "", text[szLINE] = ""U index = 0

/*从命令行提取目标行号*/

sscanf(line, "%s %d", cmd, &index)

/*在命令之后另起新行用于录入文本*/

gets(text)strcat(text, "\n")

/*执行文本编辑命令*/

edit(*lines, text, index)

}/*end edit*/

/*执行文本检索命令*/

void exec_lookup(L ** lines, char * line)

{

char cmd[szLINE] = "", text[szLINE] = ""

/*从命令行提取关键字*/

sscanf(line, "%s %s", cmd, text)

/*执行文本检索命令*/

lookup(*lines, text)

}/*end lookup*/

/*执行在线帮助命令*/

void exec_help(L ** lines, char * line)

{printf("\tcommands:\n\thelp\n\tquit\n\

\tload [file.txt]\n\

\tsave [file.txt]\n\

\tview [from line] [to line]\n\

\tcount\n\

\tappend [return + text]\n\

\tinsert [line number] [return + text]\n\

\terase [line number]\n\

\tedit [line number] [return + text]\n\

\tlookup [text]\n\

\treplace [keyword] [replacement]\n")

}/*end help*/

/*执行文本替换命令*/

void exec_replace(L ** lines, char * line)

{

char cmd[szLINE] = "", text[szLINE] = "", key[szLINE]=""

/*从命令行提取新旧关键字*/

sscanf(line, "%s %s %s", cmd, key, text)

/*执行文本替换命令*/

replace(*lines, key, text)

}/*end replace*/

/*执行退出命令*/

void exec_quit(L ** lines, char * line){exit(0)}

/*行编辑命令执行函数,顺序与关键字表keywords一一对应*/

FUNC functions[CMDS]={

exec_quit, exec_help, exec_load, exec_save,

exec_view, exec_count, exec_append, exec_insert,

exec_erase, exec_edit, exec_lookup, exec_replace

}/*end functions*/

/*从行输入中识别关键字,相当于parse*/

int identified(char * command, char * key)

{

int ln = 0, r = 0

if(!command || !key) return 0

ln = strlen(key)

r = memcmp(command, key, ln)

return r==0

}/*end identified*/

/*主函数*/

int main(int argc, char * argv[])

{

L * lines = 0char line[szLINE]=""int a = 0, b = 0, i = 0FUNC fun = 0

/*打印欢迎信息*/

printf("Welcome to LINE EDITOR V1.0\nCommand [help] is available.\n")

/*如果带主函数带参数,则可以用于打印帮助,或者根据该参数加载一个文本文件*/

if(argc >1) {

a = strcmp(argv[1], "--help")

b = strcmp(argv[1], "/h")

if(a &&b)

load(&lines, argv[1])

else{

exec_help(0, 0)

return 0

}/*end if*/

}/*end if*/

/*主命令循环*/

for() {

/*命令提示符中间是表示当前载入的文档总共有多少行的意思*/

printf("\n

/*TC2编译通过*/

/*以回车符为结束符输入一行*/

#include <stdio.h>

#define MAXSIZE 128

int main()

{

char base[MAXSIZE]

char *stack,*p

char c

stack=base

while((c=getchar())!='\n')

{

if(c=='@') stack=base

else if(c=='#'){if(stack!=base) stack--}

else *(++stack)=c

}

if(stack==base) printf("√")/*没有可输出的字符*/

p=base

while(p!=stack) printf("%c",*(++p))

getch()

}