读书人

大家帮小弟我看看这个函数是咋回事(中

发布时间: 2012-03-24 14:00:47 作者: rapoo

大家帮我看看这个函数是怎么回事(中缀表达式的)
我要把中缀表达式换成后缀表达式
菜鸟 初学c++
下了一个网上的例子,可是弄完后有些不懂
现发出来请大家帮我分析分析
class Stack
{
private:
char stacklist[100];
int top;
public:
Stack();
char getMax() const;
void Push(const T& item);
char Pop();
void ClearStack();
int StackEmpty() const;
int StackFull() const;
};
这是类的定义
其中有一个函数

Stack::StackEmpty() const
{
return top==-1;
};
这个函数放在这儿是什么意思啊
后面用到的地方很多
while(op!= '= ')
{
switch(op)
{
case '( ':
run();
break;
case ') ':
goto sto;
case '+ ':
case '- ':
if(s.StackEmpty()==0)//比如这个地方是什么用意呢 StackEnpty()
{ //又没有变 返回的是-1嘛 这个怎么会成立哦
// 大家给我解释下哈

if (s.getMax()== '+ '||s.getMax()== '- ')
{
op1=s.Pop();
cout < <op1;
s.Push(op);
}
}
else
{
s.Push(op);
}
break;

...
} //end switch
sto: while(s.StackEmpty()!=1)
{
op1=s.Pop();
cout < <op1;
}

[解决办法]
表达式top==-1的值要么是0或者是1。。。
所以函数StackEmpty()返回的是0或者1。。
[解决办法]
注意是return top==-1;
而不是return top=-1;

top==-1;是个判断条件,所以StackEmpty()就只会返回0或者1

读书人网 >C++

热点推荐