Bracket matching problem (c + +, stack)

Source: Internet
Author: User

Original address: http://www.cppblog.com/GUO/archive/2010/09/12/126483.html

/* Bracket matching problem, more classic, using stacks to implement (from the Internet) 1. There are four possibilities for matching parentheses: ① the right and left brackets are incorrect ② the closing parenthesis is more than the opening parenthesis ③ the opening parenthesis is more than the closing parenthesis ④ right and left brackets match the correct 2. Algorithm idea: Sequential scan arithmetic expression (represented as a string), when encountered three types of opening parenthesis to let the parentheses into the stack, when scanning to a certain type of the right parenthesis, compare the current stack top elements match, if the match, the fallback stack continue to judge, if the current stack top element and the current scan of the parentheses do not match, The left and right brackets are paired in an incorrect order; If the string is currently a type of closing parenthesis and the stack is empty, the closing parenthesis is more than the opening parenthesis, and if the stack is non-empty (that is, the stack has some type of opening parenthesis), the opening parenthesis is more than the closing parenthesis, otherwise the parentheses are paired correctly.     3. Program Realization: */#include <iostream>using namespace std; #define MAXSIZE 100struct sstack{Char sign[maxsize]; int top;};     int Initsstack (Sstack &ss) {ss.top=-1; return 1;}     int Isemptysstack (Sstack &ss) {if (ss.top==-1) return 1; return 0;}     int Pushsstack (Sstack &ss,char c) {ss.sign[++ss.top]=c; return 1;}         int Upsstack (Sstack &ss) {if (Isemptysstack (SS)) {cout<< "stack empty" <<endl;    return 0;    } ss.top--; return 1;}         Char topsstack (Sstack &ss) {if (Isemptysstack (SS)) {cout << "stack Empty" <<endl;    return 0; } return ss.sign[ss.top];}     int main () {string S;     cout<< "input expression:";cin>>s;     int Length=s.length ();     int i;     Sstack SS;     Initsstack (SS); for (I=0;i<length;++i) {if (s[i]== ' | | | s[i]== ' [' | |           s[i]== ' {') Pushsstack (Ss,s[i]); else if (s[i]== ') ' &&!                    Isemptysstack (ss) &&topsstack (ss) = = ' (') upsstack (ss); else if (s[i]== ') ' &&!           Isemptysstack (ss) &&topsstack (ss)! = ' (') cout<< "bracket matching order Incorrect" <<endl; else if (s[i]== '] ' &&!           Isemptysstack (ss) &&topsstack (ss) = = ' [') Upsstack (ss); else if (s[i]== '] ' &&!           Isemptysstack (ss) &&topsstack (ss)! = ' [') cout<< "bracket match order Incorrect" <<endl; else if (s[i]== '} ' &&!           Isemptysstack (ss) &&topsstack (ss) = = ' {') upsstack (ss); else if (s[i]== '} ' &&!           Isemptysstack (ss) &&topsstack (ss)! = ' {') cout<< "bracket match order Incorrect" <<endl;else if ((s[i]== ') ' | | s[i]== '] | |     s[i]== '} ') &&isemptysstack (SS)) cout<< "closing parenthesis more than opening parenthesis" <<endl; } if (!     Isemptysstack (SS)) cout<< "opening parenthesis more than closing parenthesis" <<endl;                    else if (i= (length-1) &&isemptysstack (SS)) cout<< "parentheses match correctly" <<endl;     System ("PAUSE"); return 0;}

  

Bracket matching problem (c + +, stack)

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.