1. 程式人生 > >括號匹配問題 :()[] 難度1

括號匹配問題 :()[] 難度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; } }