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

Source: Internet
Author: User

Just started learning data structures. Wrote a classic application with buyers, with parentheses matching.


Algorithm ideas:

When you enter a string. Will ' (', ' [') press into the buyers. Encountered ') ', then the stack is buyers. to match parentheses. Assume a successful match. will continue. Otherwise. End of program, input mismatch information,

Assuming ') ' is matched, then the inferred buyers is NULL, assuming null, then the input matches. Otherwise, the number of output symbols 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;}

is a simple definition. Simple function.

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

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.