C++之面向对象版表达式计算器(一)

1、常规

Node.h

class Node {
public:
    virtual double Calc() const = 0;
    virtual ~Node() {};
};

class NumberNode : public Node
{
public:
    NumberNode(double number) : number_(number)
    {

    }
    double Calc() const;
private:
    const double number_;
};

class BinaryNode : public Node
{
public:
    BinaryNode(Node* left, Node* right):left_(left), right_(right)
    {}

    ~BinaryNode();

protected:
    Node* const left_;  // const在*右边,表示指针不能再指向别的节点了
    Node* const right_;
};

// 加法
class AddNode : public BinaryNode
{
    AddNode(Node* left, Node* right) : BinaryNode(left, right)
    {
    }

    double Calc() const;
};

// 减法
class SubNode : public BinaryNode
{
    SubNode(Node* left, Node* right) : BinaryNode(left, right)
    {
    }

    double Calc() const;
};

// 乘法
class MultiplyNode : public BinaryNode
{
    MultiplyNode(Node* left, Node* right) : BinaryNode(left, right)
    {
    }

    double Calc() const;
};

// 除法
class DivideNode : public BinaryNode
{
    DivideNode(Node* left, Node* right) : BinaryNode(left, right)
    {
    }

    double Calc() const;
};

Node.cpp

#include "Node.h"
#include <cmath>

#include "iostream"
using namespace std;

double NumberNode::Calc() const {
    return number_;
}

double AddNode::Calc() const {
    return left_->Calc() + right_->Calc();
}

BinaryNode::~BinaryNode() noexcept {
    delete left_;
    delete right_;
}

double SubNode::Calc() const {
    return left_->Calc() - right_->Calc();
}

double MultiplyNode::Calc() const {
    return left_->Calc() * right_->Calc();
}

double DivideNode::Calc() const {
    double divisor = right_->Calc();
    if(divisor != 0.0)
    {
        return left_->Calc() / divisor;
    }

    cout << "Error: divisor by zero" << endl;
    return HUGE_VAL;
}

2、带有复数的操作

Node.h

class Node {
public:
    virtual double Calc() const = 0;
    virtual ~Node() {};
};

class NumberNode : public Node
{
public:
    NumberNode(double number) : number_(number)
    {

    }
    double Calc() const;
private:
    const double number_;
};

class BinaryNode : public Node
{
public:
    BinaryNode(Node* left, Node* right):left_(left), right_(right)
    {}

    ~BinaryNode();

protected:
    Node* const left_;  // const在*右边,表示指针不能再指向别的节点了
    Node* const right_;
};

class UnaryNode : public Node
{
public:
    UnaryNode(Node* child):child_(child)
    {

    }
    ~UnaryNode();

protected:
    Node* const child_;
};

class UMinusNode : public UnaryNode
{
public:
    UMinusNode(Node* child): UnaryNode(child)
    {

    }
    double Calc() const;
};

// 加法
class AddNode : public BinaryNode
{
    AddNode(Node* left, Node* right) : BinaryNode(left, right)
    {
    }

    double Calc() const;
};

// 减法
class SubNode : public BinaryNode
{
    SubNode(Node* left, Node* right) : BinaryNode(left, right)
    {
    }

    double Calc() const;
};

// 乘法
class MultiplyNode : public BinaryNode
{
    MultiplyNode(Node* left, Node* right) : BinaryNode(left, right)
    {
    }

    double Calc() const;
};

// 除法
class DivideNode : public BinaryNode
{
    DivideNode(Node* left, Node* right) : BinaryNode(left, right)
    {
    }

    double Calc() const;
};

Node.cpp

#include "Node.h"
#include <cmath>

#include "iostream"
using namespace std;

double NumberNode::Calc() const {
    return number_;
}

double AddNode::Calc() const {
    return left_->Calc() + right_->Calc();
}

BinaryNode::~BinaryNode() noexcept {
    delete left_;
    delete right_;
}

UnaryNode::~UnaryNode() noexcept {
    delete child_;
}

double SubNode::Calc() const {
    return left_->Calc() - right_->Calc();
}

double MultiplyNode::Calc() const {
    return left_->Calc() * right_->Calc();
}

double DivideNode::Calc() const {
    double divisor = right_->Calc();
    if(divisor != 0.0)
    {
        return left_->Calc() / divisor;
    }

    cout << "Error: divisor by zero" << endl;
    return HUGE_VAL;
}

double UMinusNode::Calc() const {
    return -child_->Calc();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值