Advertisement
sergezhu

Untitled

Apr 23rd, 2023
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. namespace ConsoleApp1;
  2.  
  3. using System.Text;
  4.  
  5. public class Task18
  6. {
  7.     public void Run()
  8.     {
  9.         Console.InputEncoding = Encoding.Unicode;
  10.         Console.OutputEncoding = Encoding.Unicode;
  11.  
  12.         char openingBracket = '(';
  13.         char closingBracket = ')';
  14.        
  15.         bool canExit = false;
  16.  
  17.         while ( canExit == false )
  18.         {
  19.             Console.Clear();
  20.  
  21.             Console.WriteLine("Input text with brackets");
  22.             string text = Console.ReadLine();
  23.  
  24.             int bracketsAccumulator = 0;
  25.             int maximumNestingDepth = 0;
  26.             bool isTextCorrect = true;
  27.             int firstUnexpectedClosingBracketIndex = -1;
  28.  
  29.             for ( var index = 0; index < text.Length; index++ )
  30.             {
  31.                 var symbol = text[index];
  32.                
  33.                 if ( symbol == openingBracket )
  34.                 {
  35.                     bracketsAccumulator++; 
  36.                 }
  37.                 else if ( symbol == closingBracket )
  38.                 {
  39.                     bracketsAccumulator--;
  40.                 }
  41.  
  42.                 maximumNestingDepth = Math.Max( bracketsAccumulator, maximumNestingDepth );
  43.  
  44.                 if ( isTextCorrect && bracketsAccumulator < 0 )
  45.                 {
  46.                     isTextCorrect = false;
  47.                     firstUnexpectedClosingBracketIndex = index;
  48.                 }
  49.             }
  50.  
  51.             if ( isTextCorrect && bracketsAccumulator != 0 )
  52.             {
  53.                 isTextCorrect = false;
  54.             }
  55.  
  56.             if ( isTextCorrect )
  57.             {
  58.                 Console.WriteLine($"Text is correct, maximum nesting depth = {maximumNestingDepth}");
  59.             }
  60.             else if( firstUnexpectedClosingBracketIndex != -1 )
  61.             {
  62.                 Console.WriteLine( $"Text is not valid, maximum nesting depth = {maximumNestingDepth}, has unexpected close bracket, index = {firstUnexpectedClosingBracketIndex}" );
  63.             }
  64.             else
  65.             {
  66.                 Console.WriteLine( $"Text is not valid, maximum nesting depth = {maximumNestingDepth}, missing closing bracket" );
  67.             }
  68.            
  69.             Console.WriteLine();
  70.  
  71.             string properlyExitAnswer = "n";
  72.             Console.WriteLine( $"Continue? Enter \'{properlyExitAnswer}\' for exit" );
  73.             string continueAnswer = Console.ReadLine();
  74.  
  75.             canExit = string.Equals( continueAnswer, properlyExitAnswer );
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement