Example #1
0
        // | + - |
        public static bool IsCorrect(string expression)
        {
            if (expression.Length == 0)
            {
                return(false);
            }

            bool isCorrect           = false;
            bool isCorrectBracket    = true;
            bool isCorrectSeparators = true;
            bool isCorrectOperators  = true;

            bool haveABracket = false;

            for (int i = 0; i < expression.Length; i++)
            {
                if (expression[i] == '(' || expression[i] == ')')
                {
                    haveABracket = true;
                    break;
                }
            }
            if (haveABracket)
            {
                isCorrectBracket = IsCorrectBracket(expression);
            }

            for (int i = 0; i < expression.Length; i++)
            {
                if (Contains.ToSeparators(expression[i]) && i == 0)
                {
                    isCorrectSeparators = false;
                }
                else if (Contains.ToSeparators(expression[i]) &&
                         !Contains.ToNumbers(expression[i - 1]))
                {
                    isCorrectSeparators = false;
                }
                if (Contains.ToSeparators(expression[i]) &&
                    expression.Length - 1 == i)
                {
                    isCorrectSeparators = false;
                }
                else if (Contains.ToSeparators(expression[i]) &&
                         !Contains.ToNumbers(expression[i + 1]))
                {
                    isCorrectSeparators = false;
                }

                if (Contains.ToMathOperators(expression[i]) &&
                    expression[i] != '-' && i == 0)
                {
                    isCorrectOperators = false;
                }
                else if (Contains.ToMathOperators(expression[i]) &&
                         expression[i] != '-' && !Contains.ToNumbers(
                             expression[i - 1]) && expression[i - 1] != ')')
                {
                    isCorrectOperators = false;
                }
                if (Contains.ToMathOperators(expression[i]) &&
                    i == expression.Length - 1)
                {
                    isCorrectOperators = false;
                }
                else if (Contains.ToMathOperators(expression[i]) &&
                         expression[i + 1] != '-' && !Contains.ToNumbers(
                             expression[i + 1]) && expression[i + 1] != '(')
                {
                    isCorrectOperators = false;
                }

                if (i != 0 && i != expression.Length - 1 &&
                    Contains.ToMathOperators(expression[i - 1]) &&
                    Contains.ToMathOperators(expression[i]) &&
                    Contains.ToMathOperators(expression[i + 1]))
                {
                    isCorrectOperators = false;
                }
            }

            if (isCorrectBracket && isCorrectSeparators && isCorrectOperators)
            {
                isCorrect = true;
            }
            return(isCorrect);
        }