首页 > 其他 > 详细

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

时间:2018-12-26 00:52:56      阅读:187      评论:0      收藏:0      [点我收藏+]

#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;
    }
}

 

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

原文:https://www.cnblogs.com/likeghee/p/10177103.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!