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;
- namespace _3.CheckBrackets
- {
- class CheckBrackets
- {
- static void Main()
- {
- Console.WriteLine("Enter a mathematical expression:");
- string str = Console.ReadLine();
- string newString = str;
- int leftBrackets = str.Length - str.Replace("(","").Length;
- int rightBrackets =str.Length - str.Replace(")","").Length;
- if (leftBrackets == rightBrackets)
- {
- for (int i = 0; i < leftBrackets; i++)
- {
- int lastLeftBracketIndex = newString.LastIndexOf("(");
- int matchingRigthBracketIndex = newString.IndexOf(")",lastLeftBracketIndex);
- if (matchingRigthBracketIndex - lastLeftBracketIndex < 1)
- {
- Console.WriteLine("The expression {0} is incorrect:", str);
- return;
- }
- else
- {
- newString = newString.Remove(lastLeftBracketIndex, matchingRigthBracketIndex - lastLeftBracketIndex + 1);
- }
- }
- Console.WriteLine("The expression {0} is correct:", str);
- }
- else
- {
- Console.WriteLine("The expression {0} is incorrect:", str);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement