标题:栈的行编辑
只看楼主
天地一沙鸥
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2004-8-14
 问题点数:0 回复次数:1 
栈的行编辑

神,帮你找到了

#include<stdio.h> #include<string.h> #include<conio.h> #include<stdlib.h> #define STACK_INIT_SIZE 50 #define OK 1 #define TRUE 1 #define FALSE 0 #define ERROR 0 #define ESC 27 typedef char ElemType; typedef struct STACK /*定义栈类型*/ { ElemType *base; ElemType *top; int stacksize; int length; }SqStack,*Stack; typedef int Status; void InitStack(Stack *S) /*初始化栈*/ { *S=(SqStack *)malloc(sizeof(SqStack)); (*S)->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if(!(*S)->base)exit(-1); (*S)->top=(*S)->base; (*S)->stacksize=STACK_INIT_SIZE; (*S)->length=0; } Status DestroyStack(Stack *S) /* 销毁栈*/ { free((*S)->base); free((*S)); return OK; } void ClearStack(Stack *S) /*把栈置为空*/ { (*S)->top=(*S)->base; (*S)->length=0; } Status StackEmpty(SqStack S) /*判断栈空否*/ { if(S.top==S.base) return TRUE; else return FALSE; } void Push(Stack *S,ElemType e) /*把数据压入栈*/ { if((*S)->top - (*S)->base>=(*S)->stacksize) { (*S)->base=(ElemType *) realloc((*S)->base, ((*S)->stacksize + 10) * sizeof(ElemType)); if(!(*S)->base)exit(-1); (*S)->top=(*S)->base+(*S)->stacksize; (*S)->stacksize +=10; } *((*S)->top++)=e; ++(*S)->length; } Status Pop(Stack *S,ElemType *e)/*删除栈顶元素*/ { if((*S)->top==(*S)->base) return ERROR; *e=*((*S)->top-1); --(*S)->length; (*S)->top--; return OK; } void LineEdit() { Stack S; FILE *fp; char ch,c,a[50]; int i,b; InitStack(&S); /* 构造空栈 ,用来当缓冲区*/ fp=fopen("wenjian.c","w"); if(!fp) { printf(" cannot");exit(0);} ch=getchar(); while(ch!='Q') {while(ch!='Q'&&ch!='\n') { switch(ch) { case '#':Pop(&S,&c);break;/*当栈非空时退栈*/ case '@':ClearStack(&S);break; default:Push(&S,ch);break;/*有效字进栈*/ } ch=getchar();/*接收下一个字符*/ } if(S->length!=0) { b=S->length; for(i=S->length-1;i>=0;i--) Pop(&S,&a[i]); /*把栈逆序输出到 a */ for(i=0;i<b;i++) /*输出到文件去*/ fputc(a[i],fp); if(ch=='\n') /*为还行符一起输到文件去*/ fputc(ch,fp); ClearStack(&S);} /*把栈重置为空,以便再当缓冲区*/ if(ch!='Q') ch=getchar(); } DestroyStack(&S);fclose(fp); } main() { LineEdit();}

搜索更多相关主题的帖子: include 
2004-09-08 13:19
天地一沙鸥
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2004-8-14
得分:0 

鸟凄声以孤归, 兽索偶而不还。 悼当年之晚暮, 恨兹岁之欲殚。 思宵梦以从之, XXX而不安; 若凭舟之失棹, 譬缘崖而无攀。 /img/assets/200401/200401061015134010607.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://joke./img/assets/200401/200401061015134010607.jpg');}" onmousewheel="return imgzoom(this);" alt="" />
2004-09-08 13:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-5127-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.143139 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved