ellapt

T14.3.CheckBrackets

Feb 3rd, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class CheckBrackets
  5. {
  6. static bool CheckBr(string exprString)
  7. {
  8. List<char> bracketsList = new List<char>();
  9. bool correct = true;
  10. foreach (char symbol in exprString)
  11. {
  12. if (symbol == '(')
  13. {
  14. bracketsList.Add(symbol);
  15. }
  16. else if (symbol == ')')
  17. {
  18. if (bracketsList.Count > 0)
  19. {
  20. bracketsList.Remove('(');
  21. }
  22. else
  23. {
  24. correct = false;
  25. return correct;
  26. }
  27. }
  28. }
  29. if (bracketsList.Count == 0)
  30. {
  31. correct = true;
  32. return correct;
  33. }
  34. else
  35. {
  36. correct = false;
  37. return correct;
  38. }
  39. }
  40. static void Main()
  41. {
  42. Console.WriteLine("Check if in a given expression the brackets are put correctly\n");
  43. Console.Write("Enter an expresion: ");
  44. string inpStr = Console.ReadLine();
  45. Console.WriteLine("The brackets are put correctly? {0}", CheckBr(inpStr));
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment