Ejemplo n.º 1
0
        public static Entity operator /(Entity a, Entity b)
        {
            var result = new OperatorEntity(Constants.Operators.DIV);

            result.Children.Add(a);
            result.Children.Add(b);
            return(result);
        }
Ejemplo n.º 2
0
        public bool IsOperatorEntity(IEnumerable <string> types, out OperatorEntity result)
        {
            result = null;
            var r = this as OperatorEntity;

            if (r != null && types.Contains(r.Name))
            {
                result = r;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        public bool IsOperatorEntity(out OperatorEntity result)
        {
            result = null;
            var r = this as OperatorEntity;

            if (r != null)
            {
                result = r;
                return(true);
            }

            return(false);
        }
        public override Entity Eval()
        {
            VariableEntity va = null, vb = null;
            NumberEntity   na = null, nb = null;
            OperatorEntity oa = null, ob = null;
            var            a      = Children.ElementAt(0).Eval();
            var            b      = Children.ElementAt(1).Eval();
            Entity         result = this;

            if ((a.IsNumberEntity(out na) && b.IsNumberEntity(out nb)))
            {
                return(EvalNumber(na, nb));
            }

            if (a.IsVariableEntity(out va) && b.IsVariableEntity(out vb))
            {
                return(EvalVariable(Name, va, vb));
            }

            if (Name == Constants.Operators.MUL)
            {
                return(EvalMultiplicationOperator(a, b));
            }

            if (Name == Constants.Operators.SUM || Name == Constants.Operators.SUB)
            {
                return(EvalSumOrSubOperator(Name, a, b));
            }

            switch (Name)
            {
            case Constants.Operators.MUL:
                return(a * b);

            case Constants.Operators.DIV:
                return(a / b);
            }

            return(null);
        }
        private static Entity EvalSumOrSubOperator(string name, Entity a, Entity b)
        {
            VariableEntity va = null, vb = null;
            NumberEntity   na = null, nb = null;
            OperatorEntity oa = null, ob = null;

            // (x + x2) + 3
            // (x + x2) + x5
            if ((a.IsOperatorEntity(new string[] { Constants.Operators.SUM, Constants.Operators.SUB }, out oa) || b.IsOperatorEntity(new string[] { Constants.Operators.SUM, Constants.Operators.SUB }, out ob)) &&
                ((a.IsVariableEntity(out va) || b.IsVariableEntity(out vb)) ||
                 ((a.IsNumberEntity(out na) || b.IsNumberEntity(out nb)))))
            {
                var children = new List <Entity>();
                if (ob != null)
                {
                    children.Add(a);
                    if (name == Constants.Operators.SUB)
                    {
                        ob = (Number.Create(-1) * ob).Eval() as OperatorEntity;
                    }

                    var entities = GetSumOrSubEntities(ob);
                    children.AddRange(GetSumOrSubEntities(ob));
                }
                else
                {
                    children.AddRange(GetSumOrSubEntities(oa));
                    if (name == Constants.Operators.SUB)
                    {
                        b = (Number.Create(-1) * b).Eval();
                    }

                    children.Add(b);
                }

                var    variables      = children.Where(_ => _ is VariableEntity).GroupBy(_ => ((VariableEntity)_).Name + ((VariableEntity)_).Pow);
                var    numbers        = children.Where(_ => _ is NumberEntity);
                Entity variableEntity = null;
                foreach (var grp in variables)
                {
                    Entity subVariableEntity = null;
                    foreach (var variable in grp)
                    {
                        if (subVariableEntity == null)
                        {
                            subVariableEntity = variable;
                        }
                        else
                        {
                            subVariableEntity = (subVariableEntity + variable).Eval();
                        }
                    }

                    if (variableEntity == null)
                    {
                        variableEntity = subVariableEntity;
                    }
                    else
                    {
                        variableEntity = variableEntity + subVariableEntity;
                    }
                }

                Entity numberEntity = Number.Create(0);
                foreach (var number in numbers)
                {
                    numberEntity = (numberEntity + number).Eval();
                }

                return(variableEntity + numberEntity);
            }

            // a == 0 or b == 0
            bool isAEmpty = false, isBEmpty = false;

            if (a.IsNumberEntity(out na) && (isAEmpty = na.Number.Value == 0) || b.IsNumberEntity(out nb) && (isBEmpty = nb.Number.Value == 0))
            {
                return(isAEmpty ? b : a);
            }

            switch (name)
            {
            case Constants.Operators.SUM:
                return(a + b);

            default:
                return(a - b);
            }
        }