c++ - 表达式树数据结构

标签 c++ class tree expression

我想在 C++ 中实现一个简单的算术表达式树数据结构,这样一个表达式树对象被初始化为:ExprTree(operator, expression1, expression2)。这是它应该如何工作的示例:

double x = 1, y = 2, z = 0.5;
expr1 = ExprTree('*', x, y); // expr1 = 1 * 2 = 2
expr2 = ExprTree('-', expr1, z); // expr2 = (1 * 2) - 0.5 = 1.5
cout << expr2.str() << endl; // ((1 * 2) - 0.5)
cout << expr2.eval() << endl; // 1.5

到目前为止,我的代码是这样的:

template<class operand_type>
class ExprTree
{
public:
    ExprTree(const char op_, operand_type& operand1_, operand_type& operand2_)
    {
        op = op_;
        operand1 = operand1_;
        operand2 = operand2_;
    }
    double eval() const;
    std::string str() const;
private:
    char op;
    typename operand_type operand1, operand2;
};

template<class operand_type>
std::string ExprTree<operand_type>::str() const
{
    std::ostringstream os;
    std::string op1, op2;
    if (typeid(*operand1) == typeid(ExprTree))
        op1 = operand1->str();
    else
        op1 = std::string(*operand1);
    if (typeid(*operand2) == typeid(ExprTree))
        op2 = operand1->str();
    else
        op2 = std::string(*operand2);
    os << "(" << op1 << " " << op << " " << op2 << ")";
    return os.str();
}

但是,当我编译代码时出现这个错误:

left of '->write' must point to class/struct/union/generic type

如果有人能帮助我解决这个错误并可能提供一些关于我应该如何实现这个数据结构的提示,我将不胜感激。顺便说一句,我是 C++ 的新手。

最佳答案

您的代码中存在许多问题:

  1. 您在成员变量operand1operand2 上使用指针成员 -> 运算符

  2. 您需要在模板参数中使用两种不同类型的参数来初始化具有不同参数类型的对象。

  3. 类/构造函数不像函数那样自动检测类型。这意味着您不能执行类似 ExprTree('*', x, y); 的操作。您可以指定模板参数或使用额外的模板函数来构造 ExprTree 模板类的对象。看这个answer .

  4. if (typeid(*operand1) == typeid(ExprTree)) 在运行时求值,因此您将遇到编译错误,因为您尝试调用方法 str () 并将同一对象传递给 std::string

我更喜欢以下解决方案:

#include <string>
#include <iostream>
#include <sstream>

template<typename operand_type_A, typename operand_type_B>
class ExprTree 
{
public:
    ExprTree(){};
    ExprTree(const char op_, const operand_type_A& operand1_, const operand_type_B& operand2_) {
        op = op_;
        operand1 = operand1_;
        operand2 = operand2_;
    };
    double eval() const;
    std::string str() const;

private:
    char op;
    operand_type_A operand1;
    operand_type_B operand2;
};

template<typename operand_type_A, typename operand_type_B>
ExprTree<operand_type_A, operand_type_B> makeExpr(const char op, const operand_type_A& operand1, const operand_type_B& operand2)
{
    return ExprTree<operand_type_A, operand_type_B>(op, operand1, operand2);
}

template<typename T>
std::string ToString(const T& x)
{
    return x.str();
}

template<>
std::string ToString<double>(const double& x)
{
    return std::to_string(x);
}

template<typename operand_type_A, typename operand_type_B>
std::string ExprTree<operand_type_A, operand_type_B>::str() const {
    std::ostringstream os;
    std::string op1, op2;
    op1 = ToString(operand1);
    op2 = ToString(operand2);
    os << "(" << op1 << " " << op << " " << op2 << ")";
    return os.str();
}

int main()
{
    double x = 1, y = 2, z = 0.5;
    std::cout << makeExpr('-', makeExpr('*', x, y), z).str() << std::endl;
    return 0;
}

它输出以下字符串:

((1.000000 * 2.000000) - 0.500000)

你可以试试here .

关于c++ - 表达式树数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30264450/

相关文章:

c# - 在类和方法之间共享变量

c++ - 如何将字符串添加到 vector (并随后显示它)?

algorithm - 我可以将什么算法应用于此 DAG?

recursion - Coq新手: How to iterate trough binary-tree in Coq

c++ - 为什么我的 std::atomic<int> 变量不是线程安全的?

c++ - b2 的命令参数,以便使用 Microsoft 的 Clang/C2 构建 Boost 库

C++:从对象指针 vector 中删除元素而不删除它指向的对象的正确方法是什么?

c++ - 'PolishStack' 不是类模板,虚函数幽灵错误

java - 如何将修改后的预序树遍历数据填充到 Java 树对象中?

c++ - C2070 - 非法大小的操作数