Skip to content

Instantly share code, notes, and snippets.

@raingat
Last active May 22, 2025 14:37
Show Gist options
  • Save raingat/9842fb3b15418249517869cd545e900f to your computer and use it in GitHub Desktop.
Save raingat/9842fb3b15418249517869cd545e900f to your computer and use it in GitHub Desktop.
Скобочное выражение
using System;
namespace IUnior
{
internal class Program
{
static void Main(string[] args)
{
//Пример “(()(()))” - строка корректная и максимум глубины равняется 3.
//Пример некорректных строк: "(()", "())", ")(", "(()))(()"
int countEnclosure = 0;
int countMaximumEnclosure = 0;
int lastIndex;
string text;
char openBracket = '(';
char closeBracket = ')';
Console.Write("Введите текст скобочное выражение: ");
text = Console.ReadLine();
lastIndex = text.Length - 1;
if (text[0] == closeBracket || text[lastIndex] == openBracket)
{
Console.WriteLine($"Строка {text} некорректная");
}
else
{
for (int i = 0; i < text.Length; i++)
{
if (text[i] == openBracket)
{
countEnclosure++;
}
else if (text[i] == closeBracket)
{
countEnclosure -= 1;
}
if (countEnclosure < 0)
{
Console.WriteLine($"Скобочное выражение {text} некорректно");
break;
}
if (countMaximumEnclosure < countEnclosure)
{
countMaximumEnclosure = countEnclosure;
}
}
if (countEnclosure == 0)
{
Console.WriteLine($"Скобочное выражение {text} корректно");
Console.WriteLine($"Максимальная глубина вложенности - {countMaximumEnclosure}");
}
else if (countEnclosure > 0)
{
Console.WriteLine($"Скобочное выражение {text} некорректно");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment