c语言行编辑程序
static.h 头文件
typedef struct bufferStatic{
char *top;
char *base;
int staticSize;
}bufferStatic;
typedef int Status;
//初始化栈
Status InitStatck(bufferStatic *S);
//出栈
Status Pop(bufferStatic *S,char *c);
//清空栈
Status ClearStack(bufferStatic *S);
//入栈
Status Push(bufferStatic *S,char c);
//销毁栈
Status DestoryStatic(bufferStatic *S);
//编辑缓冲区
void LineEdit();
staticBufferRealiz.c 文件
#include "static.h"
#include "stdio.h"
#include "stdlib.h"
#define MAX_SIZE 100
#define INCREMENT_SIZE 10
#define ERROR 0
#define OK 1
void main() {
LineEdit();
}
//初始化栈
Status InitStatck(bufferStatic *S) {
S->base = S->top = (bufferStatic *)malloc(sizeof(char)*MAX_SIZE);
if (!S->base)return ERROR;
return OK;
}
//出栈
Status Pop(bufferStatic *S, char *c) {
//判断栈是否为空栈
if (S->base == S->top)return ERROR;
S->top--;
*c = *(S->top);
return OK;
}
//清空栈
Status ClearStack(bufferStatic *S) {
S->base = S->top;
return OK;
}
//向栈中插入元素
Status Push(bufferStatic *S, char c) {
if ((S->top - S->base)>= MAX_SIZE) {
S->base = (bufferStatic*)realloc(S->base,(MAX_SIZE+INCREMENT_SIZE)*sizeof(char));
}
*(S->top) = c;
S->top++;
return OK;
}
//销毁栈
Status DestoryStatic(bufferStatic *S) {
free(S->base);//释放掉开辟的内存
return OK;
}
//编辑缓冲区
void LineEdit() {
bufferStatic S;
//初始化一个空栈
InitStatck(&S);
char c;
char ch = getchar();
//全文结束符
while (ch!=EOF)
{
while (ch != EOF&&ch != '\n')
{
switch (ch)
{
case '#':Pop(&S, &c);
case '@':ClearStack(&S);
default:Push(&S, ch);
break;
}
ch = getchar();
}
ClearStack(&S);
if (ch != EOF) ch = getchar();
}
DestoryStatic(&S);
}