This article is mainly implemented by the example: enter a bracket string, check in order, if the left parenthesis into the stack, if the right bracket is out of the stack a character to determine whether or not the corresponding, at the end of the need to determine whether the stack is empty, if not empty then does not match.
First, review the basics of the stack:
1. Define the structure of the stack and initialize a new stack:
struct stack
{
char strstack[stacksize];
int top;
};
void Initstack (Stack &s)
{
s.top=-1
}
2. Stack and Stack operations:
Char Push (Stack &s,char a)
{
if (s.top==stacksize-1)
{return
0;
}
s.top++;
S.strstack[s.top]=a;
return A;
}
Char Pop (Stack &s)
{
if (s.top==-1)
{return
0;
}
Char A=s.strstack[s.top];
s.top--;
return A;
}
3. Determine whether the stack is empty:
int Empty (stack &s,int re)
{
if (s.top==-1)
{return
1;
}
else
{return
0;
}
}
The above is the basic operation of the stack, the definition of a stack and initialization of a new stack, stack and into the stack operation, and to determine whether the stack is empty. Next, we will write a function, check each character of the string, the opening parenthesis is in the stack operation, the right bracket is a stack operation to see whether it matches, and finally determine whether it is empty to determine whether the match.
The main functional code is as follows:
int Check (char *str)
{
stack s;
Initstack (s);
int Strn=strlen (str);
for (int i=0;i<strn;i++)
{
char a=str[i];
Switch (a)
{case '
: Case
' [': Case
' {':
Push (s,a);
break;
Case '] ':
if (Pop (s)!= ' (')
{return
0;
}
break;
Case '] ':
if (POPs (s)!= ' [')
{return
0;
}
break;
Case '} ':
if (POPs (s)!= ' {')
{return
0;
}
break;
}
}
int re=0;
Re=empty (s,re);
if (re==1)
{return
1;
}
else
{return
0;
}
}
Since then, the question of the matching of the parentheses string has been resolved, and the complete compiled code is posted below.
The full instance code is as follows:
#include <iostream> #include <stdio.h> #include <string.h> using namespace std;
#define STACKSIZE struct Stack {char strstack[stacksize];
int top;
};
void Initstack (Stack &s) {s.top=-1} char Push (Stack &s,char a) {if (s.top==stacksize-1) {return 0;
} s.top++;
S.strstack[s.top]=a;
return A;
} char Pop (Stack &s) {if (s.top==-1) {return 0;
} Char A=s.strstack[s.top];
s.top--;
return A;
' int Empty (stack &s,int re) {if (s.top==-1) {return 1;
else {return 0;
} int Check (char *str) {stack S;
Initstack (s);
int Strn=strlen (str);
for (int i=0;i<strn;i++) {char a=str[i];
Switch (a) {case ': case ' [': Case ' {': Push (s,a);
Break
Case '] ': if (Pop (s)!= ' (') {return 0;
} break;
Case '] ': if (POPs (s)!= ' [') {return 0;
} break;
Case '} ': if (Pop (s)!= ' {') { return 0;
} break;
} int re=0;
Re=empty (S,re);
if (re==1) {return 1;
else {return 0;
} void Main () {char str[100];
cout<< "Please enter a string of less than 100 length:" <<endl;
cin>>str;
int Re=check (str); if (re==1) {cout<< "The string brackets you entered exactly match!"
"<<endl; else if (re==0) {cout<<) The string brackets you entered do not match!
"<<endl;
}
}
I hope that the example described in this article will help you with the learning of C + + algorithm design.