Advertisement
rukvir

HW 2_11_1

Sep 30th, 2023
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System;
  2.  
  3. namespace HomeWork
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int depth = 0;
  10.             int counterDepth = 0;
  11.             string strUser = "";
  12.             char[] strUserArray;
  13.  
  14.             Console.WriteLine("Введите строку для разбора.");
  15.             strUser = Console.ReadLine();
  16.             strUserArray = strUser.ToCharArray();
  17.  
  18.             for (int i = 0; i < strUserArray.Length; i++)
  19.             {
  20.                 if (strUserArray[i] == '(')
  21.                     depth++;
  22.                 else if ((strUserArray[i] == ')') && (depth > 0))
  23.                 {
  24.                     depth--;
  25.                     counterDepth++;
  26.                 }
  27.                 else if (strUserArray[i] == ')')
  28.                 {
  29.                     if (i != strUserArray.Length - 1 && strUserArray[i + 1] != '(')
  30.                         counterDepth++;
  31.                     depth--;
  32.                 }
  33.                 else if (strUserArray[i] != ')' && ((depth < 0) || depth > 0))
  34.                 {
  35.                     Console.WriteLine("Строка не верная!");
  36.                     break;
  37.                 }
  38.             }
  39.  
  40.             if (depth == 0)
  41.                 Console.WriteLine($"Строка не верная!");
  42.             else
  43.                 Console.WriteLine($"Глубина строки = {counterDepth}.");
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement