Пример #1
0
        private void IterateExpressionListWithAction(
            int startShift,
            int endSfift,
            Func <int, MathExpression> operatorCheckFunc,
            Func <MathExpression, int, int> exprListUpdatincAction)
        {
            Debug.Assert(startShift < ExpressionList.Count || startShift >= 0,
                         $"Unexpected \"startShift\" = {startShift} input in IterateExpressionListWithAction()");
            Debug.Assert(endSfift < ExpressionList.Count || startShift >= 0,
                         $"Unexpected \"endSfift\" = {endSfift} input in IterateExpressionListWithAction()");
            int i = startShift;

            while (i <= ExpressionList.Count - endSfift)
            {
                MathExpression te = operatorCheckFunc(i);
                if (te == null)
                {
                    i++;
                }
                else
                {
                    i += exprListUpdatincAction(te, i);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Implements functionality for parsing math function string using recursive approach.
        /// </summary>
        private void Parse()
        {
            int startCharIndex = 0;

            // Extract potential math expressions
            while (startCharIndex < InputText.Length)
            {
                MathExpression extractedExpression = GetExpression(InputText, startCharIndex);
                if (!string.IsNullOrEmpty(extractedExpression.Error))
                {
                    Error = extractedExpression.Error;
                    return;
                }
                ExpressionList.Add(extractedExpression);
                startCharIndex += extractedExpression.Length;
            }

            // Validate consistency of expression queue (operators do not follow in a row, there are not missed values near operators)
            for (int i = 0; i < ExpressionList.Count; i++)
            {
                string t = CheckOperatorConsistency(i);
                if (!string.IsNullOrEmpty(t))
                {
                    Error = t;
                    return;
                }
            }

            // Find pow operator "^" and replace with Math.Pow()
            IterateExpressionListWithAction(1, 2, DetectPowOperatorAndCookReplacement, (te, i) =>
            {
                ExpressionList.RemoveRange(i - 1, 3);
                ExpressionList.Insert(i - 1, te);
                return(0);
            });

            // Find missed multiply operators "*" and insert "*" expressions
            IterateExpressionListWithAction(0, 2, DetectMissedMultiplyOperatorAndCookInsertment, (te, i) =>
            {
                ExpressionList.Insert(i + 1, te);
                return(2);
            });

            //Join parsed and processed expressions replaced with C# code
            List <string> outslist = new List <string>();

            foreach (MathExpression me in ExpressionList)
            {
                outslist.Add(me.Replaced);
            }
            OutputText = string.Join("", outslist);
        }
Пример #3
0
 private MathExpression GetExpression(string mathFunc, int start)
 {
     Debug.Assert(!string.IsNullOrEmpty(mathFunc),
                  "\"mathExpression\" input parameter could not be null or empty");
     Debug.Assert(start >= 0 && start < mathFunc.Length,
                  "\"start\" input parameter should be >= 0 and < mathExpression.Length");
     foreach (Func <string, int, MathExpression> f in DetectorsMap.Values)
     {
         MathExpression te = f(mathFunc, start);
         if (te != null)
         {
             return(te);
         }
     }
     // If all detectors did not detect anything - return error about unknown construction
     return(new MathExpression()
     {
         Error = $"Обнаружен неизвестный/непонятный символ \"{mathFunc[start]}\" в выражении \"{mathFunc}\" на позиции {start}"
     });
 }