括号匹配问题 :()[] 难度1

#include <iostream>
using namespace std;
#include <string>
bool ShiPiPei(const char* test) {
    string s;
    char str[100];
    int topStackIndex = 0;
    s.assign(test);
    for (string::iterator it = s.begin(); it != s.end(); it++){
    if ((*it) == '(' || (*it) == '[') {
        str[topStackIndex++]=(*it);
    }
    else if ((*it) == ')') {
        if (str[--topStackIndex] == '(') {
                
        }
        else {
            //cout << "不匹配!" << endl;
            return false;
        }
    }
    else if ((*it) == ']') {
        if (str[--topStackIndex] == '[') {
    
        }
        else {
            //cout << "不匹配!" << endl;
            return false;
        }
    }
    }
    //cout << "匹配";
    return true;
}
int main() {
    if (ShiPiPei("([)]")) {
        cout << "匹配!" << endl;
    }
    else {
        cout<<"不匹配!" << endl;
    }
}

猜你喜欢

转载自www.cnblogs.com/likeghee/p/10177103.html