parenthese & stack

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 19
Reputation: raymyster has a little shameless behaviour in the past 
Solved Threads: 0
raymyster raymyster is offline Offline
Newbie Poster

parenthese & stack

 
0
  #1
Mar 21st, 2009
Use the stack class in a program that reads a String, one character at a time, and determine whether the String contains balanced parentheses, that is , for each left parenthesis ( if there any ) there is exactly one matching right parenthesis later in the String .


so the issue is
if (xx) => correct
if ((xx)) =>correct
if(((x) => false .....

so i managed to do a big part of it, i just need to fix this last part:
bool stack::balanced(string st)
{
	string strtmpl; // creating a string to hold the input in it
	strtmpl = st; //creating a string template to store the string entered by the user
	stack v; // overloading a stack

		int xx; // creating a variable to hold string length in it
		xx = strtmpl.length(); // filling variable xx with string length

		for(int z=0;z<=xx;z++)
		{
			v.puch(strtmpl[z]); //pushing the string into a stack
		}

		int i=0;
		int n =0;
		//cout<<i;
		//cout<<(char(v.StackArray[v.stacktop-1]));
		while(char(v.StackArray[i]) == '(')
		{
			if ((char(v.StackArray[v.stacktop-1])) == ')' && strtmpl[i] == '(')
			{
				if (char(v.StackArray[v.stacktop-1]) == ')')
				{
					n++;
				}
				v.pop();
				
				//cout<<char(v.StackArray[v.stacktop-1]);
				//i++;
				//xx--;
				//return 1;
			}
			//else
			//{
				//cout<<"bad!";
			//	n++;
				
				//xx--;
			//}
			i++;
			
		}
		if (n++ != i++)
				cout<<"bad";
			else if ((char(v.StackArray[v.stacktop-1])) != ')')
				cout<<"good";
		return 0;
}[/CODE]
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: parenthese & stack

 
0
  #2
Mar 21st, 2009
People generally like a more specific question. What exactly is the problem?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: raymyster has a little shameless behaviour in the past 
Solved Threads: 0
raymyster raymyster is offline Offline
Newbie Poster

Re: parenthese & stack

 
-2
  #3
Mar 22nd, 2009
i fixed it anyway yesterday
i really hate this about daniweb everytime i ask a question no one really helps, its like the answers i receive always"
i cant help you
i dont understand your question
this is too long to be asked on a forum
unlike 10 other different forums, daniweb is the worsest in helping this attitude is not present on any other forum...

anyway this is the solution for someone to benefit in the future if he was around the same problem:


bool stack::balanced(string st)
{
string strtmpl; // creating a string to hold the input in it
strtmpl = st; //creating a string template to store the string entered by the user
stack v; // overloading a stack
int flag =0;
int xx; // creating a variable to hold string length in it
xx = strtmpl.length(); // filling variable xx with string length

for(int z=0;z<=xx;z++)
{
v.puch(strtmpl[z]); //pushing the string into a stack
}

int i=0;

//while(char(v.StackArray[i]) == '(' || char(v.StackArray[xx]) == ')') you can iterate this way
while(strtmpl[i] == '(' || strtmpl[xx-1] == ')') // or iterate this way does not mater
{
if ((char(v.StackArray[v.stacktop-1])) == ')' && strtmpl[i] == '(') //check if the begining of the string & the end contain ()
{
v.pop(); // remove from stack
flag = 1; //success
}
else
{
if ((strtmpl[i] != '(') || (char(v.StackArray[v.stacktop-1]) != ')')) //
{
flag = 0; // failure
break; //stop while
}
}
i++; //iterate
xx--; //iterate
}
if (flag ==1)//in case of success
cout<<"Very good you parenthesized well "<<endl;
else //in case of failure
cout<<"Bad parethesis, please try again "<<endl;

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 109
Reputation: SeeTheLite is an unknown quantity at this point 
Solved Threads: 12
SeeTheLite SeeTheLite is offline Offline
Junior Poster

Re: parenthese & stack

 
0
  #4
Mar 22nd, 2009
The beauty about stacks is that you can use them in so many ways to solve one problem; the simplest, would be to read a string in a loop and insert every '(' into a stack, while popping the stack once for every ')'. Under this logic, simply make the statement false if the stack is empty when you attempt to pop it, or if you still have a value in your stack when the statement ends.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: parenthese & stack

 
0
  #5
Mar 22nd, 2009
Originally Posted by raymyster View Post
i fixed it anyway yesterday
i really hate this about daniweb everytime i ask a question no one really helps, its like the answers i receive always"
i cant help you
i dont understand your question
this is too long to be asked on a forum
unlike 10 other different forums, daniweb is the worsest in helping this attitude is not present on any other forum...

anyway this is the solution for someone to benefit in the future if he was around the same problem:
Great way to never get helped here. Remind us again how much we're getting paid to help you. ALL forums, not just Daniweb, ask for clarification and effort by the posters. We're volunteering our time. Show some appreciation.

If you are the same person who posted here:

http://www.programmingtalk.com/showthread.php?p=134369

then Daniweb is not the only forum where people aren't willing to just do the whole thing and wrap it in a bow and deliver it to you.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: raymyster has a little shameless behaviour in the past 
Solved Threads: 0
raymyster raymyster is offline Offline
Newbie Poster

Re: parenthese & stack

 
0
  #6
Mar 23rd, 2009
this was in 12-01-07, 12:32 PM
well i didn't ask for anyone to do the H.W. for me, i just asked you people to check for the error in the if statement i made..

i am pro in another language & i help people all the time actually on another form, this is what programming forums are about,
if you like i can PM the forum i am talking about & you can see how friendly people are there & we actually do help each other.

thanks anyway, i appreciate the reply.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: parenthese & stack

 
0
  #7
Mar 23rd, 2009
Originally Posted by raymyster View Post
this was in 12-01-07, 12:32 PM
But is it you?

Originally Posted by raymyster View Post
well i didn't ask for anyone to do the H.W. for me, i just asked you people to check for the error in the if statement i made..
No, you didn't mention anything about an if statement or where in the code the problem was and whether there were any compile errors, etc. You posted a function and said you had a problem with it. WE would have had to have figured out all of that. nucleon simply asked for a more structured question.

Originally Posted by raymyster View Post
i am pro in another language & i help people all the time actually on another form, this is what programming forums are about,
I'm aware of what programming forums are about and lots of people here, including me, give tons of free help. You never gave anyone any opportunity to help you because you pulled an attitude right away. You had two threads on Daniweb so far asking for help, this one and an earlier one. In the first one, you apparently PM'd someone for help when you should have simply posted on the thread itself, so you got slammed a bit in the thread. In this one, you were simply asked for a more specific question. You are likely to get those responses on any forum.

Originally Posted by raymyster View Post
if you like i can PM the forum i am talking about & you can see how friendly people are there & we actually do help each other.
Please do, along with your user name. I'd be interested in seeing it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC