C++中缀表达式建立表达式树

这段代码实现了一个中缀表达式解析器,用于构建表达式树并计算其值。它处理了括号、加减乘除、指数等运算符,并支持变量、参数和随机数。此外,还包括了对游戏客户端对象属性的查询功能。

//算符优先级
typedef struct _Priority
{
    char op;
    int isp;
    int icp;
}Priority;

const int symbols = 8;
static Priority pri_table[symbols] =
{
    {'#', 0, 0},
    {'+', 3, 2},
    {'-', 3, 2},
    {'*', 5, 4},
    {'/', 5, 4},
    {'^', 6, 7},
    {'(', 1, 8},
    {')', 8, 1}
};
inline bool isDigit(char ch)
{
    return ch >= '0' && ch <= '9';
}
inline bool opNumber(char ch)
{
    return (( ch >= '0' && ch <= '9' || ch == '.')  ||
            ( ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '&' ) || (ch == 'R' || ch == 'P'));
}
inline bool isLetterUnderline(char ch)
{
    return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
}
inline bool FloatEqualZero(float f)
{
    return (f >= -FLT_EPSILON && f <= FLT_EPSILON);
}
bool isDigitStr(const char * str , float &val)
{
    bool bRet = false;
    val = 0.00f;
    if (str == NULL)
    {
        return bRet;
    }
    size_t len = strnlen(str,MAX_EXP);
    if (len >= MAX_EXP)
    {
        return bRet;
    }
    float hasDot = false;
    float w = 1;
    for (int i=0; i<(int)len; ++i)
    {
        if (isDigit(str[i]) || str[i] == '.')
        {
            if(str[i] == '.')
            {
                if(hasDot)
                {
                    bRet =false;
                    break;
                }
                hasDot = true;
            }
            else
            {
                if(hasDot)
                {
                    w *= 0.1f;
                    val += (str[i] - '0') * w;
                }
                else
                {
                    val = val * 10 + str[i] - '0';
                }
            }
        }
        else
        {
            bRet = false;
            break;
        }
    }
    return bRet;
}
typedef enum _NodeType
{
    NODE_TYPE_OP,
    NODE_TYPE_NUM,
    NODE_TYPE_VAR,
    NODE_TYPE_EXPAND_VAR,
    NODE_TYPE_PARAM,
    NODE_TYPE_RANDOM
}NodeType;


//中缀表达式建树
bool ExpTree::CreateExpTree(const char * str)
{
    bool b = scanNode(str);
    if (!b)
    {
        return false;
    }
    std::stack<ExpNode*> node_stack;
    std::stack<ExpNode*> op_stack;
    ExpNode temp_node;
    temp_node.type = NODE_TYPE_OP;
    temp_node.op = '#';
    op_stack.push(&temp_node);
    for (int i = 0; i < count; i++)
    {
        if (nodes[i].type == NODE_TYPE_NUM || nodes[i].type == NODE_TYPE_VAR ||
            nodes[i].type == NODE_TYPE_PARAM || nodes[i].type == NODE_TYPE_RANDOM)
        {
            nodes[i].left = NULL;
            nodes[i].right = NULL;
            node_stack.push(&nodes[i]);
        }
        else if (nodes[i].type == NODE_TYPE_OP)
        {
            switch (nodes[i].op)
            {
            case '(':
                {
                    op_stack.push(&nodes[i]);
                }
                break;
            case ')':
                {
                    while (op_stack.top()->op != '(')
                    {
                        ExpNode *op = op_stack.top();
                        op_stack.pop();
                        ExpNode *right = node_stack.top();
                        node_stack.pop();
                        ExpNode *left = node_stack.top();
                        node_stack.pop();
                        op->left = left;
                        op->right = right;
                        node_stack.push(op);
                    }
                    if (op_stack.empty())
                    {
                        return false;
                    }
                    op_stack.pop(); //弹出栈顶左括号
                }
                break;
            case '+':
            case '-':
            case '*':
            case '/':
            case '^':
                {
                    while (cmpSymbolPri(op_stack.top()->op, nodes[i].op) >= 0)
                    {
                        ExpNode *op = op_stack.top();
                        op_stack.pop();
                        ExpNode *right = node_stack.top();
                        node_stack.pop();
                        ExpNode *left = node_stack.top();
                        node_stack.pop();
                        op->left = left;
                        op->right = right;
                       &nbs

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

meslog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值