Bracket Checker

This is a discussion on Bracket Checker within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Sebastiani So basically, when you encounter an 'opening' symbol, simply push the corresponding 'closing' symbol onto the ...

  1. #16
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Quote Originally Posted by Sebastiani View Post
    So basically, when you encounter an 'opening' symbol, simply push the corresponding 'closing' symbol onto the stack. If you come across a 'closing' symbol, just check the stack. If it isn't what you expected, then there is an error, otherwise, discard the current symbol (pop the stack) and proceed. If there is an attempt to pop an empty stack, or the stack is not empty when you are done, then it is ill-formed.


    I am really very sorry. I dint get what you are trying to say =((!!
    When we encounter an opening symbol why we push the corresponding closing symbol??

  2. #17
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Fatima Rizwan View Post
    I am really very sorry. I dint get what you are trying to say =((!!
    When we encounter an opening symbol why we push the corresponding closing symbol??
    It's just a convention - you can do it either way, really. Whatever seems most logical to you.

  3. #18
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    while ( input != 13 )
          {
           if input is '(' or '{' or '[' or ')' or '}' or ']' 
              {
              if input is '(' or '{' or '['
                  push it into the stack.
              else if input is  ')' or '}' or ']' 
                  {
                  if stack is empty
                           report error.
                  pop cell from stack
                  if cell "matches input" 
                           we are ok.
                  else if cell dont "matches input" or stack is empty 
                           report error.
                  }
              }
          }
       if  the stack is not empty
            report error.
    How about this??

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Very good.

    A couple of possible issues to be aware of, though:
    1) The stopping condition should be on the null-terminator (0, the end of the string), not a newline (13).
    2) The condition 'if cell "matches input"' would be incorrect, considering that if the input, for instance, is a closing bracket, then the cell should be an opening bracket. This is actually why it was suggested to push the 'closing' symbol, as it would make the checking logic a little simpler.
    3) It's probably necessary to implement an 'is_empty' member function, or similar.

    Everything else looks pretty close to being correct. Try to implement it in code now, and see what you come up with.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MISRA C Checker open source
    By huwan in forum Tech Board
    Replies: 8
    Last Post: 10-27-2009, 08:13 AM
  2. code checker crap
    By KIBO in forum C Programming
    Replies: 11
    Last Post: 09-11-2008, 02:00 AM
  3. Spell checker poem.
    By adrianxw in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-13-2004, 09:49 AM
  4. spell checker in c needs help
    By madmax in forum C Programming
    Replies: 3
    Last Post: 03-13-2003, 08:36 AM
  5. Sorting through delimiting characters
    By Aluvas in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2002, 12:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21