欢迎来到天天文库
浏览记录
ID:51436837
大小:22.27 KB
页数:4页
时间:2020-03-24
《行编辑程序(C语言).docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#defineSTACK_INIT_SIZE100#defineSTACK_INCREMENT10typedefstructStack{char*top;char*base;intstacksize;}Stack,*pStack;voidinit(pStackps);boolis_full(pStackps);boolis_empty(pStackps);voidpush(pStackps,charch);boolpop(pStackps);vo
2、idclear(pStackps);voidtraverse(pStackps,FILE*fp);voiddestroy(pStackps);voidline_edit(FILE*fp);intmain(){FILE*fp=fopen("ed.txt","w");if(!fp){printf("创建文件失败!");}else{line_edit(fp);fclose(fp);}return0;}voidinit(pStackps){ps->base=(char*)malloc(sizeof(char)*STACK_INIT_SIZE);if(!p
3、s->base)exit(-1);ps->top=ps->base;ps->stacksize=STACK_INIT_SIZE;}boolis_full(pStackps){if(ps->top-ps->base==ps->stacksize)returntrue;elsereturnfalse;}boolis_empty(pStackps){if(ps->top==ps->base)returntrue;elsereturnfalse;}voidpush(pStackps,charch){if(is_full(ps)){ps->base=(char
4、*)realloc(ps->base,sizeof(char)*(ps->stacksize+STACK_INCREMENT));if(!ps->base)exit(-1);ps->top=ps->base+ps->stacksize;ps->stacksize+=STACK_INCREMENT;}*(ps->top)=ch;ps->top++;}boolpop(pStackps){if(is_empty(ps))returnfalse;ps->top--;returntrue;}voidtraverse(pStackps,FILE*fp){char
5、*p=ps->base;while(ps->top>p){fputc(*p,fp);p++;}}voidclear(pStackps){ps->top=ps->base;}voiddestroy(pStackps){free(ps->base);ps->top=ps->base=NULL;ps->stacksize=0;}voidline_edit(FILE*fp){Stacks;init(&s);charch;printf("请输入一个文本文件,^Z结束输入:");ch=getchar();while(ch!=EOF){while(ch!=EO
6、F&&ch!=''){switch(ch){case'#':if(!is_empty(&s))pop(&s);break;case'@':clear(&s);break;default:push(&s,ch);}ch=getchar();}traverse(&s,fp);fputc('',fp);clear(&s);if(ch!=EOF)ch=getchar();}destroy(&s);}
此文档下载收益归作者所有