C language implementation of bracket matching algorithm

Source: Internet
Author: User

#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&AMP;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.