当前位置: 首页>>代码示例>>C++>>正文


C++ ExpressionNode::getOperation方法代码示例

本文整理汇总了C++中ExpressionNode::getOperation方法的典型用法代码示例。如果您正苦于以下问题:C++ ExpressionNode::getOperation方法的具体用法?C++ ExpressionNode::getOperation怎么用?C++ ExpressionNode::getOperation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ExpressionNode的用法示例。


在下文中一共展示了ExpressionNode::getOperation方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: operation

ExpressionNode::ExpressionNode(const ExpressionNode & other): //copy constructor
type(other.getType()), operation(other.getOperation()), activechildren(other.getOperation()->getArity()),
right(0), firstChild(0), variable(other.getVariable()), value(other.getValue())
{
	setRight(other.getRight());
	setFirstChild(other.getFirstChild()); // automatically also sets other children
}
开发者ID:vk-eipi,项目名称:MathParser,代码行数:7,代码来源:expression.cpp

示例2: replace

void ExpressionNode::replace(const ExpressionNode &newNode)
{
	std::clog << "...<replacing> "  << *this << " with " << newNode << std::endl;
	
	//ExpressionNode *curOld;
	//ExpressionNode *curNew;
	
	setType(newNode.getType());
	setOperation(newNode.getOperation());
//	setRight(newNode.getRight());
	setFirstChild(newNode.getFirstChild()); // automatically also sets other children
	//curOld = firstChild;
	//curNew = newNode.getFirstChild();
	
	//while (curNew != 0)
	//{
		//curOld->setRight(curNew->getRight());
		//curOld = curOld->getRight();
		//curNew = curNew->getRight();
	//}
	setVariable(newNode.getVariable());
	setValue(newNode.getValue());
	
	std::clog << "...</replacing>" << std::endl;
}
开发者ID:vk-eipi,项目名称:MathParser,代码行数:25,代码来源:expression.cpp

示例3: 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();
		}
	}
}
开发者ID:vk-eipi,项目名称:MathParser,代码行数:22,代码来源:expression.cpp

示例4: isCompatible

bool Multiplication::isCompatible(const ExpressionNode& left, const ExpressionNode& right)
{
	if (left.getType() == NUMBER && right.getType() == NUMBER)
	{
		return true;
	}
	else if (left.getType() == VARIABLE && right.getType() == VARIABLE)
	{
		if (left.getVariable() == right.getVariable())
		{
			return true;
		}
	}
	else if (left.getType() == OPERATION)
	{
		if (left.getOperation() == &ADDITION || left.getOperation() == &SUM)
		{
			return true;
		}
		else if (left.getOperation() == &MULTIPLICATION || left.getOperation() == &PRODUCT)
		{
			return true;
		}
	}
	else if (right.getType() == OPERATION)
	{
		if (right.getOperation() == &ADDITION || right.getOperation() == &SUM)
		{
			return true;
		}
		else if (right.getOperation() == &MULTIPLICATION || right.getOperation() == &PRODUCT)
		{
			return true;
		}
	}
	
	return false;
}
开发者ID:vk-eipi,项目名称:MathParser,代码行数:38,代码来源:operation.cpp

示例5:

bool ExpressionNode::operator== (const ExpressionNode& other) const
{
	ExpressionNode *selfptr;
	ExpressionNode *otherptr;
	
	if (getType() == other.getType() && 
		getOperation() == other.getOperation() && 
		getVariable() == other.getVariable() &&
		getValue() == other.getValue() )
	{
		selfptr = firstChild;
		otherptr = other.getFirstChild();
		while (selfptr != 0 && otherptr != 0)
		{
			if (*selfptr == *otherptr)
			{
				selfptr = selfptr->getRight();
				otherptr = otherptr->getRight();
			}
			else
			{
				return false;
			}
		}
		if (selfptr == 0 && otherptr == 0)
		{
			return true;
		}
		else
		{
			assert(selfptr != otherptr);
			return false;
		}
	}
	else
	{	
		return false;
	}
}
开发者ID:vk-eipi,项目名称:MathParser,代码行数:39,代码来源:expression.cpp

