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 IsCorrectString
- {
- class Program
- {
- static void Main(string[] args)
- {
- string text = "((((()))())((()()))(((())())))";
- string currentText = text;
- char leftBracket = '(';
- char rightBracket = ')';
- bool isFindDoubleBracket;
- bool isChecking;
- bool isRemoveDoubleBrackets;
- bool isRemove;
- int nestCount;
- int countLeftBracket = 0;
- int countRightBracket = 0;
- foreach (char symbol in currentText)
- {
- if (symbol == leftBracket)
- countLeftBracket++;
- if (symbol == rightBracket)
- countRightBracket++;
- }
- if (countLeftBracket == countRightBracket)
- {
- isChecking = true;
- isFindDoubleBracket = false;
- nestCount = 0;
- while (isChecking)
- {
- for (int i = 0; i < currentText.Length; i++)
- {
- if (i == currentText.Length - 1)
- break;
- if (currentText[i] == leftBracket && currentText[i + 1] == rightBracket)
- {
- isFindDoubleBracket = true;
- nestCount++;
- break;
- }
- }
- if (isFindDoubleBracket)
- {
- isFindDoubleBracket = false;
- isRemoveDoubleBrackets = true;
- int j = 0;
- while (isRemoveDoubleBrackets)
- {
- isRemove = false;
- for (int i = j; i < currentText.Length; i++)
- {
- if (i == currentText.Length - 1)
- break;
- if (currentText[i] == leftBracket && currentText[i + 1] == rightBracket)
- {
- currentText = currentText.Remove(i, 2);
- isRemove = true;
- j = i;
- break;
- }
- }
- if (!isRemove)
- isRemoveDoubleBrackets = false;
- }
- }
- else if (currentText.Length > 0)
- {
- isChecking = false;
- Console.WriteLine($"Строка \"{text}\" не является корректным скобочным выражением.");
- }
- else if (currentText.Length <= 0)
- {
- isChecking = false;
- Console.WriteLine($"Строка \"{text}\" является корректным скобочным выражением. Количество вложений = {nestCount}");
- }
- }
- }
- else
- {
- Console.WriteLine($"Строка \"{text}\" не является корректным скобочным выражением.");
- }
- Console.ReadKey();
- }
- }
- }
Add Comment
Please, Sign In to add comment