#include <stdio.h>#include<malloc.h>//Malloc,realloc#include <math.h>//contains overflow#include <process.h>//exit ()#defineS_size 100//the space size of the stack#defineStackincreament 10//Add Spacestructsqstack{int*Base;//Bottom of Stack int*top;//Top of Stack intStackSize//Stack's current storage space};voidMain () {//Child function Declaration voidInitstack (Sqstack &s);//Initializing empty stacks intStackempty (Sqstack S);//empty sentence voidPush (Sqstack &s,inte);//into the stack voidPop (Sqstack &s,int&E);//out of the stack//main function StartSqstack s;//Initializing empty stacksInitstack (s); Charch[ -],*p;inte; P=ch; printf ("lose one meaning there is () []{} bracket expression: \ n"); Gets (CH); while(*p) {Switch(*p) { Case '{': Case '[': Case '(': Push (s,*p++); Break;//as long as the left parenthesis is in the stack Case '}': Case ']': Case ')':p op (s,e); if((e=='{'&& *p=='}') || (e=='['&& *p==']') || (e=='('&& *p==')')) P++; Else{printf ("parentheses do not match!"); exit (OVERFLOW);} Break; default:p + +;//other characters are moved back } } if(Stackempty (s)) printf ("Brace matching Successful"); Elseprintf ("Missing closing parenthesis! "); printf ("\ n");}voidInitstack (Sqstack &R) {S.Base=(int*) malloc (s_size*sizeof(int)); S.stacksize=s_size; S.top=s.Base;//Initializing empty stacks}intstackempty (Sqstack S) {if(S.Base==s.top)return 1; Else return 0;}voidPush (Sqstack &s,inte) {//into the stack if(S.top-s.Base>=s.stacksize) {S.Base=(int*) ReAlloc (S.Base, (s.stacksize+stackincreament) *sizeof(int)); S.top=s.Base+s.stacksize; S.stacksize+=stackincreament;} * (s.top) =e; S.top++; }voidPop (Sqstack &s,int&e) {//out of the stack if(S.Base!=s.top) {s.top--; E=*s.top;}}
Detect the characters in the expression, if the left parenthesis on the stack, if the right parenthesis out of the stack an element paired with it, the pairing will continue to access the next character, or exit. A non-parenthesis character is skipped.
C language implementation of bracket matching algorithm