Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Simple
  6. {
  7.     class Program
  8.     {
  9.  
  10.         #region valid
  11.         static string validString = "" +
  12.             "int a = 0;" +
  13.             "int b = 10;" +
  14.             "while(a+b<100){" +
  15.             "a++;" +
  16.             "str s = \"asd\"" +
  17.             "};";
  18.         #endregion
  19.         #region not valid
  20.         static string notValidString = "" +
  21.             "int a = {0;" +
  22.             "(int b = 10;" +
  23.             "while}(a+b<100){" +
  24.             "a++;)" +
  25.             "};";
  26.         #endregion
  27.         static void Main(string[] args)
  28.         {
  29.             List<string> strings = new List<string>() { validString, notValidString, "{[(])}" };
  30.             foreach (var str in strings)
  31.             {
  32.                 Console.WriteLine($"string is { CheckValid(str)}");
  33.             }
  34.  
  35.         }
  36.         public static readonly char[] symbs = { '{', '[','(','\'','"','}',']',')'};
  37.         private static bool CheckValid(string str){
  38.             if (str == null)
  39.                 throw new ArgumentException("String is null");            
  40.             char[] lex = str.Where(c=> symbs.Contains(c)).ToArray();
  41.             Stack<char> opens = new Stack<char>();
  42.             foreach(var c in lex){
  43.                 switch(c){
  44.                     case '{':
  45.                     case '[':
  46.                     case '(':
  47.                         opens.Push(c);
  48.                         continue;
  49.                     case '"':
  50.                     case '\'':
  51.                         if (opens.Peek() != c){
  52.                             opens.Push(c);
  53.                             continue;}
  54.                         break;}
  55.                 //Закрывающие
  56.                 switch(c){
  57.                     case '}':
  58.                         if (opens.Peek() == '{')
  59.                             opens.Pop();
  60.                         break;
  61.                     case ']':
  62.                         if (opens.Peek() == '[')
  63.                             opens.Pop();
  64.                         break;
  65.                     case ')':
  66.                         if (opens.Peek() == '(')
  67.                             opens.Pop();
  68.                         break;
  69.                     case '\'':
  70.                         if (opens.Peek() == '\'')
  71.                             opens.Pop();
  72.                         break;
  73.  
  74.                     case '"':
  75.                         if (opens.Peek() == '"')
  76.                             opens.Pop();
  77.                         break;}}            
  78.             return !opens.Any();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement