TwinFrame

IsCorrectString

Mar 19th, 2022 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace IsCorrectString
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string text = "((((()))())((()()))(((())())))";
  14.             string currentText = text;
  15.             char leftBracket = '(';
  16.             char rightBracket = ')';
  17.             bool isFindDoubleBracket;
  18.             bool isChecking;
  19.             bool isRemoveDoubleBrackets;
  20.             bool isRemove;
  21.             int nestCount;
  22.  
  23.             int countLeftBracket = 0;
  24.             int countRightBracket = 0;
  25.             foreach (char symbol in currentText)
  26.             {
  27.                 if (symbol == leftBracket)
  28.                     countLeftBracket++;
  29.  
  30.                 if (symbol == rightBracket)
  31.                     countRightBracket++;
  32.             }
  33.  
  34.             if (countLeftBracket == countRightBracket)
  35.             {
  36.                 isChecking = true;
  37.                 isFindDoubleBracket = false;
  38.                 nestCount = 0;
  39.  
  40.                 while (isChecking)
  41.                 {
  42.                     for (int i = 0; i < currentText.Length; i++)
  43.                     {
  44.                         if (i == currentText.Length - 1)
  45.                             break;
  46.  
  47.                         if (currentText[i] == leftBracket && currentText[i + 1] == rightBracket)
  48.                         {
  49.                             isFindDoubleBracket = true;
  50.                             nestCount++;
  51.                             break;
  52.                         }
  53.  
  54.                     }
  55.  
  56.                     if (isFindDoubleBracket)
  57.                     {
  58.                         isFindDoubleBracket = false;
  59.  
  60.                         isRemoveDoubleBrackets = true;
  61.                         int j = 0;
  62.                         while (isRemoveDoubleBrackets)
  63.                         {
  64.                             isRemove = false;
  65.  
  66.                             for (int i = j; i < currentText.Length; i++)
  67.                             {
  68.                                 if (i == currentText.Length - 1)
  69.                                     break;
  70.  
  71.                                 if (currentText[i] == leftBracket && currentText[i + 1] == rightBracket)
  72.                                 {
  73.                                     currentText = currentText.Remove(i, 2);
  74.  
  75.                                     isRemove = true;
  76.                                     j = i;
  77.                                     break;
  78.                                 }
  79.                             }
  80.  
  81.                             if (!isRemove)
  82.                                 isRemoveDoubleBrackets = false;
  83.                         }
  84.                     }
  85.                     else if (currentText.Length > 0)
  86.                     {
  87.                         isChecking = false;
  88.  
  89.                         Console.WriteLine($"Строка \"{text}\" не является корректным скобочным выражением.");
  90.                     }
  91.                     else if (currentText.Length <= 0)
  92.                     {
  93.                         isChecking = false;
  94.  
  95.                         Console.WriteLine($"Строка \"{text}\" является корректным скобочным выражением. Количество вложений = {nestCount}");
  96.                     }
  97.                 }
  98.             }
  99.             else
  100.             {
  101.                 Console.WriteLine($"Строка \"{text}\" не является корректным скобочным выражением.");
  102.             }
  103.            
  104.             Console.ReadKey();
  105.         }
  106.     }
  107. }
Add Comment
Please, Sign In to add comment