/// <summary> /// Will add brackets if there are more unctions without brackets /// </summary> /// <param name="equation">string containing equation</param> /// <returns>equation</returns> private string FixFunctionInFunctionWithoutBracket(string equation) { string tmpEquation = equation; List <int> indexesForStartingBracket = new List <int>(); List <int> indexesForEndingBracket = new List <int>(); // search for last one argument operator int op; while ((op = GetLastIndexOfOperatorFromEnumArray(tmpEquation, Operators.OperatorsPriority1)) != -1) { int lengthOfOperator = Operators.GetOperatorFromEnum(Operators.GetOperator(tmpEquation.Substring(op))) .Length; // getting indexes for brackets indexesForStartingBracket.Add(op + lengthOfOperator); // ending bracket should be after number or after inner function // if there is internal function, then was replaced with number characters indexesForEndingBracket.Add( GetEndingIndexOfNumberOrStringInsideBracketOnIndex(tmpEquation, op + lengthOfOperator) + 1); // replace actual function with number chars tmpEquation = tmpEquation.Remove(op, lengthOfOperator).Insert(op, StringControl.GenerateStringOfNumberCharacters(lengthOfOperator)); } // sort indexes for adding brackets from the end DecreasingComparer decreasingComparer = new DecreasingComparer(); indexesForStartingBracket.Sort(decreasingComparer); indexesForEndingBracket.Sort(decreasingComparer); // adding brackets while (indexesForStartingBracket.Any() || indexesForEndingBracket.Any()) { if (!indexesForEndingBracket.Any() || indexesForStartingBracket.First() > indexesForEndingBracket.First()) { equation = equation.Insert(indexesForStartingBracket.First(), "("); indexesForStartingBracket.Remove(indexesForStartingBracket.First()); } else { equation = equation.Insert(indexesForEndingBracket.First(), ")"); indexesForEndingBracket.Remove(indexesForEndingBracket.First()); } } return(equation); }
/// <summary> /// </summary> /// <param name="equation">string containing equation</param> /// <returns>equation without brackets but same size with number characters on place brackets and their content</returns> private string ReplaceBracketsAndTheirContentWithNumberCharacters(string equation) { while (equation.Contains('(')) { int startingIndex = equation.IndexOf('('); int endingIndex = GetEndingBracketOfBracketOnIndex(equation, startingIndex); int length = endingIndex - startingIndex + 1; string numberChars = StringControl.GenerateStringOfNumberCharacters(length); equation = equation.Remove(startingIndex, length).Insert(startingIndex, numberChars); } return(equation); }