/// <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> /// will check equation if there are no unknown characters /// </summary> /// <param name="equation">string containing equation</param> /// <returns>true if equation does not contains unknown character else false if contains</returns> private bool CheckUnknownCharacter(string equation) { int i = 0; while (equation.Length > i) { // check if is constant -> the it is good :) if (StringControl.IsFirstCharOfConstantConstant(equation[i]) && StringControl.GetConstantString(equation.Substring(i)) != string.Empty) { i += StringControl.GetConstantString(equation.Substring(i)).Length; } else if (StringControl.IsNumberChar(equation[i])) { // get string of number characters string substring = string.Empty; do { substring += equation[i++]; }while (equation.Length > i && StringControl.IsNumberChar(equation[i])); // check if string can be converted to number try { StringToDouble(substring, ref _error); } catch { return(false); } } else { // get string of all another characters string substring = string.Empty; do { substring += equation[i++]; }while (equation.Length > i && !StringControl.IsNumberChar(equation[i]) && !(StringControl.IsFirstCharOfConstantConstant(equation[i]) && StringControl.GetConstantString(equation.Substring(i)) != string.Empty)); if (!CheckStringOfOperators(substring)) { _error = ErrorsEnum.InvalidSequenceOfCharacters; return(false); } } } return(true); }
/// <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); }
/// <summary> /// </summary> /// <param name="equation">string containing equation</param> /// <param name="index">index of ending char of number or starting bracket</param> /// <returns>ending index of number or ending index of corresponding bracket to given bracket</returns> private int GetEndingIndexOfNumberOrStringInsideBracketOnIndex(string equation, int index) { int endingIndex = index; if (equation[index] == '(') { // if there is bracket on index, return index of starting bracket endingIndex = GetEndingBracketOfBracketOnIndex(equation, index); } else { // else check for staring index of number or constant while (endingIndex < equation.Length && (StringControl.IsNumberChar(equation[endingIndex]) || StringControl.IsConstantChar(equation[endingIndex]))) { ++endingIndex; } --endingIndex; } if (index > endingIndex) { // if there if no number/ constant/ bracket _error = ErrorsEnum.OperatorDoNotHaveEnoughArguments; throw new SyntaxErrorException(); } if (endingIndex == -1) { // shouldn't happened - fixed before solving tree _error = ErrorsEnum.UnknownError; throw new SyntaxErrorException(); } return(endingIndex); }