Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Simple
  7. {
  8.     class Program
  9.     {
  10.  
  11.         #region valid
  12.         static string validString = "" +
  13.             "//Conmet[[[\n" +
  14.             "/*" +
  15.             "asd[]]]]" +
  16.             "*/" +
  17.             "int a = 0;" +
  18.             "int b = 10;" +
  19.             "while(a+b<100){" +
  20.             "a++;" +
  21.             "str = \"asd\"" +
  22.             "};";
  23.         #endregion
  24.         #region not valid
  25.         static string notValidString = "" +
  26.             "int a = {0;" +
  27.             "(int b = 10;" +
  28.             "while}(a+b<100){" +
  29.             "a++;)" +
  30.             "};";
  31.         #endregion
  32.         static void Main(string[] args)
  33.         {
  34.             List<string> strings = new List<string>() { validString, notValidString, "{[(])}", "}", "'][{}]]'", "//my name is [asd[}\n"};
  35.             foreach (var str in strings)
  36.             {
  37.                 Console.WriteLine($"string is { CheckValid(str)}");
  38.             }
  39.  
  40.         }
  41.         public static readonly string[] symbs = { "{", "[","(","\"","'","}","]",")"};
  42.         private static bool CheckValid(string str){
  43.             if (str == null)
  44.                 throw new ArgumentException("String is null");
  45.             var s = Regex.Matches(str, "[\\(\\{\\[\\]\\}\\)'\"\n]|(?:[\\/\\*].)|([\\/\\/].)/g").ToArray();
  46.             List<string> lex = new List<string>();
  47.             foreach(var a in s)
  48.             {
  49.                 lex.Add(a.Value);
  50.             }
  51.  
  52.            
  53.             Stack<string> opens = new Stack<string>();
  54.             foreach(var c in lex){
  55.                 if(opens.Count>0)
  56.                 {
  57.                     if (opens.Peek() == "//" && c != "\n")
  58.                         continue;
  59.  
  60.                     if (opens.Peek() == "/*" && c != "*/")
  61.                         continue;
  62.                 }
  63.                 switch(c){
  64.                     case "{":
  65.                     case "[":
  66.                     case "(":
  67.                     case @"//":
  68.                     case @"/*":
  69.                         opens.Push(c);
  70.                         continue;
  71.                     case "'":
  72.                     case "\"":
  73.                         if (opens.Count == 0 || opens.Peek() != c){
  74.                             opens.Push(c);
  75.                             continue;}
  76.                         break;
  77.                 }
  78.                 //Закрывающие
  79.                 if (opens.Count == 0)
  80.                     return false;
  81.                 if (opens.Peek() == "'" && c != "'")
  82.                     continue;
  83.                 if (opens.Peek() == "\"" && c != "\"")
  84.                     continue;
  85.                
  86.                 switch (c){
  87.                     case "}":
  88.                         if (opens.Peek() == "{")
  89.                             opens.Pop();
  90.                         break;
  91.                     case "]":
  92.                         if (opens.Peek() == "[")
  93.                             opens.Pop();
  94.                         break;
  95.                     case ")":
  96.                         if (opens.Peek() == "(")
  97.                             opens.Pop();
  98.                         break;
  99.                     case "\"":
  100.                         if (opens.Peek() == "\"")
  101.                             opens.Pop();
  102.                         break;
  103.                     case "'":
  104.                         if (opens.Peek() == "'")
  105.                             opens.Pop();
  106.                         break;
  107.                     case "\n":
  108.                         if (opens.Peek() == "//")
  109.                             opens.Pop();
  110.                         break;
  111.                     case "*/":
  112.                         if (opens.Peek() == "/*")
  113.                             opens.Pop();
  114.                         break;}}            
  115.             return !opens.Any();
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement