请高手帮忙,非诚勿扰!!!

无名君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;
}


用栈实现的行编辑器,遇#退一格,遇@退一行。


















...全文
83 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
无名君jn 2009-12-23
  • 打赏
  • 举报
回复
呵呵,俺知道了
无名君jn 2009-12-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 icelemon1314 的回复:]
提问的方式不对哈~~相信没几个人会将恩的代码完全看一遍或自己编译一遍

所以提问要说问题的重点 到底是哪里出问题了

而且楼主的代码都没有任何注释~~这个有点~~~
[/Quote]我的问题是为什麽我输入ab#时,却原样输出呢?而不是输出ab!!!
icelemon1314 2009-12-22
  • 打赏
  • 举报
回复
提问的方式不对哈~~相信没几个人会将恩的代码完全看一遍或自己编译一遍

所以提问要说问题的重点 到底是哪里出问题了

而且楼主的代码都没有任何注释~~这个有点~~~

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