Application of buyers in C + +---bracket matching

Source: Internet
Author: User

Just started to learn the data structure, with buyers wrote a classic application, bracket matching.


Algorithm ideas:

Input string, will ' (', ' [' press into buyers, encounter ') '] ', and then stack the top out buyers, brace matching, if the successful match, then continue, otherwise, the program ends, enter the mismatch information,

If ') ' is matched, then see if the buyers is empty, if it is empty, the input matches, otherwise, the output symbol number does not match.


#include <iostream>using namespace std; #define Max Stack{int top;int Maxsize;char *element;}; typedef struct STACK stack;void Init (stack *s,int N) {if (n>0) {s->top=-1; s->maxsize=n; S->element=new Char [s->maxsize];}} BOOL Isfull (Stack *s) {return (s->top==s->maxsize-1);} BOOL IsEmpty (Stack *s) {return (s->top==-1);} void push (Stack *s,char x) {if (! Isfull (S)) s->element[++s->top]=x;else{cout<< "Buyers full" &LT;&LT;ENDL;} Char pop (Stack *s) {if (! IsEmpty (S)) return s->element[s->top--];else{cout<< "Buyers Empty" <<endl;return-1;} int main () {Stack *s=new stack;init (S,max); bool Flag=true;bool flag_1=true;bool flag_2=true;cout<< "Please enter string:" <   <endl;char ch;while ((ch = getchar ()) = ' \ n ') {switch (ch) {case ' (': Push (S,CH);   Case ' [': Push (S,CH);   Case ') ': {if (pop (S)! = ' (' && flag_2) {cout<< "') '" << "does not match" <<endl;   Flag_1=false;   Flag=false;                  Break           } else {continue; }} Case ': {if (pop (S)! = ' [' && flag_1) {cout<< "'] '" << "symbol does not match" <<endl;   Flag_2=false;   Flag=false;                  Break            } else {continue; }}}}if (! IsEmpty (S) && flag) {cout<< "Quantity mismatch" &LT;&LT;ENDL;} else if (flag) {cout<< "Match" &LT;&LT;ENDL;} System ("pause"); return 0;}

Are simple to define, simple to function.

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.