Hi.I have to write a c++ programm with a stack struckture.The task is to count if the number of such "(" brackets is equal to ")" these ones.If for example we have (((( )))),then we start from begin and end.If a bracket is "(" we add it to a stos,and if ")" we should remove previous one it seems to me.So in the end stack has to be empty.I'm not good at writing a programm so please help me.And it is also necessary to define the algorythm's time complexity.thanx a lot
#include <iostream>
usingnamespace std;
int main() {
char str[80]; //We will use it to store the brackets
int curr=0; //This will contain current bracket level
cin>>str;
for(int i=0; str[i]!='\0' ;i++) // loop till array ends
{
if(str[i] == '(' ) curr++; //increase the level
elseif(str[i]==')') curr--; //decrease the level
if(curr<0){
break; // the no of closing brackets are more than opening ones, no need to move on
}
}
if(curr==0)
{
cout<<"Stack Empty"; //Opening and closing brackets are equal in no
}
else
{
cout<<"Stack not Empty";
}
return 0;
}
Please don't provide code to someone who has not offered up any of their own. This is almost definitely a homework project, so creating a culture where people can come and ask for others to do their homework is not something you want to be promoting. If the person isn't good at writing programs, then what good will it do for them to be hand fed the code? They'll never improve unless they put forth at least a little effort and see where they're going wrong. Please ask for at least a rough draft of what they've come up with.
If all it took to learn to program was looking at other people's code we'd all be perfect by now. O_o