数据结构课程设计-文本编辑器

数据结构课程设计-文本编辑器|数据结构课程设计

#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -1
//#define EOF -1
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 1000
#define MAXQSIZE 10

static int i=0;
typedef char ElemType;
typedef struct StackNode//构造栈
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;

ElemType InitStack(SqStack *S)//初始化栈
{
S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S->base)
{
exit(OVERFLOW);
}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}

ElemType StackEmpty(SqStack *S)//判断栈是否为空
{
if(S->top==S->base)
return OK;
else
return ERROR;
}

ElemType Push(SqStack *S,ElemType e)//进栈操作
{
if(S->top-S->base>=S->stacksize)
{
S->base = (ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!S->base)
{
exit(OVERFLOW);
}
S->top = S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}

ElemType Pop(SqStack *S,ElemType *e)//出栈操作
{


if(S->top==S->base)
{
return ERROR;
}
*e=*--S->top;
//printf("%d\n",e);
// return e;
return 0;
}

void ClearStack(SqStack *S)//清空栈
{
S->top=S->base;
}

ElemType LineEdit(SqStack *S )//文本编译
{
char ch, e, a[30];
int i ;
ch = getchar();


while(1)
{
while (ch!='\n')
{
switch(ch)
{
case '#':
Pop(S,&e);
break;
case
'@':
ClearStack(S);
break;
default:
Push(S,ch);
break;
}
ch = getchar();
}
i = 0;
while (!StackEmpty(S))
{
Pop(S,&e);
a[i++]=e;
}
for(--i; i>= 0; i--)
{
printf("%c",a[i]);
}
printf("\n");
ClearStack(S);
ch = getchar();
}

return 0;
}

int main(void)
{

SqStack S;

InitStack(&S);
LineEdit(&S);

system("pause");
return 0;
}

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有