public static void Test()
        {
            string pass1, pass2, pass3, fail1, fail2, fail3;

            pass1 = "()";
            pass2 = "((()))";
            pass3 = "()(()())";
            fail1 = "(";
            fail2 = ")";
            fail3 = "(()(())";
            string test1, test2, test3, test4;

            test1 = "";                                // true
            test2 = "1 + 2*(3-4)/ (( 5 * 6) -(7=8))";  // true
            test3 = "1 + 2*(3-4)/ (( 5 * 6) -(7=8)))"; // false
            test4 = "))))))()";                        // false
            string[] tests = new string[] { pass1, pass2, pass3, fail1, fail2, fail3, test1, test2, test3, test4 };

            foreach (var test in tests)
            {
                Console.WriteLine($"{test}: {MatchedParentheses.IsBalanced(test)}");
            }

            foreach (var test in tests)
            {
                Console.WriteLine($"Testing array overload_ {test}: {MatchedParentheses.IsBalanced(test.Split())}");
            }
        }
Пример #2
0
        /// <summary>
        /// Verifies input string contains only letters, numbers, whitespace, allowed symbols
        /// </summary>
        /// <param name="input">string input to verify</param>
        /// <returns>True if verified</returns>
        public bool VerifyInput(string input)
        {
            bool checkMatchedParentheses = MatchedParentheses.IsBalanced(input);

            if (!checkMatchedParentheses)
            {
                Console.WriteLine("Invalid input: Parentheses: '(' and ')' must be balanced\n");
                return(false);
            }

            bool _allCharsAllowed = false;

            foreach (var c in input)
            {
                // Currently allows letters, numbers, whitespace, AllowedSymbols as input
                if (Char.IsLetterOrDigit(c) || Char.IsWhiteSpace(c) || AllowedSymbols.Contains(c))
                {
                    _allCharsAllowed = true;
                }
                else
                {
                    Console.WriteLine($"Invalid input: {c}\nOnly letters, numbers and symbols: {AllowedSymbols} are valid input");
                    return(false);
                }
            }
            return(_allCharsAllowed);
        }