The main implementation of the example: Enter a parenthesis string, and then check, if the left parenthesis into the stack, if the right parenthesis out of the stack a character to determine whether it corresponds to, at the end of the need to determine whether the stack is empty, if not null does not match.
First review the basics of stacks:
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. Out of stack and into the stack operation:
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 if 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, define a stack and initialize a new stack, out of the stack and into the stack operation, and determine whether the stack is empty. Next, a function is written, each character of the string is checked, the left parenthesis is in the stack, the right parenthesis is stacked to see if it matches, and the final judgment is null to determine if it matches.
The main function 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 (POP (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; }}
Since then, the question of how to match 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 100struct 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 (Pop (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<< "you enter the string parentheses exactly match! "<<endl; } else if (re==0) {cout<< "The string brackets you entered do not match! "<<endl; }}
Using stacks to determine string matching problems