请教一个面向对象范例的问题

kingbox 2005-10-20 12:47:47
这是c++沉思录里的一个例子,
程序描述的内容是用来表示算术表达式的树
调试的过程中出现一大堆的问题,弄得我蛮郁闷,
请教高手帮忙找哈错误
源代码如下:
#include < iostream >
#include < string >

using namespace std;

class Expr;
/////////////////// define the base class//////////////////////////
class Expr_node{
friend ostream& operator << ( ostream&, const Expr_node& );
friend class Expr;

int use;

protected:
Expr_node():use(1){}

virtual int eval()const = 0;
virtual void print ( ostream& )const = 0;
virtual ~Expr_node ( ){}
};

ostream& operator << ( ostream& os,const Expr_node& en )
{
en.print (os);
return os;
}

//////////////derived class/////////////////////////////
class Int_node : public Expr_node {
friend class Expr;

int n;

Int_node ( int k ): n(k) { }
int eval()const { return n;}
void print ( ostream& os)const { os<<n;}
};

class Unary_node : public Expr_node {
friend class Expr;
string op;
Expr opnd;

Unary_node ( const string& s, Expr b ):
op (s), opnd(b) { }
int eval()const;
void print ( ostream& os) const { os<<"("<<op<< opnd<<")";}
};

int Unary_node::eval()const
{
if ( op =='-')
return -opnd.eval();
throw "error,bad op "+op+"int UnaryNode";
}


class Binary_node:public Expr_node {
friend class Expr;
string op;
Expr left;
Expr right;

Binary_node ( const string& a, Expr b,Expr c ):
op (a), left (b),right(c) {}
int eval()const;
void print ( ostream& os ) const { os<<"("<<left<<op<<right<<")";}
};

int Binary_node::eval()const
{
int op1 = left.eval();
int op2 = right.eval();

if ( op == "-" ) return op1-op2;
if ( op == "+" ) return op1+op2;
if ( op == "*" ) return op1*op2;
if ( op == "/" && op2 != 0 ) return op1/op2;

throw "error, bad op" + op + " in BinaryNode";
}
///////////////////////////////////////////
class Expr {
friend ostream& operator << ( ostream&, const Expr& );
friend class Expr_node;
Expr_node *p;
public:
Expr ( int n ) { p = new Int_node ( n );} //creat the Int_node
Expr ( const string& op, Expr t ) { p=new Unary_node(op,t);}//creat the unary_node
Expr ( const string& op , Expr r, Expr l){ p = new Binary_node ( op, r,l );}
//creat the binary_node


Expr ( const Expr & );
Expr& operator = ( const Expr & );

int eval()const { return p->eval(); }

~Expr ( ) { if ( --p->use ==0)delete p; }
};

ostream& operator << ( ostream& os, const Expr& rhs)
{
rhs.p->print ( os);
return os;
}

Expr::Expr ( const Expr & rhs)
{
p=rhs.p;
++p->use;
}

Expr& Expr::operator = ( const Expr & rhs)
{
rhs.p->use ++;
if ( --p->use == 0 )
delete p;

p = rhs.p;
return *this;
}
/////////////////////////////////////////////////////////////////////////////////

int main ( )
{
Expr t = Expr("*", Expr("-",5),Expr("+",3,4) );
cout <<t<<"="<<t.eval()<<endl;
t = Expr ( "*",t,t);
cout<<t<<"="<<t.eval()<<endl;
}
...全文
159 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qhfu 2005-10-21
  • 打赏
  • 举报
回复
帮你改了, 可运行。
qhfu 2005-10-21
  • 打赏
  • 举报
回复
#include < iostream >
#include < string >

using namespace std;

class Expr;

class Expr {
friend ostream& operator << ( ostream&, const Expr& );
friend class Expr_node;
Expr_node *p;
public:
Expr ( int n );
Expr ( const string& op, Expr t );
Expr ( const string& op , Expr r, Expr l);
//creat the binary_node


Expr ( const Expr & );
Expr& operator = ( const Expr & );

int eval()const;

~Expr ( ) ;
};

/////////////////// define the base class//////////////////////////
class Expr_node{
friend ostream& operator << ( ostream& os, const Expr& rhs);
friend ostream& operator << ( ostream&, const Expr_node& );
friend class Expr;

int use;

protected:
Expr_node():use(1){}

virtual int eval()const = 0;
virtual void print ( ostream& )const = 0;
virtual ~Expr_node ( ){}
};

ostream& operator << ( ostream& os,const Expr_node& en )
{
en.print (os);
return os;
}

//////////////derived class/////////////////////////////
class Int_node : public Expr_node {
friend class Expr;

int n;

Int_node ( int k ): n(k) { }
int eval()const { return n;}
void print ( ostream& os)const { os<<n;}
};

class Unary_node : public Expr_node {
friend class Expr;
string op;
Expr opnd;

Unary_node ( const string& s, Expr b ):
op (s), opnd(b) { }
int eval()const;
void print ( ostream& os) const { os<<"("<<op<< opnd<<")";}
};

int Unary_node::eval()const
{
if ( op == "-")
return -opnd.eval();
throw "error,bad op "+op+"int UnaryNode";
}


class Binary_node:public Expr_node {
friend class Expr;
string op;
Expr left;
Expr right;

Binary_node ( const string& a, Expr b,Expr c ):
op (a), left (b),right(c) {}
int eval()const;
void print ( ostream& os ) const { os<<"("<<left<<op<<right<<")";}
};

int Binary_node::eval()const
{
int op1 = left.eval();
int op2 = right.eval();

if ( op == "-" ) return op1-op2;
if ( op == "+" ) return op1+op2;
if ( op == "*" ) return op1*op2;
if ( op == "/" && op2 != 0 ) return op1/op2;

throw "error, bad op" + op + " in BinaryNode";
}
///////////////////////////////////////////

ostream& operator << ( ostream& os, const Expr& rhs)
{
rhs.p->print ( os);
return os;
}

Expr::Expr ( const Expr & rhs)
{
p=rhs.p;
++p->use;
}

Expr& Expr::operator = ( const Expr & rhs)
{
rhs.p->use ++;
if ( --p->use == 0 )
delete p;

p = rhs.p;
return *this;
}
Expr::Expr ( int n ) { p = new Int_node ( n );} //creat the Int_node
Expr::Expr ( const string& op, Expr t ) { p=new Unary_node(op,t);}//creat the unary_node
Expr::Expr ( const string& op , Expr r, Expr l){ p = new Binary_node ( op, r,l );}
int Expr::eval()const { return p->eval(); }
Expr::~Expr ( ) { if ( --p->use ==0)delete p; }
hello_jun 2005-10-20
  • 打赏
  • 举报
回复
好长啊!

3,882

社区成员

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

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