Home > Back-end >  For data structure parentheses matching problem, the program can be run but the result is always dis
For data structure parentheses matching problem, the program can be run but the result is always dis

Time:09-25

#include
#include
#include
using namespace std;
Const int STACK_INIT_SIZE=100;
Const int STACKINCREMENT=10;
Typedef char SElemType;
Typedef struct
{
SElemType * elem;
int top;
Int stacksize;
Int increment;
} SqStack;
//initialize the stack
Void InitStack (SqStack& S, int maxsize=STACK_INIT_SIZE, int incresize=STACKINCREMENT)
{
S.e lem=new SElemType [STACK_INIT_SIZE];
S.t op=1;
S.s tacksize=maxsize;
S.i ncrement=incresize;
}
//into the stack
Void Push (SqStack& S, SElemType e)
{
If (S.t op==S.s tacksize)
{
S.s tacksize=S.s tacksize + STACKINCREMENT;
SElemType * elem=new SElemType [S.s tacksize];
for(int i=0; i<=S.t op; I++)
Elem [I]=S.e lem [I];
The delete [] S.e lem.
S.e lem=elem;
}
S.e lem [+ + S.t op]=e;
}
//check empty stack
Bool StackEmpty (SqStack S)
{
If (S.t op==1)
return true;
else return false;
}
//take the top element
Bool GetTop (SqStack S, SElemType & amp; E)
{
If (S.t op==1) return false.
E=S.e lem [S.t op];
return true;
}

//element to stack the stack
Bool Pop (SqStack & amp; S, SElemType& E)
{
If (S.t op==1) return false.
E=S.e lem [S.t op];
S.t op -;
return true;
}

//matching brackets
Bool matching (char exp [])
{
SqStack S;
InitStack (S);
Int state=1;
Char e;
Char ch=* exp++;
While (ch!='#' & amp; & State)
{switch (ch)
{
The switch (ch) {
Case '('
{
Push (S, ch);
break;
}
Case [' : '
{
Push (S, ch);
break;
}
Case '{' :
{
Push (S, ch);
break;
}

Case ') :
{

if (! StackEmpty (S) & amp; & GetTop (S, e)=='(')
Pop (S, e);
The else state=0;
break; }
Case '] ':
{

if (! StackEmpty (S) & amp; & GetTop (S, e)=='[')
Pop (S, e);
The else state=0;
break; }
Case '} ':
{
if (! StackEmpty (S) & amp; & GetTop (S, e)=='{')
Pop (S, e);
The else state=0;
break; }
}
Default: break;
}
Ch=* exp++;
}
If (state& & StackEmpty (S))
return true;
else return false;


}
//the main function
Int main ()
{
Int Max=50;//array capacity
Char a, (Max).
Cout<" Input expression brackets (end with "#") "& lt; Cin> a;
If (matching) (a)
{
Cout<" Parentheses match "& lt; Cout<" \n";
}
The else cout<" Brackets do not match "& lt; }

  • Related