# Include <stdio. h>
# Include <stdlib. h>
# Define STACK_INIT_SIZE 100
# Define STACKINCREMENT 10
# Define OVERFLOW-2
# Define INFEASIBLE-1
# Define ERROR 0
# Define OK 1
Typedef char SElemType;
Typedef struct
{
SElemType * base;
SElemType * top;
Int stacksize;
} SqStack;
Int InitStack (SqStack * sq)
{
Sq-> base = (SElemType *) malloc (STACK_INIT_SIZE * sizeof (SElemType ));
If (! Sq-> base) exit (OVERFLOW );
Sq-> top = sq-> base;
Sq-> stacksize = STACK_INIT_SIZE;
Return OK;
}
Int GetTop (SqStack * sq, SElemType * e)
{
If (sq-> base = sq-> top) return ERROR;
* E = * (sq-> top-1 );
Return OK;
} // GetTop
Int Push (SqStack * sq, SElemType e)
{
If (sq-> top-sq-> base> = sq-> stacksize)
{
Sq-> base = (SElemType *) realloc (sq-> base, (sq-> stacksize + STACKINCREMENT) * sizeof (SElemType ));
If (! Sq-> base) exit (OVERFLOW );
Sq-> top = sq-> base + sq-> stacksize;
Sq-> stacksize + = STACKINCREMENT;
}
* Sq-> top ++ = e;
Return OK;
} // Push
Int Pop (SqStack * sq, SElemType * e)
{
If (sq-> base = sq-> top) return ERROR;
* E = * -- sq-> top;
Return OK;
} // Pop
Int Clear (SqStack * sq, SElemType e)
{
Sq-> top = sq-> base;
Return OK;
} // Clear
Int StackEmpty (SqStack * sq)
{
If (sq-> base = sq-> top) return OK;
Else return ERROR;
} // StackEmpty
Int Match (SElemType ch, SElemType str)
{
If (ch = '(' & str = ') return OK;
Else if (ch = '[' & str = ']') return OK;
Else if (ch = '{' & str = '}') return OK;
Else return ERROR;
}
Void BracketMatch (SElemType * str)
{
SqStack S;
Int I;
Char ch;
InitStack (& S );
For (I = 0; str [I]! = '#'; I ++)
{
Switch (str [I])
{
Case '(':
Case '[':
Case '{':
Push (& S, str [I]);
Break;
Case ')':
Case ']':
Case '}':
If (StackEmpty (& S ))
{
Printf ("/n parentheses are redundant! /N "); return;
}
Else
{
GetTop (& S, & ch );
If (Match (ch, str [I]) Pop (& S, & ch );
Else
{
Printf ("/n left and right parentheses do not match! /N "); return;
}
} // Else
} // Switch
} //
If (StackEmpty (& S ))
Printf ("/n matched successfully! /N ");
Else
Printf ("/n left parenthesis is redundant! /N ");
} // BracketMatch
Void main ()
{// Input ended '#'
Int I;
Char ch;
Char str [100];
Gets (str );
BracketMatch (& str [0]);
}