请高手帮忙,非诚勿扰!!!
无名君jn 2009-12-22 06:34:00 #include < iostream >
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int status ;
#define OK 1
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define ERROR 0
class SqStack
{
private:
char * top;
char * base;
int stacksize;
public:
status InitStack ( SqStack & S );
status ClearStack ( SqStack & S );
status Push ( SqStack & S , char e );
status Pop ( SqStack & S , char & e );
status DestroyStack ( SqStack & );
friend void LineEdit ( SqStack & );
};
status SqStack::InitStack ( SqStack & S )
{
S.base = ( char * ) malloc ( STACK_INIT_SIZE * sizeof ( char ) );
if ( ! S.base )
exit ( OVERFLOW );
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
status SqStack::ClearStack ( SqStack & S )
{
S.top = S.base;
return OK;
}
status SqStack::Push ( SqStack & S , char e )
{
if ( S.top - S.base >= S.stacksize )
{
S.base = ( char * ) realloc ( S.base , ( S.stacksize + STACKINCREMENT ) * sizeof ( char ) );
if ( ! S.base )
exit ( OVERFLOW );
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
* S.top = e;
S.top ++;
return OK;
}
status SqStack::Pop ( SqStack & S , char & e )
{
if ( S.top == S.base )
return ERROR;
S.top --;
e = * S.top;
return OK;
}
status SqStack::DestroyStack ( SqStack & S )
{
S.base = NULL;
S.top = S.base;
return OK;
}
void LineEdit ( SqStack & S)
{
char ch , c ;
S.InitStack ( S );
ch = getchar ( );
while ( ch != EOF )
{
while ( ch != EOF && ch != ' \n ' )
{
switch ( ch )
{
case ' # ':
S.Pop ( S , c );
break;
case ' @ ':
S.ClearStack ( S );
break;
default:
S.Push ( S , ch );
break;
}
ch = getchar ( );
}
char * p = S.base;
while ( p < S.top )
{
cout << * p;
p ++;
}
S.ClearStack ( S );
if ( ch != EOF )
ch = getchar ( );
}
S.DestroyStack ( S );
}
int main ( )
{
SqStack S;
LineEdit ( S );
return 0;
}
用栈实现的行编辑器,遇#退一格,遇@退一行。