1. 程式人生 > >嘗試用函式遞迴判斷括號使用的正確性

嘗試用函式遞迴判斷括號使用的正確性

嘗試用函式遞迴判斷括號使用的正確性

``

#include <iostream>

using namespace std;

int check(char str[], char c, int *n);

int main()
{
    char str[60];
    while(cin.getline(str, 60))
    {
        int i = 0;
        int flag = check(str, '\0', &i);
        if(flag == 1)
            cout << "yes"
<< endl; else cout << "no" << endl; } return 0; } int check(char str[], char c, int *n) { while(str[*n] != '\0') { if(c == '(') //判斷是否與 ( 相對應 { if(str[*n] == c+1) return 1; } else if(str[*
n] == c+2) //判斷是否與 [ 或 { 相對應 return 1; if(str[*n] == ')' || str[*n] == ']' || str[*n] == '}') { return 0; } else if(str[*n] == '(' || str[*n] == '[' || str[*n] == '{') { (*n) ++; int flag = check(str, str[(*n)-1], n)
; if(flag == 0) return 0; } (*n)++; } if(c == '\0') return 1; else return 0; }