神,帮你找到了
#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();}