Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Homework18
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- int maxDepth = 0;
- int currentDepth = 0;
- int bracketBalance = 0;
- char leftBracket = '(';
- char rightBracket = ')';
- string userInput;
- Console.WriteLine("Введите скобочное выражение:");
- userInput = Console.ReadLine();
- foreach (char bracket in userInput)
- {
- if (bracket == leftBracket)
- {
- currentDepth++;
- }
- else if (bracket == rightBracket)
- {
- currentDepth--;
- if (currentDepth < bracketBalance)
- {
- break;
- }
- }
- if (currentDepth > maxDepth)
- {
- maxDepth = currentDepth;
- }
- }
- if (currentDepth == bracketBalance)
- {
- Console.WriteLine($"Скобочное выражение {userInput} корректно");
- Console.WriteLine($"Максимальная глубина вложенности: {maxDepth}");
- }
- else if (currentDepth < bracketBalance || currentDepth > 0)
- {
- Console.WriteLine($"Скобочное выражение {userInput} не корректно");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement