RedFlys

Home work - bracket expression

Mar 20th, 2022 (edited)
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Home_Work
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             string userInput;
  14.             int depth = 0;
  15.             int maxDepth = depth;
  16.             bool isCorrect = true;
  17.  
  18.             Console.Write("Введите скобочное выражение: ");
  19.             userInput = Console.ReadLine();
  20.  
  21.             foreach (char symbol in userInput)
  22.             {
  23.                 switch (symbol)
  24.                 {
  25.                     case '(':
  26.                         depth++;
  27.  
  28.                         if (depth > maxDepth)
  29.                             maxDepth = depth;
  30.                         break;
  31.  
  32.                     case ')':
  33.                         depth--;
  34.  
  35.                         if (depth < 0)
  36.                             isCorrect = false;
  37.                         break;
  38.  
  39.                     default:
  40.                         isCorrect = false;
  41.                         break;
  42.                 }
  43.             }
  44.  
  45.             if (isCorrect && depth == 0)
  46.                 Console.WriteLine("Скобочное выражение корректно. Максимальная глубина: " + maxDepth);
  47.             else
  48.                 Console.WriteLine("Скобочное выражение не корректно. Возможная глубина " + maxDepth);
  49.  
  50.             Console.ReadKey();
  51.         }
  52.     }
  53. }
Add Comment
Please, Sign In to add comment