Beispiel #1
0
        /// <summary>
        /// Парсинг математического выражения
        /// </summary>
        /// <param name="mathExpression"></param>
        /// <param name="variables"></param>
        /// <returns>MathTryParseResult</returns>
        public MathTryParseResult TryParse(string mathExpression, ICollection <Variable> variables = null)
        {
            var defaultResult = new MathTryParseResult()
            {
                IsSuccessfulCreated = false,
                InputString         = mathExpression
            };

            var parameters = new MathParserTryParseParameters(mathExpression, variables);

            var errorMessage = CheckTryParseParameters(parameters);

            if (!string.IsNullOrEmpty(errorMessage))
            {
                defaultResult = defaultResult with {
                    ErrorMessage = errorMessage
                };
                return(defaultResult);
            }

            var tryParseResult = TryParseExpression(parameters.MathExpression, parameters.Variables);

            tryParseResult = tryParseResult with {
                InputString = defaultResult.InputString
            };

            return(tryParseResult);
        }
Beispiel #2
0
        private string CheckTryParseParameters(MathParserTryParseParameters parameters)
        {
            var errorMessage   = "";
            var mathExpression = parameters.MathExpression;
            var variables      = parameters.Variables;


            if (string.IsNullOrEmpty(mathExpression) || mathExpression.All(ch => char.IsWhiteSpace(ch)))
            {
                errorMessage = $"Empty string in mathExpression";
            }

            //проверка на корректность имен переменных
            if (variables != null && variables.Any(v => string.IsNullOrEmpty(v.Name)))
            {
                errorMessage = $"Empty variable name";
            }

            if (variables != null && variables.Any(v => v.Name.Any(ch => !char.IsLetter(ch))))
            {
                errorMessage = $"Incorrect variable name";
            }

            //проверка имен переменных на совпадение с именами констант
            var matchedName = string.Empty;

            if (variables != null)
            {
                matchedName = variables.Where(v => _mathparserEntities.Exists(c => c.Name.ToString() == v.Name.ToLower()))
                              .Select(v => v.Name)
                              .FirstOrDefault();
            }

            if (!string.IsNullOrEmpty(matchedName))
            {
                errorMessage = $"Wrong name for variable {matchedName}. There is already entity with the same name";
            }


            //форматирование строки
            mathExpression = mathExpression.Replace(" ", "");
            mathExpression = mathExpression.ToLower();

            if (!Validate.IsBracketsAreBalanced(mathExpression))
            {
                errorMessage = "brackets are not balanced";
            }

            while (Validate.IsExpressionInBrackets(mathExpression))
            {
                mathExpression = mathExpression
                                 .Remove(mathExpression.Length - 1, 1)
                                 .Remove(0, 1);
            }

            parameters.MathExpression = mathExpression;

            if (string.IsNullOrEmpty(mathExpression))
            {
                errorMessage = $"Empty string in mathExpression";
            }

            return(errorMessage);
        }