features: Mainly matches the parentheses in the string (including "{}", "[]", "()"), and when the match succeeds, the print match succeeds or the print match fails.
Algorithm idea:
Start the scan from the first character of the string, if the normal character is ignored, if the left symbol is the stack operation, and when the right symbol is encountered, pops the top element from the stack and matches it;
At the end of the match:
Success: All characters were scanned and the stack was empty;
Failed: Match failed or all character scan completed but stack is not empty;
Attaching: The debugging process, some small problems, have time to revise;
. h file #pragma once #include <stdio.h> #include <stdlib.h> #include <string.h> #define TRUE 1 #define Fals
E 0 #define SIZE typedef char ELEMTYPE;
typedef struct STACK {elemtype *base;
int top;
int stacksize;
}stack;
1. Create an empty stack void Init (stack *s);
2. into the stack int Push (stack *s,elemtype D);
3. Stack int Pop (stack *s,elemtype *mptr);
4. Get stack top element int GetTop (stack *s,elemtype *e);
5. Judge stack non-null int Empty (stack *s);
6. Print stack element void List (stack *s);
7. Match bracket int MyFunc (char *mstring);
The. c file #include "Stack.h"//1. Create an empty stack void Init (Stack *s) {s->base= (Elemtype *) malloc (sizeof (elemtype) *size);
s->top=0;
s->stacksize=size;
return; //2. into stack int Push (stack *s,elemtype D) {//Stack full if (s->top>=s->stacksize) {s->base= (elemtype
*) ReAlloc (s->base,sizeof (s->stacksize+1) *sizeof (elemtype));
Determine if stack memory space is allocated successfully if (S->base==null) return false;
s->stacksize++; } S->BASE[S->top++]=d;
return true;
//3. stack int Pop (stack *s,elemtype *mptr) {if (s->top==0) return false;
else {*mptr=s->base[s->top--];
return true;
//4. Gets the top element int GetTop (stack *s,elemtype *e) {if (s->top==0) return false;
*e=s->base[--s->top];
return true;
//5. Judgment stack non-null int Empty (stack *s) {if (s->top==0) return true;
return false;
//6. Print stack element void List (Stack *s) {if (s->top==0) return;
while (s->top-1>=0) {printf ("data:%c\n", s->base[s->top-1]);
s->top--;
//7. Match parentheses int MyFunc (char *mstring)//mstring->[26* (12+8)/(45-29)] {int i=0,flag=1;
Stack S1;
Elemtype e;
Init (&S1);
while (mstring[i]!= ' ") {switch (Mstring[i]) {case ' (':P ush (' &s1, ' (');
Case ' [':P ush (&S1, ' [];
Case ' {':P ush (&s1, ' {'); Case ') ': GetTop (&s1,&amP;E);
if (e== ' (') Pop (&s1,&e);
else flag=0;
Break
Case '] ': GetTop (&s1,&e);
if (e== ' [') Pop (&s1,&e);
else flag=0;
Break
Case '} ': GetTop (&s1,&e);
if (e== ' {') Pop (&s1,&e);
else flag=0;
Break
} ++i;
} if (Empty (&S1) &&flag) return true;
else return false;
test.c #include "Stack.h" int main (void) {char str[]= "[20* (16+4)/(15-6)]"; if (MyFunc (str)) printf ("Match succeeded.")
\ n "); else printf ("Match failed.")
\ n ");
System ("pause");
return 0;
}