示例6: simplify

ExpressionNode ChainOperation::simplify(const ExpressionNode& root) const
{
	ExpressionNode* left = root.getFirstChild();
	ExpressionNode* lPtr;
	ExpressionNode* rPtr;
	vector<ExpressionNode *> used;
	ExpressionNode newNode(this);
	bool ending = false;
	
	cout << "ChainOp simplify " << *left << " " <<*left->getRight() << endl;
	
	if (left == 0)
	{
		return ExpressionNode(root.getOperation()->getIdentity());
	}
	else if (left->getRight() == 0)
	{
		return *left;
	}
	else
	{
		for (lPtr=left; lPtr->getRight()!=0; lPtr=lPtr->getRight())
		{
			if (find(used.begin(), used.end(), lPtr) == used.end() ) //if lPtr not in used
			{
				if (lPtr->getOperation() == this || lPtr->getOperation() == pairwiseOp)  // if lPtr is itself the same Chain Operation
				{
					for (ExpressionNode* i = lPtr->getFirstChild(); i!=0; i=i->getRight())
					{
						newNode.appendChild(*i);
					}
					used.push_back(lPtr);
				}
				else
				{
					for (rPtr = lPtr->getRight(); rPtr!=0; rPtr=rPtr->getRight())
					{
						if (find(used.begin(),used.end(),rPtr)==used.end() && pairwiseOp->isCompatible(*lPtr, *rPtr))
						{
							newNode.appendChild(pairwiseOp->simplify(*lPtr,*rPtr));
							used.push_back(lPtr);
							used.push_back(rPtr);
							break;
						}
					}
				}
			}
		}
		if (find(used.begin(), used.end(), lPtr) == used.end() ) // check last child for expansion
		{
			if (lPtr->getOperation() == this || lPtr->getOperation() == pairwiseOp)
			{
				for (ExpressionNode* i = lPtr->getFirstChild(); i!=0; i=i->getRight())
				{
					newNode.appendChild(*i);
				}
				used.push_back(lPtr);
			}
		}
	}
	if (newNode.getFirstChild() == 0)
	{
		ending = true;
	}
	for (lPtr=left; lPtr!=0; lPtr=lPtr->getRight())
	{
		if (find(used.begin(),used.end(),lPtr) == used.end())
		{
			newNode.appendChild(*lPtr);
		}
	}
	if (ending)
	{
		clog << "ending ChainSimplify, root/newNode: " << root << newNode << endl;
		return newNode;
	}
	else
	{
		return simplify(newNode);
	}
}
开发者ID:vk-eipi,项目名称:MathParser,代码行数:81,代码来源:operation.cpp

示例7: addTerms

