#include <stdio.h>
#include <stdlib.h>
#define STACKSIZE 100
typedef char DataType;
typedef struct
{
DataType stack[STACKSIZE];
int top;
}SeqStack;
void InitStack(SeqStack *S);
int StackEmpty(SeqStack S);
int GetTop(SeqStack S, DataType *e);
int PushStack(SeqStack *S, DataType e);
int PopStack(SeqStack *S, DataType *e);
int StackLength(SeqStack S);
void ClearStack(SeqStack *S);
void InitStack(SeqStack *S)
{
S->top = 0;
}
int StackEmpty(SeqStack S)
{
if (S.top == 0)
{
return 1;
}
else
{
return 0;
}
}
int GetTop(SeqStack S, DataType *e)
{
if (S.top <= 0)
{
printf("栈已经空!n");
return 0;
}
else
{
*e = S.stack[S.top - 1];
return 1;
}
}
int PushStack(SeqStack *S,DataType e)
{
if(S->top >= STACKSIZE-1)
{
printf("栈已满,不能入栈!");
return 0;
}
else
{
S->stack[S->top] = e;
S->top++;
return 1;
}
}
int PopStack(SeqStack *S,DataType *e)
{
if(S->top <= 0)
{
printf("栈已经没有元素,不能出栈!n");
return 0;
}
else
{
S->top--;
*e = S->stack[S->top];
return 1;
}
}
int StackLength(SeqStack S)
{
return S.top;
}
void ClearStack(SeqStack *S)
{
S->top = 0;
}
typedef char DataType;
void LineEdit();
int main()
{
LineEdit();
}
void LineEdit()
{
SeqStack S;
char ch;
DataType e;
DataType a[50];
int i, j = 0;
InitStack(&S);
printf("请输入字符序列(#表示前一个字符无效,@表示当前行无效).\n");
ch = getchar();
while (ch != '\n')
{
switch (ch)
{
case '#':
if (!StackEmpty(S))
PopStack(&S, &ch);
break;
case '@':
ClearStack(&S);
break;
default:
PushStack(&S, ch);
}
ch = getchar();
}
while (!StackEmpty(S))
{
PopStack(&S, &e);
a[j++] = e;
}
for (i = j - 1; i >= 0; i--)
printf("%c", a[i]);
printf("\n");
ClearStack(&S);
}