【发布时间】:2019-12-01 02:23:25
【问题描述】:
while (temp->left->oper == '+' ||
temp->left->oper == '-' ||
temp->left->oper == '*' ||
temp->left->oper == '/' ||
temp->right->oper == '+' ||
temp->right->oper == '-' ||
temp->right->oper == '*' ||
temp->right->oper == '/')
{
// do something
}
为了清楚起见:temp 是一个指向下面的node 结构的指针:
struct node
{
int num;
char oper;
node* left;
node* right;
};
【问题讨论】:
-
在不知道
temp->left和temp->right之间的依赖关系的情况下,您无法在所有相等的运算符中进行优化。从视觉上看,您可以使用正则表达式,但在内部,它可能几乎相同,甚至效率更低。 -
我很想知道你为什么认为你有这个问题。它有点像表达式树的运行时解释,如果是这样的话,还有更好的方法来做到这一点
标签: c++ algorithm if-statement while-loop simplify