ExpressionNode Addition::addTerms(const ExpressionNode& term1, const ExpressionNode& term2) const
{
	clog << "checkpoint addTerms" << endl;
	
	Number coefficient1;
	Number coefficient2;
	const ExpressionNode * varpart1;
	const ExpressionNode * varpart2;
	ExpressionNode * left;
	ExpressionNode * right;
	ExpressionNode newRight;
	ExpressionNode newCoef;
	ExpressionNode newNode;
	
	if (term1.getType() == NUMBER)
	{
		coefficient1 = term1.getValue();
		varpart1 = 0;
	}
	else if (term1.getType() == OPERATION)
	{
		if (term1.getOperation() == &MULTIPLICATION)
		{
			left = term1.getFirstChild();
			right = left->getRight();
			if (left->getType() == NUMBER)
			{
				coefficient1 = left->getValue();
				varpart1 = right;
			}
			else if (right->getType() == NUMBER)
			{
				coefficient1 = right->getValue();
				varpart1 = left;
			}
			else
			{
				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 = &term2;
	}
	
	if (varpart1 != 0 && varpart2 != 0)
	{
		if (*varpart1 == *varpart2)
		{
			clog << "addTerms simplify like terms" << endl
				<< "coef1: " << coefficient1 << " var1: " << *varpart1 << endl
//.........这里部分代码省略.........
开发者ID:vk-eipi,项目名称:MathParser,代码行数:101,代码来源:operation.cpp

示例8: simplify

ExpressionNode Addition::simplify(ExpressionNode& left, ExpressionNode& right)
{
	std::clog << "checkpoint addsimplify" << std::endl;
	
	//simplest case: at least one side is just 0
	if (left.getType() == NUMBER && left.getValue().getInt() == 0)
	{
		return right;
	}
	else if (right.getType() == NUMBER && right.getValue().getInt() == 0)
	{
		return left;
	}
	
	// for each left term: for each right term: try adding
	
	std::stack <ExpressionNode*> leftNodeStack;
	ExpressionNode * currentLeftNode = &left;
	bool leftFinished = false;
	std::stack <ExpressionNode*> rightNodeStack;
	ExpressionNode * currentRightNode = &right;
	bool rightFinished = false;
	ExpressionNode * tempNodePtr;
	ExpressionNode newNode;
	
	while (leftFinished == false)
	{
		std::clog << "looping left" << std::endl;
		
		if (currentLeftNode->getType() == OPERATION && currentLeftNode->getOperation() == &ADDITION)
		{
			
			if (currentLeftNode->getFirstChild() == 0)
			{
				throw ExpressionNode::WrongArityError();
			}
			currentLeftNode = currentLeftNode->getFirstChild();
			leftNodeStack.push(currentLeftNode);
			continue;
		}
		
		// leaf term on left tree: traverse right tree
		while (rightFinished == false)
		{
			std::clog << "looping right" << std::endl;
			
			if (currentRightNode->getType() == OPERATION && currentRightNode->getOperation() == &ADDITION)
			{
				if (currentRightNode->getFirstChild() == 0)
				{
					throw ExpressionNode::WrongArityError();
				}
				currentRightNode = currentRightNode->getFirstChild();
				rightNodeStack.push(currentRightNode);
				continue;
			}
			
			std::clog << "checkpoint addsimplify: before isAddable; " << std::endl;
			// leaf terms on both sides: attempt adding
			if (isAddable(*currentLeftNode, *currentRightNode))
			{
				std::clog << "checkpoint addsimplify: after isAddable; " << std::endl;
				std::clog << *currentLeftNode << " " << *currentRightNode << std::endl;
				std::clog << "add testrun: " << addTerms(*currentLeftNode, *currentRightNode);
				(*currentLeftNode).replace( addTerms(*currentLeftNode, *currentRightNode) );
				std::clog << "checkpoint addsimplify: after addTerms; " << std::endl;
				if (currentRightNode == &right)
				{
					//entire right tree has been assimilated
					return left;
				}
				tempNodePtr = currentRightNode;
				currentRightNode = right.findParentOf(*currentRightNode);
				right.remove(*tempNodePtr);
			} 
			while (true)
			{
				if (rightNodeStack.size() == 0)
				{
					rightFinished = true;
					break;
				}
				currentRightNode = rightNodeStack.top();
				rightNodeStack.pop();
				if (currentRightNode->getRight() != 0)
				{
					currentRightNode = currentRightNode->getRight();
					rightNodeStack.push(currentRightNode);
					break;
				}
			}
		}
		while (true)
		{
			if (leftNodeStack.size() == 0)
			{
				leftFinished = true;
				break;
			}
			currentLeftNode = leftNodeStack.top();
//.........这里部分代码省略.........
开发者ID:vk-eipi,项目名称:MathParser,代码行数:101,代码来源:operation.cpp


注:本文中的ExpressionNode::getOperation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。