Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace homeWorkParenthesisExpression
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- const char OpeningBracket = '(';
- const char ClosingBracket = ')';
- string userInput;
- int depth = 0;
- int maxDepth = 0;
- bool isValid = true;
- Console.Write("Введите строку выражения включающую в себя скобки: ");
- userInput = Console.ReadLine();
- foreach (char simbol in userInput)
- {
- if (simbol == OpeningBracket)
- {
- depth++;
- if (depth > maxDepth)
- maxDepth = depth;
- }
- else if (simbol == ClosingBracket)
- {
- depth--;
- if (depth < 0)
- {
- isValid = false;
- break;
- }
- }
- }
- if (depth != 0)
- isValid = false;
- if (isValid)
- Console.WriteLine($"Выражение корректно. Максимальная глубина {maxDepth}");
- else
- Console.WriteLine("Выражение не корректно.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement