Пример #1
0
        public ICleanerNode Execute(IComputableCleanerNode a, IComputableCleanerNode b)
        {
            switch (type)
            {
            case '+':
                if (a == null)
                {
                    return(b);
                }
                return(a.Add(b));

            case '-':
                if (a == null)
                {
                    return(b.Multiply(new CleanerNode(-1, NO_ID)));
                }
                return(a.Sub(b));

            case '*':
                return(a.Multiply(b));

            case '/':
                return(a.Divide(b));

            case '^':
                return(a.Power(b));
            }
            throw new Exception(string.Format("Unsupported operation {0}", type));
        }
Пример #2
0
        public ICleanerNode Multiply(ICleanerNode a)
        {
            if (typeof(ComplexCleanerNode) == a.GetType())
            {
                ComplexCleanerNode cnna = (ComplexCleanerNode)a;

                if (cnna.IsEqual(this))
                {
                    ComplexCleanerNode n = new ComplexCleanerNode(subNodes.ToArray());

                    if (cnna.powerNode == null && powerNode == null)
                    {
                        n.powerNode = new CleanerNode(2, CleanerOperatorNode.NO_ID);
                    }
                    else if (cnna.powerNode == null)
                    {
                        n.powerNode = (IComputableCleanerNode)powerNode.Add(new CleanerNode(1, CleanerOperatorNode.NO_ID));
                    }
                    else if (powerNode == null)
                    {
                        n.powerNode = (IComputableCleanerNode)cnna.powerNode.Add(new CleanerNode(1, CleanerOperatorNode.NO_ID));
                    }
                    else
                    {
                        n.powerNode = (IComputableCleanerNode)powerNode.Add(cnna.powerNode);
                    }
                    return(n);
                }

                if ((powerNode == null && cnna.powerNode == null) ||
                    (powerNode != null && !powerNode.IsEqual(cnna.powerNode)) ||
                    (powerNode == null && cnna.powerNode != null))
                {
                    return(new ComplexCleanerNode(this, new CleanerOperatorNode('*'), cnna));
                }

                cnna.IterateOverComputables((IComputableCleanerNode node, int i) =>
                {
                    Multiply(node);
                });
                return(this);
            }
            else if (typeof(CleanerNode) == a.GetType())
            {
                if (powerNode != null)
                {
                    return(new ComplexCleanerNode(this, new CleanerOperatorNode('*'), a));
                }

                if (subNodes[1].GetType() == typeof(CleanerOperatorNode))
                {
                    if (((CleanerOperatorNode)subNodes[1]).type == '*' ||
                        ((CleanerOperatorNode)subNodes[1]).type == '/')
                    {
                        for (int i = 0; i < subNodes.Count; i++)
                        {
                            if (!typeof(IComputableCleanerNode).IsAssignableFrom(subNodes[i].GetType()))
                            {
                                continue;
                            }
                            IComputableCleanerNode node = (IComputableCleanerNode)subNodes[i];
                            subNodes[i] = node.Multiply((CleanerNode)a);
                            break;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < subNodes.Count; i++)
                        {
                            if (!typeof(IComputableCleanerNode).IsAssignableFrom(subNodes[i].GetType()))
                            {
                                continue;
                            }
                            IComputableCleanerNode node = (IComputableCleanerNode)subNodes[i];
                            subNodes[i] = node.Multiply((CleanerNode)a);
                        }
                    }
                }
                return(this);
            }
            throw new System.NotImplementedException();
        }