#include<iostream>
#include<cctype>
using namespace std;
class stack
{
public:
int tp,size;
void push(char a);
void pop();
char b[10];
int isfull();
int isempty();
stack (int c)
{
size=c;
tp=-1;
}
stack()
{
size=0;tp=-1;
}
};
void stack::push(char a)
{
if(isfull())
cout<<"stack is full";
else
b[++tp]=a;
}
void stack::pop()
{
if(!isempty())
tp--;
}
int stack::isfull()
{
if(tp>=size-1)
return 1;
else return 0;
}
int stack::isempty()
{
if(tp<=-1)
return 1;
else return 0;
}
int main()
{int i=0,m=0;
stack s(8);
char a[10],b[10];
cin.getline(a,10);
char pl='(',pr=')',sl='{',sr='}',ql='[',qr=']';
while(a[i]!='\0'&&m==0)
{
if((a[i]==pl)||(a[i]==sl)||(a[i]==ql))
s.push(a[i]);
else
{
if(a[i]==pr)
{if(s.isempty()||s.b[s.tp]!=pl)
m=1;else
{
m=0;s.pop();}
}
if(a[i]==sr)
{
if(s.isempty()||s.b[s.tp]!=sl)
m=1;else
{
m=0;s.pop();}
}
if(a[i]==qr)
{
if(s.isempty()||s.b[s.tp]!=ql)
m=1;
else
{
m=0;s.pop();}
}
} i++;}
if(m==0)
cout<<" balanced";
else
cout<<" 0000000unbalanced";
return 0;
}
#include<cctype>
using namespace std;
class stack
{
public:
int tp,size;
void push(char a);
void pop();
char b[10];
int isfull();
int isempty();
stack (int c)
{
size=c;
tp=-1;
}
stack()
{
size=0;tp=-1;
}
};
void stack::push(char a)
{
if(isfull())
cout<<"stack is full";
else
b[++tp]=a;
}
void stack::pop()
{
if(!isempty())
tp--;
}
int stack::isfull()
{
if(tp>=size-1)
return 1;
else return 0;
}
int stack::isempty()
{
if(tp<=-1)
return 1;
else return 0;
}
int main()
{int i=0,m=0;
stack s(8);
char a[10],b[10];
cin.getline(a,10);
char pl='(',pr=')',sl='{',sr='}',ql='[',qr=']';
while(a[i]!='\0'&&m==0)
{
if((a[i]==pl)||(a[i]==sl)||(a[i]==ql))
s.push(a[i]);
else
{
if(a[i]==pr)
{if(s.isempty()||s.b[s.tp]!=pl)
m=1;else
{
m=0;s.pop();}
}
if(a[i]==sr)
{
if(s.isempty()||s.b[s.tp]!=sl)
m=1;else
{
m=0;s.pop();}
}
if(a[i]==qr)
{
if(s.isempty()||s.b[s.tp]!=ql)
m=1;
else
{
m=0;s.pop();}
}
} i++;}
if(m==0)
cout<<" balanced";
else
cout<<" 0000000unbalanced";
return 0;
}
No comments:
Post a Comment