计算器的实现比较复杂。不是几句话可以解决。
大致用以下步骤:(二叉数法)
建立父类:class node:
建立子类:class leafnode(用于存储数值)
建立子类:class Optnode(用于运算)
再实现一个类用实现数的建立

解决方案 »

  1.   

    #include "math.h"
    class VarBinNode : public CObject  
    {
    public:
    short grade;
    virtual double value(short &result); VarBinNode();
    virtual ~VarBinNode();
    virtual BOOL isLeaf();
    };
    ///////////////////////                 LeafNode
    class LeafNode : public VarBinNode
    {
    public:
    double value(short &result );
    double leaf;
    LeafNode();
    virtual ~LeafNode();
    virtual BOOL isLeaf(){return TRUE;};
    };
    ///////////////////////                 IntlNode
    class IntlNode : public VarBinNode
    {
    public:
    double value(short &result);
    short optor;
    VarBinNode *left;
    VarBinNode *right; IntlNode();
    virtual ~IntlNode();
    virtual BOOL isLeaf(){return FALSE;};
    };
      

  2.   

    BOOL VarBinNode::isLeaf()
    {return FALSE;}
    double VarBinNode::value(short &result)
    { return 0; }
    //////////////               leafNode
    LeafNode::LeafNode()
    {
    }
    LeafNode::~LeafNode()
    {}
    double LeafNode::value(short &result)
    {
    result = 0;
    return leaf;
    }
    ///////////////////////////  IntlNode
    IntlNode::IntlNode()
    {
    left= NULL;
    right=NULL;
    optor = 0;
    }
    IntlNode::~IntlNode()
    {
    if(left != NULL)
    {
    delete left;
    }
    if(right != NULL)
    {
    delete right;
    }
    }
    double IntlNode::value(short &result)
    {
    short lresult,rresult;
    double lvar,rvar;
    BOOL isLeaf lvar = left->value(lresult);
    rvar = right->value(rresult);
    switch (optor)    //1+ 2- 3* 4/ 5sin 6cos 
    {
    case 1:    //+
    {
    //左右分支是否有值
    if((lresult != 0)&&(rresult != 0))
    {
    result = 3;
    return 0;
    }
    if(lresult != 0)
    {
    result = 0;
    lvar = 0;
    // return 0;
    }
    if(rresult != 0)
    {
    result =2;
    return 0;
    } result = 0;
    return (lvar+rvar);
    }
    case 2:  ///// -
    {
    //左右分支是否有值
    if((lresult != 0)&&(rresult != 0))
    {
    result = 3;
    return 0;
    }
    if(lresult != 0)
    {
    // result = 0;
    lvar = 0;
    }
    if(rresult != 0)
    {
    result =2;
    return 0;
    } result = 0;
    return (lvar-rvar);
    }
    case 3://       *
    {
    //左右分支是否有值
    if((lresult != 0)&&(rresult != 0))
    {
    result = 3;
    return 0;
    }
    if(lresult != 0)
    {
    result = 1;
    return 0;
    }
    if(rresult != 0)
    {
    result =2;
    return 0;
    }
    result =0;
    return (lvar*rvar);
    }
    case 4:  ///  /
    {
    //左右分支是否有值
    if((lresult != 0)&&(rresult != 0))
    {
    result = 3;
    return 0;
    }
    if(lresult != 0)
    {
    result = 1;
    return 0;
    }
    if(rresult != 0)
    {
    result =2;
    return 0;
    } if(rvar != 0)
    {
    result = 0;
    return (lvar/rvar);
    }
    else
    {
    result = 4;
    return 0;
    }
    }
    case 5:  ///////  sin
    {
    //右分支是否有值
    if(rresult != 0)
    {
    result =2;
    return 0;
    }
    else
    {
    result = 0;
    return sin(rvar);
    }
    }
    case 6:   // cos
    {
    //右分支是否有值
    if(rresult != 0)
    {
    result =2;
    return 0;
    }
    else
    {
    result = 0;
    return cos(rvar);
    }
    }
    case 7:  // tan
    {
    //右分支是否有值
    if(rresult != 0)
    {
    result =2;
    return 0;
    }
    else
    {
    result = 0;
    return tan(rvar);
    }
    }
    case 8:  //arcsin
    {
    //右分支是否有值
    if(rresult != 0)
    {
    result =2;
    return 0;
    }
    else
    {
    if((rvar>=-1)&&(rvar<=1))
    {
    result = 0;
    return asin(rvar);
    }
    else
    {
    result = 4;
    return 0;
    }
    }
    }
    case 9:  //arccos
    {
    //右分支是否有值
    if(rresult != 0)
    {
    result =2;
    return 0;
    }
    else
    {
    if((rvar>=-1)&&(rvar<=1))
    {
    result = 0;
    return acos(rvar);
    }
    else
    {
    result =4;
    return 0;
    }
    }
    }
    case 10://arctan
    {
    //右分支是否有值
    if(rresult != 0)
    {
    result =2;
    return 0;
    }
    else
    {
    result = 0;
    return atan(rvar);
    }
    }
    case 11://x的y 次方
    {
    //左右分支是否有值
    if((lresult != 0)&&(rresult != 0))
    {
    result = 3;
    return 0;
    }
    if(lresult != 0)
    {
    result = 1;
    return 0;
    }
    if(rresult != 0)
    {
    result =2;
    return 0;
    } result = 0;
    return pow(lvar,rvar);
    }
    case 12:
    {
    //右分支是否有值
    if(rresult != 0)
    {
    result =2;
    return 0;
    }

    result = 0;
    return exp(rvar);
    }
    case 13: //ln
    {
    if(rresult != 0)
    {
    result =2;
    return 0;
    }

    result = 0;
    return log(rvar);
    }
    case 14://  平方根
    {
    if(rresult != 0)
    {
    result =2;
    return 0;
    }
    if(rvar>=0)
    {
    result = 0;
    return sqrt(rvar);
    }
    else
    {
    result =4;
    return 0;
    }
    }
    case 15://  |a-b|
    {
    if(rresult != 0)
    {
    result =2;
    return 0;
    }

    result = 0;
    return fabs(rvar);
    }

    default:
    {
    result = 5;
    return 0;
    }
    }}