//算符优先级
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

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

被折叠的 条评论
为什么被折叠?



