コード例 #1
0
        /// <summary>
        /// Gets a list of the sub expressions.
        /// </summary>
        /// <param name="expression">The root expression.</param>
        /// <returns>List of the sub expressions</returns>
        private IEnumerable <CustomExpression> GetChildren(CustomExpression expression)
        {
            if (expression.InnerExpression != null)
            {
                yield return(expression.InnerExpression);

                var children = this.GetChildren(expression.InnerExpression);
                foreach (var child in children)
                {
                    yield return(child);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Pre-calculates the extra functions (defined at the <see cref="Custom.Functions"/> class) for a given math expression.
        /// </summary>
        /// <param name="expression">The math expression to translate.</param>
        private static void TranslateCustomFunctions(ref string expression)
        {
            var copy         = expression;
            var functionName = expression.GetRootFunctionName();

            if (functionName == null)
            {
                // The expression does not contains custom function.
                return;
            }

            var extracted = ExtractFunctions(expression);

            foreach (var item in extracted)
            {
                if (item.ArgumentValue.HasValue)
                {
                    // The function argument is already simplified.
                    expression = expression.Replace(item.Argument, item.ArgumentValue.Value.ToString());
                }
                else
                {
                    // This function must be simplified.
                    expression = expression.Replace(item.Expression, item.Simplify());
                }
            }

            var expressions = expression.Split(
                new string[] { " + ", " - ", " * ", " / " },
                StringSplitOptions.RemoveEmptyEntries);

            var operators      = expression.Split(expressions, StringSplitOptions.RemoveEmptyEntries);
            var newExpressions = new List <string>();

            var customFunctionNames = Custom.Functions.Keys;

            foreach (var expr in expressions)
            {
                var fname = expr.GetRootFunctionName();
                if (fname != null)
                {
                    var customExpr = new CustomExpression(expr);
                    var simplified = customExpr.Simplify();
                    newExpressions.Add(simplified);
                }
                else
                {
                    newExpressions.Add(expr);
                }
            }

            if (newExpressions.Count - 1 != operators.Length)
            {
                throw new InvalidProgramException(
                          "The simplification of the expression failed!");
            }

            // Building the output expression
            string simplifiedExpression = string.Empty;

            for (int i = 0; i < newExpressions.Count; i++)
            {
                simplifiedExpression += newExpressions[i];
                if (i < operators.Length)
                {
                    simplifiedExpression += operators[i];
                }
            }

            expression = simplifiedExpression;
        }