Advertisement
AziLif

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

Dec 10th, 2024 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int currentDepth = 0;
  10.             int maximumDepth = 0;
  11.  
  12.             char openingBracket = '(';
  13.             char closureBracket = ')';
  14.  
  15.             string userInput;
  16.  
  17.             Console.WriteLine("Здравствуйте. Введите символы скобочек, желательно правильно," +
  18.                 "чтобы показать что код сработает верно! Спасибо))");
  19.  
  20.             userInput = Console.ReadLine();
  21.  
  22.             for (int i = 0; i < userInput.Length; i++)
  23.             {
  24.                 if (userInput[i] == openingBracket)
  25.                 {
  26.                     currentDepth++;
  27.  
  28.                     if (currentDepth > maximumDepth)
  29.                     {
  30.                         maximumDepth = currentDepth;
  31.                     }
  32.                 }
  33.                 else if (userInput[i] == closureBracket)
  34.                 {
  35.                     currentDepth--;
  36.  
  37.                     if (currentDepth < 0)
  38.                     {
  39.                         break;
  40.                     }
  41.                 }
  42.             }
  43.  
  44.             if (maximumDepth <= 0 || currentDepth < 0)
  45.             {
  46.                 Console.WriteLine("Что-то пошло уж совсем не так...");
  47.             }
  48.             else if (currentDepth == 0 || maximumDepth != 0)
  49.             {
  50.                 Console.WriteLine($"Скобочное выражение верно! Глубина скобок - {maximumDepth}");
  51.             }
  52.             else
  53.             {
  54.                 Console.WriteLine("Скобочное выражение неверно!");
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement