/// <summary> /// fill multiply operators between: /// constant | number; /// number | constant; /// constant | constant; /// ) | constant, number, (; /// constant, number | (; /// constant, number, ) | function; /// (if there is no operator) /// </summary> /// <param name="equation">string containing equation</param> /// <returns>equation with filled multiply operators where are required</returns> private static string FixMissingMultiplyOperator(string equation) { for (int i = equation.Length - 1; i > 0; --i) { char ch1 = equation[i - 1]; char ch2 = equation[i]; // constant | number if ((StringControl.IsLastCharOfConstantConstant(ch1) && StringControl.GetConstantStringByLastChar(equation.Substring(0, i)) != string.Empty && StringControl.IsNumberChar(ch2)) // number | constant || (StringControl.IsNumberChar(ch1) && StringControl.IsFirstCharOfConstantConstant(ch2) && StringControl.GetConstantString(equation.Substring(i)) != string.Empty) // constant | constant || (StringControl.IsLastCharOfConstantConstant(ch1) && StringControl.GetConstantStringByLastChar(equation.Substring(0, i)) != string.Empty && StringControl.IsFirstCharOfConstantConstant(ch2) && StringControl.GetConstantString(equation.Substring(i)) != string.Empty) // ) | constant number ( || (ch1 == ')' && (StringControl.IsNumberChar(ch2) || StringControl.IsFirstCharOfConstantConstant(ch2) && StringControl.GetConstantString(equation.Substring(i)) != string.Empty || ch2 == '(')) // constant number | ( || ((StringControl.IsNumberChar(ch1) || StringControl.IsLastCharOfConstantConstant(ch1) && StringControl.GetConstantStringByLastChar(equation.Substring(0, i)) != string.Empty) && ch2 == '(') // constant number ) | function || ((StringControl.IsNumberChar(ch1) || StringControl.IsLastCharOfConstantConstant(ch1) && StringControl.GetConstantStringByLastChar(equation.Substring(0, i)) != string.Empty || ch1 == ')') && Operators.IsFirstCharOfFunctionOperator(ch2) && Operators.GetOperator(equation.Substring(i)) != OperatorsEnum.None) ) { equation = equation.Insert(i, Strings.Multiply); } } return(equation); }
/// <summary> /// </summary> /// <param name="equation">string containing equation</param> /// <param name="index">index of ending char of number or ending bracket</param> /// <returns>starting index of number or starting index of corresponding bracket to given bracket</returns> private int GetStartingIndexOfNumberOrStringInsideBracketOnIndex(string equation, int index) { int startingIndex = index; if (equation[index] == ')') { // if there is bracket on index, return index of starting bracket startingIndex = GetStartingBracketOfBracketOnIndex(equation, index); } else { // else check for staring index of number or constant while (startingIndex >= 0 && (StringControl.IsNumberChar(equation[startingIndex]) || StringControl.IsLastCharOfConstantConstant(equation[startingIndex]))) { if (StringControl.IsLastCharOfConstantConstant(equation[startingIndex])) { string constant = StringControl.GetConstantStringByLastChar(equation.Substring(0, startingIndex + 1)); if (constant == string.Empty) { break; } startingIndex -= constant.Length; } else { --startingIndex; } } ++startingIndex; } if (index < startingIndex) { // if there if no number/ constant/ bracket _error = ErrorsEnum.OperatorDoNotHaveEnoughArguments; throw new SyntaxErrorException(); } if (startingIndex == -1) { // shouldn't happened - fixed before solving tree _error = ErrorsEnum.UnknownError; throw new SyntaxErrorException(); } return(startingIndex); }