/// <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); }