| | |
parenthese & stack
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 19
Reputation:
Solved Threads: 0
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:
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]•
•
Join Date: Jan 2008
Posts: 19
Reputation:
Solved Threads: 0
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;
}

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;
}
•
•
Join Date: Mar 2009
Posts: 109
Reputation:
Solved Threads: 12
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.
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
•
•
•
•
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:
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.
•
•
Join Date: Jan 2008
Posts: 19
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
But is it you?
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.
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.
Please do, along with your user name. I'd be interested in seeing it.
•
•
•
•
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,
Please do, along with your user name. I'd be interested in seeing it.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Program for a PIC in C++
- Next Thread: Set class variable once for multiple constructors
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






