本文整理汇总了C++中ExpressionNode::replace方法的典型用法代码示例。如果您正苦于以下问题:C++ ExpressionNode::replace方法的具体用法?C++ ExpressionNode::replace怎么用?C++ ExpressionNode::replace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionNode
的用法示例。
在下文中一共展示了ExpressionNode::replace方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: remove
void ExpressionNode::remove(ExpressionNode& target)
{
ExpressionNode * parent = findParentOf(target);
if (this == &target)
{
throw EmptyTreeError();
}
else
{
assert(parent!=0);
if (parent->getOperation()->getArity() == 1)
{
remove(*parent);
}
else
{
target.replace(ExpressionNode(parent->getOperation()->getIdentity()));
(*parent).simplify();
}
}
}
示例2: addTerms
//.........这里部分代码省略.........
{
coefficient1 = MULTIPLICATION.getIdentity();
varpart1 = &term1;
}
}
else
{
coefficient1 = MULTIPLICATION.getIdentity();
varpart1 = &term1;
}
}
else // VARIABLE
{
assert(term1.getType() == VARIABLE);
coefficient1 = MULTIPLICATION.getIdentity();
varpart1 = &term1;
}
if (term2.getType() == NUMBER)
{
coefficient2 = term2.getValue();
varpart2 = 0;
}
else if (term2.getType() == OPERATION)
{
if (term2.getOperation() == &MULTIPLICATION)
{
left = term2.getFirstChild();
right = left->getRight();
if (left->getType() == NUMBER)
{
coefficient2 = left->getValue();
varpart2 = right;
}
else if (right->getType() == NUMBER)
{
coefficient2 = right->getValue();
varpart2 = left;
}
else
{
coefficient2 = MULTIPLICATION.getIdentity();
varpart2 = &term2;
}
}
else
{
coefficient2 = MULTIPLICATION.getIdentity();
varpart2 = &term2;
}
}
else // VARIABLE
{
assert(term2.getType() == VARIABLE);
coefficient2 = MULTIPLICATION.getIdentity();
varpart2 = &term1;
}
if (varpart1 != 0 && varpart2 != 0)
{
if (*varpart1 == *varpart2)
{
std::clog << "addTerms simplify like terms" << std::endl
<< "coef1: " << coefficient1 << " var1: " << *varpart1 << std::endl
<< "coef2: " << coefficient2 << " var2: " << *varpart2 << std::endl;
newCoef = ExpressionNode(coefficient1 + coefficient2);
newRight.replace(*varpart1);
newNode = ExpressionNode(&MULTIPLICATION);
newNode.setFirstChild(&newCoef);
newNode.getFirstChild()->setRight(&newRight);
std::clog << "sum: " << newNode << std::endl;
return newNode;
}
else
{
throw ExpressionNode::GenericError("adding unaddable terms");
}
}
else
{
if (varpart1 == 0 && varpart2 == 0)
{
std::clog << "sum: " << coefficient1 + coefficient2 << std::endl;
return ExpressionNode(coefficient1 + coefficient2);
}
else
{
throw ExpressionNode::GenericError("adding unaddable terms");
}
}
throw ExpressionNode::GenericError("reached end of addTerms w/o returning.");
//if (left.getNodeType() == NUMBER && right.getNodeType() == NUMBER)
//{
//ExpressionNode newNode;
//newNode.setNodeType(NUMBER);
//newNode.setValue(Number(left.getValue().getInt() + right.getValue().getInt()));
//return newNode;
//}
}