Parenthesis matching algorithm (C ++ syntax)

This code is what I wrote before learning style of programming code after the change I will follow up the whole issue.

//括号匹配
#include<iostream>
#include<stack>
using namespace std;
int iskh(char ch)
{
    if (ch == '(' || ch == '[' || ch == '{')return 1;
    else if (ch == ')' || ch == ']' || ch == '}')return -1;
    else return  0 ; 
} 

int matching () 
{ 
    Stack < char > S;
     char CH, TEMP;
     int I;
     the while (! (CH = getchar ()) = ' \ n- ' ) 
    { 
        // determines whether bracket 
        I = iskh ( CH);
         iF (I> 0 ) // is a left parenthesis, the stack 
            S.push (CH);
         the else  iF (I < 0 ) // is a right bracket, the stack is determined taking matches 
        {
             iF(S.empty ())
                 return  0 ; 
            TEMP = S.top ();
             IF ((TEMP == ' ( ' && CH == ' ) ' ) || (TEMP == ' [ ' && CH == ' ] ' ) ||  
            (TEMP == ' { ' && CH == ' } ' )) // successful match 
                
                S.pop ();         
            the else // match fails 
                return  0 ;
        }
        else// not in parentheses, no operation is performed, continue to traverse down 
            Continue ; 
    } 
    return  . 1 ; 
} 

int main () {
     int I = matching ();
     IF (I) 
            COUT << " successful match! " << endl;
     the else  
            COUT << " fails to match! " << endl;
     return  0 ; 
}

        

 

Guess you like

Origin www.cnblogs.com/lyf98/p/11706758.html