...

View Full Version : C++ Help with matching delimiters



chiefbutz
10-20-2007, 03:33 PM
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.

shyam
10-20-2007, 03:51 PM
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

chiefbutz
10-20-2007, 04:40 PM
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.

oracleguy
10-20-2007, 06:10 PM
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.

chiefbutz
10-20-2007, 06:13 PM
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! :D

I got it working thank you everyone for you help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum