Home > Back-end >  Parentheses matching problem
Parentheses matching problem

Time:11-04

#include
#include
Typedef char stackelemtype;
# define MAXSIZE 100
//definition - order stack
Typedef struct
{
Stackelemtype elem [MAXSIZE];
Char top;
} seqstack;
//order stack initialization
Void initstack (seqstack * s)
{
S - & gt; Top=1;
}
//order into the stack
Char push (seqstack * s, stackelemtype x)
{
If (s - & gt; Top==MAXSIZE - 1)
return 0;
The else
{
S - & gt; Top++;
S - & gt; Elem [s - & gt; top]=x;
return 1;
}
}
//determine whether initialization
Int isempty (seqstack * s)
{
If (s - & gt; Top==1)
return 1;
The else
return 0;
}
//order a stack
Char pop (seqstack * s, stackelemtype * x)
{
If (s - & gt; Top==1)
return 0;
The else
X={* s - & gt; Elem [s - & gt; top];
S - & gt; Top -;
return 1;
}
}
//in order to read stack top element
Char gettop (seqstack * s, stackelemtype * x)
{
If (s - & gt; Top==1)
return 0;
The else
{
* x=s - & gt; Elem [s - & gt; top];
return 1;
}
}
//match
Int the match (char ch, char STR)
{
If (ch=='(' & amp; & STR==') ')
return 1;
Else if (ch=='[' & amp; & amp; STR=='] ')
return 1;
Else if (ch=='{' & amp; & STR=='} ')
return 1;
The else
return 0;
}

//matching brackets
Void bracketmatch (char * STR)
{
Seqstack s;
int i;
char ch;
Initstack (& amp; S);
for(i=0; STR [I]!='\ n'; I++)
{
The switch (STR [I])
{
Case '('
Case [' : '
Case '{' :
Push (& amp; S, STR [I]);
break;
Case ') :
Case '] ':
Case '} ':
If (isempty (& amp; S))
{
Printf (" \ n right parenthesis excess!" );
return;
}
The else
{
Gettop (& amp; S, & amp; Ch);
If (match (ch, STR [I]))
Pop (& amp; S, & amp; Ch);
The else
{
Printf (" \ n corresponds to the left and right parentheses is not the same!" );
return;
}
}
}
}
If (isempty (& amp; S))

Printf (" \ n parentheses match!" );

The else
Printf (" \ n left parenthesis excess!" );
}
Void main ()
{
Char a, [50].
Printf (" please enter a string brackets: ");
The scanf (" % s ", a);
Bracketmatch (a);
}
Where is the wrong

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