lovelyvook

Unit_26

Jun 28th, 2024 (edited)
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Ijunior
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.Write("Введите скобочное выражение: ");
  10.             string userInput = Console.ReadLine();
  11.             char[] frames = userInput.ToCharArray();
  12.             int tempDeepFrames = 0;
  13.             int deepFrames = 0;
  14.             char openFrame = '(';
  15.             char closeFrame = ')';
  16.  
  17.             foreach (char frame in frames)
  18.             {
  19.                 if (frame == openFrame)
  20.                 {
  21.                     tempDeepFrames++;
  22.  
  23.                     if (deepFrames < tempDeepFrames)
  24.                     {
  25.                         deepFrames = tempDeepFrames;
  26.                     }
  27.                 }
  28.                 else if (frame == closeFrame)
  29.                 {
  30.                     tempDeepFrames--;
  31.                 }
  32.  
  33.                 if (tempDeepFrames < 0)
  34.                 {
  35.                     break;
  36.                 }
  37.             }
  38.  
  39.             if (tempDeepFrames == 0)
  40.             {
  41.                 Console.WriteLine("Уровень вложенности: " + deepFrames);
  42.             }
  43.             else
  44.             {
  45.                 Console.WriteLine("Неверное выражение");
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment