#include <iostream>
#include <cassert>
#include <cstring>
#include <string>
using namespace Std;
int maxlength=100;
const int stackincreament = 20;
Template<class t>
Class stack{
Public
Stack () {}
virtual void Push (const t& x) = 0;
virtual bool Pop (t& x) = 0;
virtual bool Getpop (t& x) = 0;
virtual bool IsEmpty () = 0;
virtual bool Isfull () = 0;
virtual int getsize () = 0;
};
Template<class t>
Class Seqstack:public stack<t>{
Public
Seqstack (int sz=50);
~seqstack () {delete []elements;}
void Push (const t& x);
BOOL Pop (t& x);
BOOL Getpop (t& x);
BOOL IsEmpty () {return (top==-1)? True:false;}
BOOL Isfull () {return (top==maxsize-1)? True:false;}
int GetSize () {return top+1;}
void Makeempty () {top=-1;}
Private
T *elements;
int top;
int maxSize;
void Overflowprocess ();
};
Template<class t>
Seqstack<t>::seqstack (int sz) {
Top=-1;
Maxsize=sz;
elements = new T[maxsize];
ASSERT (Elements!=null);
}
Template<class t>
void Seqstack<t>::P ush (const t& x)
{
if (Isfull () ==true)
Overflowprocess ();
Elements[++top]=x;
}
Template<class t>
BOOL Seqstack<t>::P op (t& x)
{
if (IsEmpty () ==true)
return false;//cout<<top<<endl;
x=elements[top--];
return true;
}
Template<class t>
BOOL Seqstack<t>::getpop (t& x)
{
if (IsEmpty () ==true)
return false;
X=elements[top];
return true;
}
Template<class t>
void Seqstack<t>::overflowprocess () {
T *newarray = new T[maxsize+stackincreament];
if (newarray==null)
{
cout<< "Memory allocation failed" <<endl;
return;
}
for (int i=0;i<=top;i++)
{
Newarray[i]=elements[i];
}
Maxsize=maxsize+stackincreament;
delete []elements;
Elements =newarray;
}
void Printmatchedpairs (string expression)
{
seqstack<int> s (maxLength);
Seqstack<int> A (maxLength);
Seqstack<int> b (maxLength);
int J,c,d,r,t=0,length=expression.length ();
int e=0,m=0;
for (int i=1;i<=length+1;i++)
{
if (expression[i-1]== ' (')
S.push (i);
else if (expression[i-1]== ') ')
{
for (int r=1;r<=i;r++)
if (expression[r-1]== ') ' | | expression[r-1]== '} ')
t++;
if (t!=0)
{
cout<< "Failure";
m++;
Break
}
S.pop (j);
}
else if (expression[i-1]== ' [')
A.push (i);
else if (expression[i-1]== '] ')
{
for (int r=1;r<=i;r++)
if (expression[r-1]== '} ')
e++;
if (e!=0)
{
cout<< "Failure";
m++;
Break
}
A.pop (j);
}
else if (expression[i-1]== ' {')
B.push (i);
else if (expression[i-1]== '} ')
B.pop (d);
}
if (m==0)
{
if (S.isempty () ==true&&a.isempty () ==true&&b.isempty () ==true)
{
cout<< "Success";
}
Else
cout<< "Failure";
}
}
int main ()
{
string expression;
cin>>expression;
Printmatchedpairs (expression);
return 0;
}
C + + implementation of the sequential stack and its use to implement parentheses matching