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();
}