Advertisement
petromaxa

Untitled

Jan 31st, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _3.CheckBrackets
  7. {
  8. class CheckBrackets
  9. {
  10. static void Main()
  11. {
  12. Console.WriteLine("Enter a mathematical expression:");
  13. string str = Console.ReadLine();
  14. string newString = str;
  15. int leftBrackets = str.Length - str.Replace("(","").Length;
  16. int rightBrackets =str.Length - str.Replace(")","").Length;
  17. if (leftBrackets == rightBrackets)
  18. {
  19. for (int i = 0; i < leftBrackets; i++)
  20. {
  21. int lastLeftBracketIndex = newString.LastIndexOf("(");
  22. int matchingRigthBracketIndex = newString.IndexOf(")",lastLeftBracketIndex);
  23. if (matchingRigthBracketIndex - lastLeftBracketIndex < 1)
  24. {
  25. Console.WriteLine("The expression {0} is incorrect:", str);
  26. return;
  27. }
  28. else
  29. {
  30. newString = newString.Remove(lastLeftBracketIndex, matchingRigthBracketIndex - lastLeftBracketIndex + 1);
  31. }
  32. }
  33. Console.WriteLine("The expression {0} is correct:", str);
  34. }
  35. else
  36. {
  37. Console.WriteLine("The expression {0} is incorrect:", str);
  38. }
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement