表达树数据结构
Expression tree data structure
本文关键字:数据结构 更新时间:2023-10-16
我想在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 的新手。
您的代码中有许多问题:
-
您在成员变量上使用指针的成员 -> 操作员 operand1 and Operand2
-
您需要模板参数中的两种不同类型的类型来使用不同的参数类型初始化对象。
-
类/构造函数不像功能这样的自动检索类型。这意味着您无法做
ExprTree('*', x, y);
之类的事情。您的Ether指定模板参数或使用其他模板函数来构建 exprtree 模板类的对象。看到这个答案。 -
if (typeid(*operand1) == typeid(ExprTree))
在运行时进行评估,因此您将获得汇编错误,因为您尝试调用方法 str()并将同一对象传递给 std :: string 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)
您可以在这里尝试。
说:
operand1->str();
您应该说:
operand1.str();
因为operand1
不是指针,而是成员变量。
错误消息
' -> str'的左侧必须指向class/struct/union/ension类型
基本上说操作员->
的左侧必须是指针(不是)。(还说它必须指向一个或类似的人,而不是整数)。
相关文章:
- 链表,反向函数,数据结构
- 如何使用set实现无序数据结构?
- 我们可以将数据永久保存为数据结构吗?
- C++中的可变长度数组/数据结构
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 通过 NIF 从C++返回自定义数据结构
- 编译器上的策略数据结构不起作用
- 尝试构建"lock-free"数据结构C++
- 设计将引用元素移动到开头的数据结构.C++
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何解析表示树状数据结构的字符串
- 我对数据结构、双向链表有一些问题
- googletest:测试太大的数据结构
- C++中deque数据结构的大O是什么?
- 我可以使用哪种数据结构来处理这种方式
- 将文本文件解析为树状数据结构
- C++ 中具有 O(1) 搜索时间复杂度的数据结构
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- C++,您能否设计一种数据结构,将指针保存在连续内存中并且不会使它们失效?
- 带参数的数据结构的全局声明