《行编辑程序(C语言).docx》由会员分享,可在线阅读,更多相关《行编辑程序(C语言).docx(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、#include #include #include #define STACK_INIT_SIZE 100#define STACK_INCREMENT 10typedef struct Stackchar *top;char *base;int stacksize;Stack, *pStack;void init(pStack ps);bool is_full(pStack ps);bool is_empty(pStack ps);void push(pStack ps, char ch);bool pop(pStack ps);void clear(pStack ps);void tra
2、verse(pStack ps, FILE *fp);void destroy(pStack ps);void line_edit(FILE *fp);int main()FILE *fp = fopen(ed.txt,w);if(!fp)printf(创建文件失败!n);elseline_edit(fp);fclose(fp);return 0;void init(pStack ps)ps-base = (char *)malloc(sizeof(char) * STACK_INIT_SIZE);if(!ps-base)exit(-1);ps-top = ps-base;ps-stacksi
3、ze = STACK_INIT_SIZE;bool is_full(pStack ps)if(ps-top - ps-base = ps-stacksize)return true;elsereturn false;bool is_empty(pStack ps)if(ps-top = ps-base)return true;elsereturn false;void push(pStack ps, char ch)if(is_full(ps)ps-base = (char *)realloc(ps-base, sizeof(char) * (ps-stacksize + STACK_INCR
4、EMENT);if(!ps-base)exit(-1);ps-top = ps-base + ps-stacksize;ps-stacksize+=STACK_INCREMENT;*(ps-top) = ch;ps-top+;bool pop(pStack ps)if(is_empty(ps)return false;ps-top-;return true;void traverse(pStack ps, FILE *fp)char * p = ps-base;while(ps-top p)fputc(*p, fp);p+;void clear(pStack ps)ps-top = ps-ba
5、se;void destroy(pStack ps)free(ps-base);ps-top = ps-base =NULL;ps-stacksize = 0;void line_edit(FILE *fp)Stack s;init(&s);char ch;printf(请输入一个文本文件,Z结束输入:n);ch = getchar();while(ch != EOF)while(ch != EOF & ch != n)switch(ch)case #:if(!is_empty(&s)pop(&s);break;case :clear(&s);break;default:push(&s, ch);ch = getchar();traverse(&s, fp);fputc(n, fp);clear(&s);if(ch != EOF)ch = getchar();destroy(&s);