Advertisement
TwinFrame

Clight_18_ExpressionOfBrackets

Mar 24th, 2023 (edited)
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Clight_18
  4. {
  5.     class Program
  6.     {
  7.         static void Main()
  8.         {
  9.             string expressionOfBrackets = "((((()))())((()()))(((())())))";
  10.             char leftBracket = '(';
  11.             char rightBracket = ')';
  12.  
  13.             int totalNestingDepth = 0;
  14.             int currentNestingDepth = 0;
  15.  
  16.             foreach (var symbol in expressionOfBrackets)
  17.             {
  18.                 if (symbol == leftBracket)
  19.                 {
  20.                     currentNestingDepth++;
  21.  
  22.                     if (currentNestingDepth > totalNestingDepth)
  23.                         totalNestingDepth = currentNestingDepth;
  24.                 }
  25.                 else if (symbol == rightBracket)
  26.                 {
  27.                     currentNestingDepth--;
  28.                 }
  29.  
  30.                 if (currentNestingDepth < 0)
  31.                     break;
  32.             }
  33.  
  34.             if (currentNestingDepth == 0)
  35.             {
  36.                 Console.WriteLine($"Строка: \"{expressionOfBrackets}\"\nЯвляется корректным скобочным выражением, " +
  37.                     $"c глубиной вложения = {totalNestingDepth}");
  38.             }
  39.             else
  40.             {
  41.                 Console.WriteLine($"Строка \"{expressionOfBrackets}\"\nНе является корректным скобочным выражением.");
  42.             }
  43.  
  44.             Console.ReadKey();
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement