# include
# define stack_init_size 100
# define stack_increment 10
# define TRUE 1
# define FALSE 0
Typedef char ElemType;
Typedef struct {
ElemType * base;
ElemType * top;
Int stacksize;
} SqStack;
Void init (SqStack & amp; S)
{
S.b ase=(ElemType *) malloc (stack_init_size * sizeof (ElemType));
If (S.b ase)
{
exit(-1);
}
S.t op=S.b ase;
S.s tacksize=stack_init_size;
}
Void getTop (SqStack S, ElemType & amp; E)
{
If (S.t op==S.b ase)
{
return;
}
E=* (S.t op - 1);
}
Void pop (SqStack & amp; S, ElemType & amp; E)
{
If (S.t op=S.b ase)
{
return;
}
E=* - S.t op;
}
Void push (SqStack & amp; S, ElemType c)
{
If (S.t op - S.b ase==S.s tacksize)
{
S.b ase=(ElemType *) realloc (S.b ase, (S.s tacksize + stack_increment) * sizeof (ElemType));
if(! S.b ase) {
exit(-1);
}
S.t op=S.b ase + S.s tacksize;
S.s tacksize +=stack_increment;
}
* (S.t op++)=c;
}
Int isEmpty (SqStack S)
{
If (S.t op==S.b ase)
{
Return TRUE;
}
The else
{
Return FALSE;
}
}
Int isLeft (char & amp; C)
{
If (c=='(' | | c==' [' | | c=='{')
{
Return TRUE;
}
The else
{
Return FALSE;
}
}
Int isMatch (char & amp; C, char & amp; E)
{
If ((c==') & amp; & E=='(') | | (c=='] '& amp; & E=='[') | | (c=='} '& amp; & amp; e==' {'))
{
Return TRUE;
}
The else
{
Return FALSE;
}
}
Int checkBrackets (char [] s)
{
int i=0;
SqStack S;
Init (S);
ElemType c, e,
While ((c=s [i++])!='\ 0')
{
If (isLeft (c))
{
Push (S, c);
}
The else
{
If (isEmpty (S))
{
return 0;
}
The else
{
GetTop (S, e);
If (isMatch (c, e))
{
Pop (S, e);
}
The else
{
return 0;
}
}
}
}
If (isEmpty (S))
{
return 1;
}
return 0;
}
Int main (void)
{
Char [] s={' [', '(',') ', '] ', '{','} '};
Int m=checkBrackets (s);
If (m==1)
{
Printf (" yes ");
}
The else
{
Printf (" no ");
}
return 0;
}
Topic: 1, the input from the keyboard a includes only (,), [and], a total of 6 {and} character string of brackets, using stack to check its legitimacy, such as: [] {}, [{} ()] legal; [] ({}, {} is illegal, in order to facilitate I use arrays instead of directly input, ask how change can be a legal character input yes, no illegal input
CodePudding user response:
CodePudding user response: