Advertisement
IGRODELOFF

HW: Parenthesis Expression

Nov 13th, 2024 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2.  
  3. namespace homeWorkParenthesisExpression
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             const char OpeningBracket = '(';
  10.             const char ClosingBracket = ')';
  11.  
  12.             string userInput;
  13.  
  14.             int depth = 0;
  15.             int maxDepth = 0;
  16.  
  17.             bool isValid = true;
  18.  
  19.             Console.Write("Введите строку выражения включающую в себя скобки: ");
  20.             userInput = Console.ReadLine();
  21.  
  22.             foreach (char simbol in userInput)
  23.             {
  24.                 if (simbol == OpeningBracket)
  25.                 {
  26.                     depth++;
  27.                     if (depth > maxDepth)
  28.                         maxDepth = depth;
  29.                 }
  30.                 else if (simbol == ClosingBracket)
  31.                 {
  32.                     depth--;
  33.                     if (depth < 0)
  34.                     {
  35.                         isValid = false;
  36.                         break;
  37.                     }
  38.                 }
  39.             }
  40.  
  41.             if (depth != 0)
  42.                 isValid = false;
  43.  
  44.             if (isValid)
  45.                 Console.WriteLine($"Выражение корректно. Максимальная глубина {maxDepth}");
  46.             else
  47.                 Console.WriteLine("Выражение не корректно.");
  48.         }
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement