NikaBang

ДЗ: Скобочное выражение

Oct 22nd, 2025
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | Gaming | 0 0
  1. internal class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         char openParenthesis = '(';
  6.         char closedParenthesis = ')';
  7.  
  8.         int countParenthesis = 0;
  9.         int maxCountParenthesis = 0;
  10.  
  11.         Console.Write($"Введите строку из скобок '{openParenthesis}' и '{closedParenthesis}' - ");
  12.  
  13.         string userInput = Console.ReadLine();
  14.  
  15.         foreach (char parenthesis in userInput)
  16.         {
  17.             if (parenthesis == openParenthesis)
  18.             {
  19.                 countParenthesis++;
  20.  
  21.                 if (countParenthesis > maxCountParenthesis)
  22.                     maxCountParenthesis = countParenthesis;
  23.             }
  24.             else
  25.             {
  26.                 countParenthesis--;
  27.  
  28.                 if (countParenthesis < 0)
  29.                     break;
  30.             }
  31.  
  32.         }
  33.  
  34.         if (countParenthesis == 0)
  35.         {
  36.             Console.WriteLine($"строка корректная и максимум глубины равняется {maxCountParenthesis}");
  37.         }
  38.         else
  39.         {
  40.             Console.WriteLine("Некорректная строка.");
  41.         }
  42.  
  43.         Console.ReadKey();
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment