Advertisement
JohnJuly

Homework18

Nov 13th, 2023 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 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 Homework18
  8. {
  9.     internal class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int maxDepth = 0;
  14.             int currentDepth = 0;
  15.             int bracketBalance = 0;
  16.             char leftBracket = '(';
  17.             char rightBracket = ')';
  18.             string userInput;
  19.  
  20.             Console.WriteLine("Введите скобочное выражение:");
  21.             userInput = Console.ReadLine();
  22.  
  23.             foreach (char bracket in userInput)
  24.             {
  25.                 if (bracket == leftBracket)
  26.                 {
  27.                     currentDepth++;
  28.                 }
  29.                 else if (bracket == rightBracket)
  30.                 {
  31.                     currentDepth--;
  32.  
  33.                     if (currentDepth < bracketBalance)
  34.                     {
  35.                         break;
  36.                     }
  37.                 }
  38.  
  39.                 if (currentDepth > maxDepth)
  40.                 {
  41.                     maxDepth = currentDepth;
  42.                 }
  43.             }
  44.  
  45.             if (currentDepth == bracketBalance)
  46.             {
  47.                 Console.WriteLine($"Скобочное выражение {userInput} корректно");
  48.                 Console.WriteLine($"Максимальная глубина вложенности: {maxDepth}");
  49.             }
  50.             else if (currentDepth < bracketBalance || currentDepth > 0)
  51.             {
  52.                 Console.WriteLine($"Скобочное выражение {userInput} не корректно");
  53.             }
  54.         }
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement