我想在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
Run Code Online (Sandbox Code Playgroud)
这是我的代码到目前为止的样子:
template<class operand_type>
class ExprTree
{
public:
ExprTree(const char op_, operand_type& operand1_, operand_type& operand2_)
{
op = op_; …Run Code Online (Sandbox Code Playgroud)