Advertisement
Guest User

Untitled

a guest
Dec 21st, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. public static class BracketsChecker {
  2.  
  3.     public class BracketMismatchException : ApplicationException {
  4.  
  5.         public readonly char openChar;
  6.         public readonly char closeChar;
  7.         public readonly int closePosition;
  8.  
  9.         public BracketMismatchException(char openChar, char closeChar, int closePosition)
  10.         : base(string.Format("Bracket mismatch at {2}: got \"{1}\" for opening \"{0}\"", openChar, closeChar, closePosition)) {
  11.             this.openChar = openChar;
  12.             this.closeChar = closeChar;
  13.             this.closePosition = closePosition;
  14.         }
  15.  
  16.     }
  17.  
  18.     public class ExtraOpeningBracketException : ApplicationException {
  19.  
  20.         public ExtraOpeningBracketException()
  21.         : base("Extra opening bracket") {
  22.         }
  23.  
  24.     }
  25.  
  26.     public class ExtraClosingBracketException : ApplicationException {
  27.  
  28.         public readonly char closeChar;
  29.         public readonly int closePosition;
  30.  
  31.         public ExtraClosingBracketException(char closeChar, int closePosition)
  32.         : base(string.Format("Extra closing bracket at {1}: \"{0}\"", closeChar, closePosition)) {
  33.             this.closeChar = closeChar;
  34.             this.closePosition = closePosition;
  35.         }
  36.  
  37.     }
  38.  
  39.     private class Bracket {
  40.  
  41.         char[] openChars;
  42.  
  43.         char[] closeChars;
  44.  
  45.         public Bracket(char[] openChars, char[] closeChars) {
  46.             this.openChars = openChars;
  47.             this.closeChars = closeChars;
  48.         }
  49.  
  50.     }
  51.  
  52.     private static readonly Bracket[] Brackets = new[] {
  53.         new Bracket(new[] { '(' }, new[] { ')' }),
  54.         new Bracket(new[] { '[' }, new[] { ']' }),
  55.         new Bracket(new[] { '{' }, new[] { '}' }),
  56.     }
  57.  
  58.     private static readonly Dictionary<char, Bracket> BracketsByOpenChar =
  59.         (
  60.             from bracket in Brackets
  61.             from openChar in bracket.openChars
  62.             select new KeyValuePair<char, Bracket>(openChar, bracket)
  63.         ).ToDictionary();
  64.  
  65.     private static readonly HashSet<char> CloseChars =
  66.         new HashSet<char>(
  67.             from bracket in Brackets
  68.             from closeChar in bracket.CloseChars
  69.             select closeChar
  70.         );
  71.  
  72.     public static void CheckString(string str) {
  73.         Stack<Bracket> currentState = new Stack<Bracket>();
  74.         for(int i=0; i<str.length; i++) {
  75.             if(BracketsByOpenChar.ContainsKey(str[i])) {
  76.                 currentState.Push(BracketsByOpenChar[str[i]]);
  77.             } else if(CloseChars.Contains(str[i])) {
  78.                 if(currentState.IsEmpty) {
  79.                     throw new ExtraClosingBracketException();
  80.                 }
  81.                 if(!currentState.Peek().closeChars.Contains(str[i])) {
  82.                     throw new BracketsMismatchException();
  83.                 }
  84.                 currentState.Pop();
  85.             }
  86.         }
  87.         if(!currentState.IsEmpty) {
  88.             throw new ExtraOpeningBracketException();
  89.         }
  90.     }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement