二进制表达式树c++构建函数

Binary Expression Tree c++ Build Function

本文关键字:构建 函数 c++ 表达式 二进制      更新时间:2023-10-16

所以我要上一堂计算机科学课,我们必须构建一个二进制表达式树,这是我目前所拥有的:

void buildtree(string str)
{
    string tmp;
    stack<char> opstack;
    stack<BTNP> treeStack;
    double value;
    int i = 0;
    while(i < str.length)
    {
        while(isspace(str[i++]))
        {
            if(str[i] == '(')
            {
                opstack.push(str[i]);
            }
            else if(isdigit(str[i] || str[i] == '*'))
            {
                tmp.clear();
                while(isdigit(str[i]) || str[i] == '+')
                {
                    tmp =+ str[i++];
                }
            }
        }
    }
    BTNP ptr= new BTN;
    ptr->flag = false;
    ptr->value = value;
    ptr->left = NULL;
    ptr->right = NULL;
    treeStack.push(ptr);
}

这是我造树的功能。我的教授已经把它写在黑板上了,我们不得不对它进行一些编辑,但我在BTNP部分不断出错。

stack<BTNP> treestack;

我一直在到处搜索示例代码,看看如何构建表达式树,但没有找到任何结果。即使我没有得到回应,一个链接也会很好。

我的错误:

BTNP is not identified.

while(i < str.length())得到3个不同的错误,例如:

Error   3   error C2297: '<' : illegal, right operand has type 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Alloc>::* )(void) throw() const'  
Error   2   error C2446: '<' : no conversion from 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Alloc>::* )(void) throw() const' to 'int'  
Error   1   error C3867: 'std::basic_string<_Elem,_Traits,_Alloc>::length': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Alloc>::length' to create a pointer to member   
    4   IntelliSense: no instance of overloaded function "std::stack<_Ty, _Container>::push [with _Ty=BET::BTN, _Container=std::deque<BET::BTN, std::allocator<BET::BTN>>]" matches the argument list
            argument types are: (BET::BTN *)
            object type is: std::stack<BET::BTN, std::deque<BET::BTN, s

我得到的最后一个错误是treeStack.push(ptr(;

3 IntelliSense:重载函数"std::stack<_Ty,_Container>::push[with_Ty=BET::BTN,_Container=std::deque>]"的实例与参数列表不匹配参数类型为:(BET::BTN*(对象类型为:std::stack>>

错误2错误C2664:"void std::stack&lt_Ty>::push(BET::BTN&(':无法将参数1从"BET::BTN*"转换为"BET:&'

树的代码,或者我认为有些相关。。

struct BTN
{
    bool flag;
    char op;
    double value;
    BTN * left;
    BTN * right;
    BTN * BTNP;
};
// ...snip
int i = 0;
while(i < str.length)
{
    // etc...

假设这段代码被复制并粘贴到问题中,那么str.length后面的括号就不见了(尽管稍后在注释中已经包含了这些括号(。

因此,实际上并没有调用该函数,编译器抱怨(非常正确(它不知道如何告诉您成员函数是否大于int

将代码更改为str.length(),这将有助于您进一步了解:-(