示例#1
0
        //Проверка корректности скобок
        private bool checkBrackets(string sourceStr)
        {
            Stack <int>  posOfBrackets = new Stack <int>();
            Stack <char> brackets      = new Stack <char>();

            for (int i = 0; i < sourceStr.Length; i++)
            {
                if (sourceStr [i] == '(')
                {
                    brackets.Push('(');
                    posOfBrackets.Push(i);
                }
                if (sourceStr[i] == ')')
                {
                    try{
                        brackets.Pop();
                        posOfBrackets.Pop();
                    } catch {
                        ErrorExeption.checkExeption(sourceStr[i], i);
                        return(false);
                    }
                }
            }
            if (brackets.Count == 0)
            {
                return(checkOperations(sourceStr));
            }
            else
            {
                ErrorExeption.checkExeption(brackets.Pop(), posOfBrackets.Pop());
                return(false);
            }
        }
示例#2
0
        //Проверка операций
        private bool checkOperations(string sourceStr)
        {
            string charsOfOperation = "+-/*.";

            for (int i = 0; i < sourceStr.Length; i++)
            {
                if (charsOfOperation.Contains(sourceStr[i].ToString()) && charsOfOperation.Contains(sourceStr[i + 1].ToString()))
                {
                    ErrorExeption.checkExeption(sourceStr[i], i);
                    return(false);
                }
            }
            return(true);
        }
示例#3
0
        //проверка выражения
        public bool checkExpression()
        {
            string enabledChars = "0123456789+-/*.()";

            for (int i = 0; i < sourceStr.Length; i++)
            {
                if (!enabledChars.Contains(sourceStr[i].ToString()))
                {
                    ErrorExeption.checkExeption(sourceStr[i], i);
                    return(false);
                }
            }
            return(checkBrackets(sourceStr));
        }