コード例 #1
0
ファイル: Node.cpp プロジェクト: QlowB/Mathy
std::shared_ptr<ExpressionNode> DivisionNode::evaluate(Environment* e)
{
    std::shared_ptr<ExpressionNode> left = a->evaluate(e);
    std::shared_ptr<ExpressionNode> right = b->evaluate(e);
    ConstantNode* cLeft = dynamic_cast<ConstantNode*>(&*left);
    ConstantNode* cRight = dynamic_cast<ConstantNode*>(&*right);
    
    if (cLeft != 0 && cRight != 0) {
        IntegerNode* iLeft = dynamic_cast<IntegerNode*>(cLeft);
        IntegerNode* iRight = dynamic_cast<IntegerNode*>(cRight);
        RealNode* rLeft = dynamic_cast<RealNode*>(cLeft);
        RealNode* rRight = dynamic_cast<RealNode*>(cRight);
        
        if (iLeft != 0 && iRight != 0) {
            return shared_from_this();
        }
        if (iLeft != 0 && rRight != 0) {
            return std::make_shared<RealNode>
                    (iLeft->getValue() / rRight->getValue());
        }
        if (rLeft != 0 && iRight != 0) {
            return std::make_shared<RealNode>
                    (rLeft->getValue() / iRight->getValue());
        }
        if (rLeft != 0 && rRight != 0) {
            return std::make_shared<RealNode>
                    (rLeft->getValue() / rRight->getValue());
        }
        
        return 0;
    } else {
        return std::make_shared<DivisionNode>(left, right);
    }
}
コード例 #2
0
ファイル: Node.cpp プロジェクト: QlowB/Mathy
std::shared_ptr<ExpressionNode> MultiplicationNode::evaluate(Environment* e)
{
    std::shared_ptr<ExpressionNode> left = a->evaluate(e);
    std::shared_ptr<ExpressionNode> right = b->evaluate(e);
    ConstantNode* cLeft = dynamic_cast<ConstantNode*>(&*left);
    ConstantNode* cRight = dynamic_cast<ConstantNode*>(&*right);
    
    if (cLeft != 0 && cRight != 0) {
        IntegerNode* iLeft = dynamic_cast<IntegerNode*>(cLeft);
        IntegerNode* iRight = dynamic_cast<IntegerNode*>(cRight);
        RealNode* rLeft = dynamic_cast<RealNode*>(cLeft);
        RealNode* rRight = dynamic_cast<RealNode*>(cRight);
        
        if (iLeft != 0 && iRight != 0) {
            return std::make_shared<IntegerNode>
                    (iLeft->getValue() * iRight->getValue());
        }
        if (iLeft != 0 && rRight != 0) {
            return std::make_shared<RealNode>
                    (iLeft->getValue() * rRight->getValue());
        }
        if (rLeft != 0 && iRight != 0) {
            return std::make_shared<RealNode>
                    (rLeft->getValue() * iRight->getValue());
        }
        if (rLeft != 0 && rRight != 0) {
            return std::make_shared<RealNode>
                    (rLeft->getValue() * rRight->getValue());
        }
        
        return 0;
    } else {
        IntegerNode* iLeft = dynamic_cast<IntegerNode*>(cLeft);
        IntegerNode* iRight = dynamic_cast<IntegerNode*>(cRight);
        if (iLeft != 0 && iLeft->getValue() == 1) {
            return right;
        }
        if (iLeft != 0 && iLeft->getValue() == 0) {
            return std::make_shared<IntegerNode>(0);
        }
        if (iRight != 0 && iRight->getValue() == 1) {
            return left;
        }
        if (iRight != 0 && iRight->getValue() == 0) {
            return std::make_shared<IntegerNode>(0);
        }
        return std::make_shared<MultiplicationNode>(left, right);
    }
}
コード例 #3
0
/**
 * Transform a RealNode to a XML node.
 *
 * @param node		A settings tree node representing a real node.
 * @param parent	The parent node of the XML document.
 */
Void XmlSettingsTree::settingsTreeToXml( RealNode& node, TiXmlNode& parent ) {
	TiXmlElement* 	element	= new TiXmlElement( XMLSETTINGSTREE_REAL_ELEMENT );
	TiXmlText* 		text 	= new TiXmlText( TextUtils::doubleToString( node.getValue(), 14 ).c_str() );

	element->LinkEndChild( text );
	parent.LinkEndChild( element );
}