Exemple #1
0
        public bool IsEqual(ICleanerNode n)
        {
            if (n == null)
            {
                return(false);
            }

            if (n.GetType() == typeof(ComplexCleanerNode))
            {
                ComplexCleanerNode ccnn = (ComplexCleanerNode)n;
                if (ccnn.subNodes.Count != subNodes.Count ||
                    (ccnn.powerNode != null && ccnn.powerNode.IsEqual(powerNode)) ||
                    (ccnn.powerNode == null && powerNode != null))
                {
                    return(false);
                }

                for (int i = 0; i < subNodes.Count; i++)
                {
                    if (!subNodes[i].IsEqual(ccnn.subNodes[i]))
                    {
                        return(false);
                    }
                }

                return(true);
            }
            return(false);
        }
Exemple #2
0
        public bool BodyEquals(ComplexCleanerNode n)
        {
            if (subNodes.Count != n.subNodes.Count)
            {
                return(false);
            }

            for (int i = 0; i < subNodes.Count; i++)
            {
                if (!subNodes[i].IsEqual(n.subNodes[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Exemple #3
0
        public ICleanerNode Add(ICleanerNode a)
        {
            if (typeof(ComplexCleanerNode) == a.GetType())
            {
                ComplexCleanerNode ccna = ((ComplexCleanerNode)a);
                for (int j = 0; j < ccna.subNodes.Count; j++)
                {
                    if (!typeof(IComputableCleanerNode).IsAssignableFrom(ccna.subNodes[j].GetType()))
                    {
                        continue;
                    }
                    Add(ccna.subNodes[j]);
                }

                return(this);
            }
            else if (typeof(CleanerNode) == a.GetType())
            {
                bool added = false;

                for (int i = 0; i < subNodes.Count; i++)
                {
                    if (!typeof(IComputableCleanerNode).IsAssignableFrom(subNodes[i].GetType()))
                    {
                        continue;
                    }
                    IComputableCleanerNode node = (IComputableCleanerNode)subNodes[i];
                    if (CleanerNode.CanAdd((CleanerNode)node, (CleanerNode)a))
                    {
                        added       = true;
                        subNodes[i] = (CleanerNode)node + (CleanerNode)a;
                        break;
                    }
                }

                if (!added)
                {
                    subNodes.Add(new CleanerOperatorNode('+'));
                    subNodes.Add(a);
                }

                return(this);
            }
            throw new System.NotImplementedException();
        }
Exemple #4
0
        public ICleanerNode Divide(ICleanerNode a)
        {
            if (a.GetType() == typeof(ComplexCleanerNode))
            {
                if (a.IsEqual(this))
                {
                    return(new CleanerNode(1, CleanerOperatorNode.NO_ID));
                }

                ComplexCleanerNode ccna = (ComplexCleanerNode)a;

                if (BodyEquals(ccna))
                {
                    ComplexCleanerNode     n   = new ComplexCleanerNode(subNodes.ToArray());
                    IComputableCleanerNode cpn = powerNode == null ? new CleanerNode(1, CleanerOperatorNode.NO_ID) : powerNode;
                    IComputableCleanerNode apn = powerNode == null ? new CleanerNode(1, CleanerOperatorNode.NO_ID) : ccna.powerNode;

                    n.powerNode = (IComputableCleanerNode)cpn.Sub(apn);
                    return(n);
                }
            }
            return(new ComplexCleanerNode(this, new CleanerOperatorNode('/'), a));
        }
Exemple #5
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();
        }