数据结构实验之栈:行编辑器
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^题目描述
一个简单的行编辑程序的功能是:接受用户从终端输入的程序或数据,并存入用户的数据区。
由于用户在终端上进行输入时,不能保证不出差错,因此,若在编辑程序中,“每接受一个字符即存入用户数据区”的做法显然不是最恰当的。较好的做法是,设立一个输入缓冲区,用以接受用户输入的一行字符,然后逐行存入用户数据区。允许用户输入出差错,并在发现有误时可以及时更正。例如,当用户发现刚刚键入的一个字符是错的时,可补进一个退格符"#",以表示前一个字符无效;
如果发现当前键入的行内差错较多或难以补救,则可以键入一个退行符"@",以表示当前行中的字符均无效。
如果已经在行首继续输入'#'符号无效。
输入
输入多行字符序列,行字符总数(包含退格符和退行符)不大于250。
输出
按照上述说明得到的输出。
示例输入
whli##ilr#e(s#*s)outcha@putchar(*s=#++);
示例输出
while(*s)putchar(*s++);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define stacksize 251
typedef char ElemType;
typedef struct
{
ElemType *top;
ElemType *base;
int Stacksize;
}SQ;
int InitStack(SQ &S)
{
S.base=(ElemType *)malloc(stacksize*sizeof(ElemType));
if(!S.base) exit(-1);
S.top=S.base;
S.Stacksize=stacksize;
return 1;
}
int Push(SQ &S,ElemType e)
{
*S.top++=e;
return 1;
}
int Pop(SQ &S,ElemType &e)
{
if(S.top==S.base) return 0;
e=*--S.top;
return 1;
}
void ClearStack(SQ &S)
{
S.top=S.base;
}
int EmptyStack(SQ &S)
{
if(S.top==S.base) return 1;
return 0;
}
int main()
{
char str[251],sstr[251];
while(~scanf("%s",str))
{
SQ S;
char e;
InitStack(S);
int len;
len=strlen(str);
for(int i=0;i<len;i++)
{
if(EmptyStack(S)&&(str[i]=='#'||str[i]=='@'))
continue;
else
{
if(EmptyStack(S))
Push(S,str[i]);
else if(str[i]=='#')
Pop(S,e);
else if(str[i]=='@')
ClearStack(S);
else
Push(S,str[i]);
}
}
int j=-1;
while(!EmptyStack(S))
{
Pop(S,e);
sstr[++j]=e;
}
while(j>-1)
printf("%c",sstr[j]);
printf("\n");
}
}