Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ConorMkGrEGOR/f4a7d944c3de7e30213a1f1a17d6a9cc to your computer and use it in GitHub Desktop.
Save ConorMkGrEGOR/f4a7d944c3de7e30213a1f1a17d6a9cc to your computer and use it in GitHub Desktop.
using System;
namespace CSLight
{
public class Program
{
public static void Main(string[] args)
{
const char OpenBracket = '(';
const char ClosedBracket = ')';
Console.WriteLine("Введите скобочное выражение:");
string brackets = Console.ReadLine();
int depthAttachment = 0;
int lastIndex = brackets.Length - 1;
int maxDepth = 1;
bool isCorrect = true;
for (int i = 0; i < brackets.Length; i++)
{
if (brackets[i] != OpenBracket && brackets[i] != ClosedBracket)
{
Console.WriteLine("Вы ввели некорректные данные.");
isCorrect = false;
maxDepth = 0;
break;
}
if (brackets[i] == OpenBracket)
{
depthAttachment++;
}
else
{
depthAttachment--;
}
if (depthAttachment < 0)
{
isCorrect = false;
break;
}
if (depthAttachment > maxDepth)
{
maxDepth = depthAttachment;
}
}
Console.WriteLine($"Выражение верно ? - {isCorrect}. " +
$"Глубина вложенности - {maxDepth}.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment