Problem Description
一个简单的行编辑程序的功能是:接受用户从终端输入的程序或数据,并存入用户的数据区。
由于用户在终端上进行输入时,不能保证不出差错,因此,若在编辑程序中,“每接受一个字符即存入用户数据区”的做法显然不是最恰当的。较好的做法是,设立一个输入缓冲区,用以接受用户输入的一行字符,然后逐行存入用户数据区。允许用户输入出差错,并在发现有误时可以及时更正。例如,当用户发现刚刚键入的一个字符是错的时,可补进一个退格符"#",以表示前一个字符无效;
如果发现当前键入的行内差错较多或难以补救,则可以键入一个退行符"@",以表示当前行中的字符均无效。
如果已经在行首继续输入’#'符号无效。
Input
输入多行字符序列,行字符总数(包含退格符和退行符)不大于250。
Output
按照上述说明得到的输出。
Sample Input
whli##ilr#e(s#*s)
outcha@putchar(*s=#++);
Sample Output
while(*s)
putchar(*s++);
#include <stdio.h>
#include <stdlib.h>
#define stackinitsize 300
#define stackcreat 10
#define ok 1
#define error -1
#define overflow -1
typedef int elemtype;
typedef struct
{
elemtype *base;
elemtype *top;
int stacksize;
} sqstack;
int initstack(sqstack *s)
{
s->base=(elemtype*)malloc(stackinitsize*sizeof(elemtype));
if(!s->base)exit(overflow);
s->top=s->base;
s->stacksize=stackinitsize;
return ok;
}
int push(sqstack *s,elemtype e)
{
if(s->top-s->base>=s->stacksize)
{
s->top=(elemtype*)realloc(s->base,(s->stacksize+stackcreat)*sizeof(elemtype));
s->top=s->base+s->stacksize;
}*(s->top++)=e;
return ok;
}
int pop(sqstack *s)
{
if(s->top==s->base)return error;
s->top--;
return ok;
}
char gettop(sqstack *s)
{
char e;
if(s->top==s->base)return error;
e=*(s->top-1);
return e;
}
int stackempty(sqstack *s)
{
if(s->top==s->base)return 1;
else return 0;
}
int main()
{
char c[300],e;
sqstack s,r;
int i;
while(scanf("%s",c)!=EOF)
{
initstack(&s);
initstack(&r);
for(i=0; c[i]!='\0'; i++)
{
if(c[i]=='#')
{
if(!stackempty(&s))
{
pop(&s);
}
else continue;
}
else if(c[i]=='@')
{
if(!stackempty(&s))
{
while(!stackempty(&s))
{
pop(&s);
}
}
else continue;
}
else push(&s,c[i]);
}
while(!stackempty(&s))
{
e=gettop(&s);
push(&r,e);
pop(&s);
}
while(!stackempty(&r))
{
e=gettop(&r);
printf("%c",e);
pop(&r);
}
printf("\n");
}
return 0;
}