Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Clight_18
- {
- class Program
- {
- static void Main()
- {
- string expressionOfBrackets = "((((()))())((()()))(((())())))";
- char leftBracket = '(';
- char rightBracket = ')';
- int totalNestingDepth = 0;
- int currentNestingDepth = 0;
- foreach (var symbol in expressionOfBrackets)
- {
- if (symbol == leftBracket)
- {
- currentNestingDepth++;
- if (currentNestingDepth > totalNestingDepth)
- totalNestingDepth = currentNestingDepth;
- }
- else if (symbol == rightBracket)
- {
- currentNestingDepth--;
- }
- if (currentNestingDepth < 0)
- break;
- }
- if (currentNestingDepth == 0)
- {
- Console.WriteLine($"Строка: \"{expressionOfBrackets}\"\nЯвляется корректным скобочным выражением, " +
- $"c глубиной вложения = {totalNestingDepth}");
- }
- else
- {
- Console.WriteLine($"Строка \"{expressionOfBrackets}\"\nНе является корректным скобочным выражением.");
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement