bhlzlx的专栏

烟台大学 计算机 李新

C++ 沉思录第八章 代码整理,修改(好像书上的代码根本没法用,这只是按他那个思想写的,若发现错误请指正.)

头文件:

#ifndef EXPRESSION_H
#define EXPRESSION_H

#include <iostream>
#include <string>
using namespace std;

class ExpressionNode;

class Expression
{
public:
    friend ostream & operator << (ostream & o ,Expression &expression);
    Expression & operator = (Expression & expression);
    Expression & operator = (const Expression & expression);
    Expression();
    Expression(int n);
    Expression(const string& op,const Expression& opnd);
    Expression(string op,Expression left,Expression right);
        virtual ~Expression();
    protected:
        ExpressionNode * expressionNode;
        string handleType;
    private:
};

class ExpressionNode
{
    friend class Expression;
    friend ostream & operator << (ostream & o , ExpressionNode & node);
    public:
        ExpressionNode();
        virtual ~ExpressionNode();
        virtual void print( ostream & o )=0;
    protected:
        int count;
    private:
};



class BinaryNode :public ExpressionNode
{
    friend class Expression;
    public:
        BinaryNode(const string& oper_,const Expression &left_,const Expression & right_);
        void print( ostream & o);
        virtual ~BinaryNode();
    protected:
        string oper;
        Expression left;
        Expression right;
    private:
};


class IntNode:public ExpressionNode
{
    friend class Expression;
    public:
        IntNode(int k);
        virtual ~IntNode();
        void print( ostream & o);
    protected:
        int n;
    private:
};

class UnaryNode:public ExpressionNode
{
    friend class Expression;
    public:
        UnaryNode(const string & op,const Expression & expression);
        virtual ~UnaryNode();
        void print( ostream & o);
    protected:
        string oper;
        Expression opnd;
    private:
};




#endif // EXPRESSION_H


实现:


/*
** Copyright (C) QPSOFT.COM All rights reserved.
*/

#include "expression.h"

Expression::Expression():expressionNode(0)
{
    //ctor
}

Expression::~Expression()
{
    //dtor
	this->expressionNode->count--;
    cout<<handleType<<"Destruct!"<<endl;
	if(this->expressionNode->count==0)
	{
		cout<<"被delete掉"<<endl;
		delete this->expressionNode;
	}

}

ostream & operator << (ostream & o ,Expression &expression)
{
    o<<*(expression.expressionNode);
    return o;
}

Expression::Expression(int n)
{
    this->expressionNode=new IntNode(n);
    expressionNode->count=1;
    this->handleType="Int Hanle";
}
Expression::Expression(const string& op,const Expression & opnd)
{
    this->expressionNode=new UnaryNode(op,opnd);
    expressionNode->count=1;
    this->handleType="Unary Handle";
}
Expression::Expression(string op,Expression left,Expression right)
{
    this->expressionNode =new BinaryNode(op,left,right);
    expressionNode->count=1;
    this->handleType="Binary Handle";
}

Expression & Expression::operator = (Expression & expression)
{
    expression.expressionNode->count++;
	this->handleType=expression.handleType;
    if(this->expressionNode)
    {
        if((--(this->expressionNode->count))==0)
        {
            delete this->expressionNode;
        }
    }

    cout<<this->handleType<<"赋值一次";
    this->expressionNode=expression.expressionNode;
    return *this;
}

Expression & Expression::operator = (const Expression & expression)
{
    expression.expressionNode->count++;
	this->handleType=expression.handleType;
    if(this->expressionNode)
    {
        if((--(this->expressionNode->count))==0)
        {
            delete this->expressionNode;
        }
    }

    cout<<this->handleType<<"赋值一次";
    this->expressionNode=expression.expressionNode;
    return *this;
}

BinaryNode::~BinaryNode()
{
    //dtor
}

BinaryNode::BinaryNode(const string& oper_,const Expression &left_,const Expression & right_)
{
    this->oper = oper_;
    this->left=  left_;
    this->right = right_;
}

void BinaryNode::print( ostream & o)
{
    o<<'('<<left<<oper<<right<<')';
}

UnaryNode::UnaryNode(const string & op,const Expression & expression)
{
    //ctor
    this->oper = op;
    this->opnd = expression;
}

UnaryNode::~UnaryNode()
{
    //dtor
}

void UnaryNode::print( ostream & o)
{
    o<<'('<<oper<<opnd<<')';
}


IntNode::IntNode(int k)
{
    //ctor
    this->n=k;
}

IntNode::~IntNode()
{
    //dtor
}

void IntNode::print( ostream & o)
{
    o<<this->n;
}


ExpressionNode::ExpressionNode()
{
    //ctor
    cout<<"constructor"<<endl;
}

ExpressionNode::~ExpressionNode()
{
    //dtor
}

ostream & operator << (ostream & o ,ExpressionNode & node)
{
    node.print(o);
    return o;
}

main:

#include <iostream>
#include <string>
#include "expression.h"
using namespace std;

void test()
{
	Expression exp("*",Expression("+",3),Expression("-",7,9));
	cout<<exp;
}

int main()
{
   // Expression *exp=new Expression("*",2)
   test();
    return 0;
}





阅读更多
个人分类: C++ 技术&思想
想对作者说点什么? 我来说一句

C++沉思录完整版 pdf

2008年01月08日 7.46MB 下载

C++沉思C++沉思

2010年02月21日 7.46MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