I'm currently working on my first datastructure homework
The topic is "parenthesis matching"
The answer in my mind is simple,
just push the opening parenthesis to the stack , and when you meet the closing parenthesis pop it out.
After finishing my code , I submit it to our school's online judgement system
Only get 9 corrects out of 10 questions.
Here's my c++ code
Is there any situation that I missed in this code?? Thank you all!
/Question's here/ The question is that input a integer N < 1000 which means the testcases
and following N strings length < 1000 should be tested if it's a valid string
If yes , output Case N(from 1~N):Yes
No , output Case N(from 1~N):No
string may contain newline character
#Test cases
Input
2
[][]<>()[{}]
<{>}
Output
Case 1: Yes
Case 2: No
#include <iostream>
#include <string>
using namespace std;
class PARENTHE
{
public:
PARENTHE(int slength);
~PARENTHE();
int StackSize() const;
bool StackEmpty() const;
char top() const;
void Push(const char);
void Pop();
private:
char *str;
int slength;
int stop;
};
PARENTHE::PARENTHE(int slength)
{
str = new char [slength];
stop = -1;
}
PARENTHE::~PARENTHE()
{ delete [] str; }
inline int PARENTHE::StackSize() const
{ return stop+1; }
inline bool PARENTHE::StackEmpty() const
{
return (stop == -1);
}
inline char PARENTHE::top() const
{ return str[stop]; }
void PARENTHE::Push(const char c)
{
str[++stop] = c;
}
void PARENTHE::Pop()
{
stop--;
}
int main()
{
int t;
while( cin>>t )
{
int i = 0;
while( i < t )
{
string temp;
cin>>temp;
if(temp == "\n")
{
cout<<"Case "<<++i<<": "<<"Yes"<<endl;
break;
}
PARENTHE S( 1001 );
bool check = false;
for( int it = 0; it < temp.size() ; ++it )
{
if( temp[it] == '{' || temp[it] == '[' || temp[it] == '(' || temp[it] == '<' )
S.Push(temp[it]);
else if ( temp[it] == '}' || temp[it] == ']' || temp[it] == '>' || temp[it] == ')' )
{
if(!S.StackEmpty())
{
if(( S.top() == '{' && temp[it] == '}' ) || ( S.top() == '(' && temp[it] == ')' ) || ( S.top() == '[' && temp[it] == ']' ) || ( S.top() == '<' && temp[it] == '>' ) )
{
S.Pop();
}
else { break; }
}
else { break; }
}
if ( it == (temp.size()-1) && (!S.StackSize()))
{
check = true;
}
}
if(check)
cout<<"Case "<<++i<<": "<<"Yes"<<endl;
else
cout<<"Case "<<++i<<": "<<"No"<<endl;
}
}
return 0;
}