数据结构实验之栈:行编辑器
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
输出
示例输入
whli##ilr#e(s#*s) outcha@putchar(*s=#++);
示例输出
while(*s) putchar(*s++);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define stackmax 10000
#define stacknum 10000
typedef int elemtype;
typedef struct
{
elemtype *base;
elemtype *top;
int stacksize;
}sqstack;
int initstack(sqstack &s)
{
s.base = (elemtype *)malloc(stackmax * sizeof(elemtype));
if(!s.base) exit(0);
s.top = s.base;
s.stacksize = stackmax;
}
char push(sqstack &s, char e)
{
if(s.top - s.base > s.stacksize)
{
s.base = (elemtype *)realloc(s.base,(s.stacksize+stacknum) * sizeof(elemtype));
if(!s.base) exit(0);
s.top = s.base + s.stacksize;
s.stacksize += stacknum;
}
*s.top++=e;
}
int pop(sqstack &s)
{
if(s.top==s.base) return 0;
s.top--;
return 1;
}
int clearstack(sqstack &s)
{
s.top = s.base;
return 1;
}
int stackempty(sqstack &s)
{
if(s.top == s.base)
return 1;
else
return 0;
}
char editor(sqstack &s, char a[])
{
int n = strlen(a);
int i;
for(i=0; i<n; i++)
{
if(a[i]!='#'&&a[i]!='@')
{
push(s, a[i]);
}
if(a[i]=='#'&&!stackempty(s))
pop(s);
if(a[i]=='@'&&!stackempty(s))
clearstack(s);
}
}
void putstack(sqstack &s)
{
while(s.top > s.base)
{
printf("%c", *(s.base));
s.base++;
}
printf("\n");
}
int main()
{
char a[255];
sqstack s;
while(gets(a)!=NULL)
{
initstack(s);
editor(s, a);
putstack(s);
}
return 0;
}