943,559 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 885
  • C++ RSS
Mar 21st, 2009
0

parenthese & stack

Expand Post »
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]
Reputation Points: -3
Solved Threads: 0
Light Poster
raymyster is offline Offline
26 posts
since Jan 2008
Mar 21st, 2009
0

Re: parenthese & stack

People generally like a more specific question. What exactly is the problem?
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Mar 22nd, 2009
-2

Re: parenthese & stack

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:


C++ Syntax (Toggle Plain Text)
  1. bool stack::balanced(string st)
  2. {
  3. string strtmpl; // creating a string to hold the input in it
  4. strtmpl = st; //creating a string template to store the string entered by the user
  5. stack v; // overloading a stack
  6. int flag =0;
  7. int xx; // creating a variable to hold string length in it
  8. xx = strtmpl.length(); // filling variable xx with string length
  9.  
  10. for(int z=0;z<=xx;z++)
  11. {
  12. v.puch(strtmpl[z]); //pushing the string into a stack
  13. }
  14.  
  15. int i=0;
  16.  
  17. //while(char(v.StackArray[i]) == '(' || char(v.StackArray[xx]) == ')') you can iterate this way
  18. while(strtmpl[i] == '(' || strtmpl[xx-1] == ')') // or iterate this way does not mater
  19. {
  20. if ((char(v.StackArray[v.stacktop-1])) == ')' && strtmpl[i] == '(') //check if the begining of the string & the end contain ()
  21. {
  22. v.pop(); // remove from stack
  23. flag = 1; //success
  24. }
  25. else
  26. {
  27. if ((strtmpl[i] != '(') || (char(v.StackArray[v.stacktop-1]) != ')')) //
  28. {
  29. flag = 0; // failure
  30. break; //stop while
  31. }
  32. }
  33. i++; //iterate
  34. xx--; //iterate
  35. }
  36. if (flag ==1)//in case of success
  37. cout<<"Very good you parenthesized well :)"<<endl;
  38. else //in case of failure
  39. cout<<"Bad parethesis, please try again :("<<endl;
  40.  
  41. return 0;
  42. }
Last edited by Nick Evan; Mar 16th, 2010 at 9:30 am. Reason: Added code-tags
Reputation Points: -3
Solved Threads: 0
Light Poster
raymyster is offline Offline
26 posts
since Jan 2008
Mar 22nd, 2009
0

Re: parenthese & stack

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.
Reputation Points: 38
Solved Threads: 13
Junior Poster
SeeTheLite is offline Offline
109 posts
since Mar 2009
Mar 22nd, 2009
0

Re: parenthese & stack

Click to Expand / Collapse  Quote originally posted by raymyster ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,371 posts
since Jan 2008
Mar 23rd, 2009
0

Re: parenthese & stack

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.
Reputation Points: -3
Solved Threads: 0
Light Poster
raymyster is offline Offline
26 posts
since Jan 2008
Mar 23rd, 2009
0

Re: parenthese & stack

Click to Expand / Collapse  Quote originally posted by raymyster ...
this was in 12-01-07, 12:32 PM
But is it you?

Click to Expand / Collapse  Quote originally posted by raymyster ...
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.

Click to Expand / Collapse  Quote originally posted by raymyster ...
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.

Click to Expand / Collapse  Quote originally posted by raymyster ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,371 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Program for a PIC in C++
Next Thread in C++ Forum Timeline: Set class variable once for multiple constructors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC