Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    C++ Help with matching delimiters

    Alright, I will say this right upfront, this is a homework assignment. I do have most of it done and only need a little help. I read in a file and I have to check for matching brackets ( '(', '[', '{' ). I have to checking to see that there are the right number of them already, I just need to check to be sure they match up and aren't in some stupid order or something ( like this for example: "([{ ]( )})" ). We are required to use stacks in this. When I read the file into the stack it all ends of backwards. My first thought was to create a second stack going the correct way and since one would start at the beginning and the other the end just keep comparing and popping them off however that wouldn't work on something like this: "([ ]{( )}) ". I know how to do it in English logic, I am just not sure how to do it in C++ logic. If this is a problem that you can help on and could help me with the logic of it, I can do the code.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    you aren't supposed to read all of the file into the stack (if thats not what ur doing...thats the impression i got...) the way to go about it would be to push into the stack whenever u encounter a starting bracket and whenever u encounter a closing bracket. u have to pop the stack and see if the closing bracket matches the the last opened one...if it doesn't theres something wrong in the order of closing the brackets
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I am only reading into the stack the brackets. So are you saying that when I read a closing bracket I need to pop off things into another stack to hold them and then check for the opening bracket? I think I get what you are saying, but it would say that "([{ ]( )})" is valid, but it isn't. Unless I misunderstood you.

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    No, No, I think you are mis-understanding what shyam is saying.

    When you encounter an opening character, push it onto the stack. When you encounter a closing character, check to see if the item on the top of the stack is the matching opening character (e.g. if you see ) make sure that ( is on the top of the stack). Your stack class should have a top or peek method which will tell you what is on the top of the stack.

    If the characters don't match, the data isn't valid and the symbols are out of order. If it does match, you pop the top item off the stack. Also when you are done parsing the file, if there are any items left on the stack, then the data isn't valid either.

    You only need one stack to accomplish this.
    OracleGuy

  • Users who have thanked oracleguy for this post:

    chiefbutz (10-20-2007)

  • #5
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oracleguy View Post
    No, No, I think you are mis-understanding what shyam is saying.

    When you encounter an opening character, push it onto the stack. When you encounter a closing character, check to see if the item on the top of the stack is the matching opening character (e.g. if you see ) make sure that ( is on the top of the stack). Your stack class should have a top or peek method which will tell you what is on the top of the stack.

    If the characters don't match, the data isn't valid and the symbols are out of order. If it does match, you pop the top item off the stack. Also when you are done parsing the file, if there are any items left on the stack, then the data isn't valid either.

    You only need one stack to accomplish this.


    Oh, ok I think I get what you mean. yeah, that makes a lot of sense... THANK YOU!

    I got it working thank you everyone for you help
    Last edited by chiefbutz; 10-20-2007 at 07:31 PM. Reason: I got it to work


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •