示例#1
0
        /// <summary>
        /// Solve the equation!
        /// This method recurses into the whole tree and returns a result from the equation.
        /// </summary>
        /// <param name="paramCallback">Parameter callback that will be used to get teh values of parameter nodes.</param>
        /// <returns>The solution of this node and all its subnodes!</returns>
        public override float Solve(ParamDelegate paramCallback)
        {
            //make sure this node is set up correctly
            Debug.Assert(null != Prev);
            Debug.Assert(null != Next);

            //Solve the sub nodes!
            float prevResult = Prev.Solve(paramCallback);
            float nextResult = Next.Solve(paramCallback);

            //what kind of operator do we got?
            switch (OrderOfOperationsValue)
            {
            case PemdasValue.Exponent:
            {
                return((float)Math.Pow(prevResult, nextResult));
            }

            case PemdasValue.Multiplication:
            {
                return(prevResult * nextResult);
            }

            case PemdasValue.Division:
            {
                //TODO: can hit divide by zero exception here
                return(prevResult / nextResult);
            }

            case PemdasValue.Addition:
            {
                return(prevResult + nextResult);
            }

            case PemdasValue.Subtraction:
            {
                return(prevResult - nextResult);
            }

            default:
            {
                throw new NotSupportedException("found a weirdo thing in an equation node?");
            }
            }
        }
示例#2
0
        /// <summary>
        /// Solve the equation!
        /// This method recurses into the whole tree and returns a result from the equation.
        /// </summary>
        /// <param name="paramCallback">Parameter callback that will be used to get teh values of parameter nodes.</param>
        /// <returns>The solution of this node and all its subnodes!</returns>
        public override double Solve(ParamDelegate paramCallback)
        {
            //make sure this node is set up correctly

            //check arguments
            if (null == Prev)
            {
                throw new ArgumentNullException("Prev");
            }
            if (null == Next)
            {
                throw new ArgumentNullException("Next");
            }

            //Solve the sub nodes!
            double prevResult = Prev.Solve(paramCallback);
            double nextResult = Next.Solve(paramCallback);

            //what kind of operator do we got?
            switch (OrderOfOperationsValue)
            {
            case PemdasValue.Exponent:
            {
                return(Math.Pow(prevResult, nextResult));
            }

            case PemdasValue.Multiplication:
            {
                return(prevResult * nextResult);
            }

            case PemdasValue.Division:
            {
                //guard against divide by zero exception
                if (0.0 == nextResult)
                {
                    return(0.0);
                }
                else
                {
                    return(prevResult / nextResult);
                }
            }

            case PemdasValue.Modulo:
            {
                //guard against divide by zero exception
                if (0.0 == nextResult)
                {
                    return(0.0);
                }
                else
                {
                    return(prevResult % nextResult);
                }
            }

            case PemdasValue.Addition:
            {
                return(prevResult + nextResult);
            }

            case PemdasValue.Subtraction:
            {
                return(prevResult - nextResult);
            }

            default:
            {
                throw new NotSupportedException("found a weirdo thing in an equation node?");
            }
            }
        }