Home > Back-end >  C language parentheses matching problem, the program is running is not correct
C language parentheses matching problem, the program is running is not correct

Time:09-25

# include
# include
#include

# define STACK_INIT_SIZE 100
# define STACKINCREMENT 10
# define OK 1
# define the ERROR 0
# define OVERFLOW - 2
Typedef int the Status;
Typedef char SElemType;
Typedef struct/* stack statement */
{
SElemType * base;/* instructions stored data element space base address pointer */
SElemType * top; The stack pointer/* */

Int stacksize;/* stack storage size, in data element */
} SqStack;
The Status InitStack (SqStack * S)/* */
create an empty stack{
S - & gt; The base=(SElemType *) malloc (STACK_INIT_SIZE * sizeof (SElemType));
S - & gt; Top=S - & gt; The base;
S - & gt; Stacksize=STACK_INIT_SIZE;
Return OK;
}/* create a stack */
The Status StackEmpty (SqStack * S)
{
If (S - & gt; The top!=S - & gt; Base) return the ERROR;
Return OK;
}/* determine whether the stack is empty */
Status of Push (SqStack * S, SElemType e)
{
If (S - & gt; Top - S - & gt; Base & gt;=S - & gt; Stacksize)
{
S - & gt; The base=(SElemType *) realloc (S - & gt; Base, (S - & gt; Stacksize + STACKINCREMENT) * sizeof (SElemType));
if (! S - & gt; Base) exit (OVERFLOW);
S - & gt; Top=S - & gt; Base + S - & gt; Stacksize;
S - & gt; Stacksize +=STACKINCREMENT;
}
* S - & gt; Top++=e;
Return OK;
}/* stack */
The Status of Pop (SqStack * S, SElemType * e)
{
If (S - & gt; Top==S - & gt; Base) return the ERROR;
=* * e - S - & gt; Top;
Return OK;
}/* * the stack/
The Status Bracket (SqStack * S, char * STR)/* * determine whether matching/
{
Int I=0, flag1=0, flag2;
SElemType e;
While (STR [I]!='\ 0')
{
The switch (STR [I])
{
Case '(' : Push (S,' ('); break;
Case '[' : Push (S, ['); break;
Case '{' : Push (S, "{"); break;
Case ') : {Pop (S, & amp; E);
If (e!='(')
Flag1=1;
break;
}
Case '] ': {Pop (S, & amp; E);
If (e!='[')
Flag1=1;
break;
}
Case '}, {Pop (S, & amp; E);
If (e! )
='{'Flag1=1;
break;
}
Default: break;
}
If (flag1) break;
i++;
}
Flag2=StackEmpty (S);
if (! Flag1 & amp; & Flag2) printf (" It is ok! \n");
The else printf (" It is error! \n");
Return OK;
}
Int main ()
{
Char temp, flag='y'
While (flag=='y')
{
Char STR [255].
SqStack S;
InitStack (& amp; S);
Printf (" enter a cross: ");
The scanf (" % s ", STR);
The scanf (" % c ", & amp; Temp);
Bracket (& amp; S, STR);
Printf (" do you want to try again, enter y again: ");
The scanf (" % c ", & amp; The flag);
printf("\n");
}
Printf (" close. \ n ");
}

CodePudding user response:

Fyi:
 # include 
# include
# include
# define STACK_INIT_SIZE 10
# define STACK_GROW_SIZE 5
# define ELEMTYPE char
# define OK 1
# define the ERROR 0
Typedef struct {/* create a stack of the first node */
ELEMTYPE * base;
ELEMTYPE * top;
Int stacksize;
} SpStack;
Int InitStack (SpStack * s) {/* create empty stack and returns the first address */
S - & gt; The base=((ELEMTYPE *) malloc (STACK_INIT_SIZE * sizeof (ELEMTYPE)));
if (! S - & gt; Base) return the ERROR;
S - & gt; Top=s - & gt; The base;
S - & gt; Stacksize=STACK_INIT_SIZE;
Return OK;
}
Int StackEmpty (SpStack * s) {/* determine whether the stack is empty */
If (s - & gt; Top==s - & gt; Base) return OK;
The else return ERROR;
}
Int a Push (ELEMTYPE SpStack * s, e) {/* insert into the stack elements into the stack that */
If (s - & gt; Top - s - & gt; Base>=s - & gt; Stacksize) {/* determine whether stack full */
S - & gt; The base=((ELEMTYPE *) realloc (s - & gt; Base, (s - & gt; Stacksize + STACK_GROW_SIZE) * sizeof (ELEMTYPE)));
if (! S - & gt; Base) return the ERROR;
S - & gt; Stacksize +=STACK_GROW_SIZE;
S - & gt; Top=s - & gt; Base + s - & gt; Stacksize;
}
* s - & gt; Top++=e;
Return OK;
}
Int Pop (SpStack * s, ELEMTYPE * e) {/* let top element output that is in turn the stack */
If (StackEmpty (s)) return the ERROR;
=* * e - s - & gt; Top);
Return OK;
}
Int Comp (ELEMTYPE a, ELEMTYPE b) {
If (a=='(' & amp; & B! )
=') '| | (a=='[' & amp; & amp; b!='] ')
| | (a=='{' & amp; & B!={'} '))
return ERROR;
} the else return OK;
}
Int Count (SpStack * s) {
ELEMTYPE e [STACK_INIT_SIZE * 2];
ELEMTYPE e1;
int i;

InitStack (s);
The fgets (e, STACK_INIT_SIZE * 2, stdin);
If (=='\ n' e [strlen (e) - 1)) e [strlen (e) - 1)=0.
Printf (" % s \ n ", e);
for (i=0; E [I]!='\ 0'; I++) {
The switch (e) [I] {
Case '('
Case [' : '
Case '{' :
Push (s, e [I]);
break;
Case ') :
Case '] ':
Case '} ':
If (StackEmpty (s)) {
Printf (" % * s ↖ right parenthesis excess \ n ", I + 1, "");
Return (ERROR);
} the else Pop (s, & amp; E1);
if (! Comp (e1, e [I]) {
Printf (" % * s ↖ about matching error \ n ", I + 1, "");
Return (ERROR);
}
}
}
if (! StackEmpty (s)) {
Printf (" % * s ↖ left parenthesis excess \ n ", I, "");
Return (ERROR);
} else {
Printf (" match correct \ n ");
Return (OK);
}
}
Void main () {
SpStack s;
Count (& amp; S);
Free (s.b ase);
}
  • Related