
我的task 4有點卡 ''"""'""""''''''" 這個應該輸出1 但我的輸出是0 ![]()
/*
 * Note:
 * - DO NOT change any of the function headers given
 * - 不能用 loops
 * - 不能用 any global variables or add additional libraries.
 * - You can add helper function(s) if needed.
 */
#include <iostream>
using namespace std;
// Constants
// NULL character. This is the last char of all C Strings
const char END = '\0';
// Single quotation character '
const char SQUOTE = '\'';
// Double quotation character "
const char DQUOTE = '\"';
// Error. Used in Task 2 and 3
const int ERROR = -1;
// Practice Task: Task 0 (Not Graded)
unsigned int recursive_strlen(const char line[], int start)
{
	if(line[start]!= END){
		++start;
		recursive_strlen(line,start);
	}
	else if(line[start]== END){
		return start;
}
}
// Normal Task: Task 1
unsigned int count_dquotes(const char line[], int start)
{
    int x =  recursive_strlen(line,0);
	if(x>start){
		if(line[start]== '"'){
            if(start==(x-1)){return 1;}
            else{return 1+count_dquotes(line,start+1);}
		}
		else{
			if(start==(x-1)){return 0;}
			else{return 0+count_dquotes(line,start+1);}
		}
	}
}
// Normal Task: Task 2
int find_first_dquote(const char line[], int start)
{
if(recursive_strlen(line,start)==0||count_dquotes(line,start)==0){return ERROR;}
else if(line[start]!= END){
	if(line[start]=='"'){return start;}
	else{find_first_dquote(line,start+1);}
	}
}
// Normal Task: Task 3
int count_chars_in_matched_dquote(const char line[], int start)
{
int num,div;
int numsave = 0;
	if(find_first_dquote(line,start)!=-1 && find_first_dquote(line,find_first_dquote(line,start)+1)==-1){
		return -1;}
	else if(find_first_dquote(line,start)==-1){
	return 0;}
		else if(count_dquotes(line,start)%2 != 0){
			return -1;}
		else{
			div = count_dquotes(line,start)/2;
			if(div>1){
				--div;
				num = find_first_dquote(line,find_first_dquote(line,start)+1) - find_first_dquote(line,start)-1;
				numsave+=num;
				start= find_first_dquote(line,find_first_dquote(line,start)+1)+1;
				count_chars_in_matched_dquote(line,start);
			}
			if(div==1){
				num = find_first_dquote(line,find_first_dquote(line,start)+1) - find_first_dquote(line,start)-1;
				numsave+=num;
return numsave;
			}
		}
}
bool funfortask(const char line[],int start){
	char line2[recursive_strlen(line,start)-1]={};
if(line[start]!=END){
		if(line[start]==DQUOTE){
		line2[start]= SQUOTE;
		funfortask(line,start+1);
		}
		else if(line[start]==SQUOTE){
			line2[start] = DQUOTE;
		funfortask(line,start+1);}
		else{
			line2[start]=line[start];
			funfortask(line,start+1);
		}
		}
if(line2[start]==(line2[recursive_strlen(line2,start)-1]) && line2[start]== DQUOTE){
		return true;
	}
else{return false;}
}
// Challenging Task: Task 4
bool check_quotes_matched(const char line[], int start)
{
	if(recursive_strlen(line,start)==0||count_dquotes(line,start)==0){
		if(line[start]==SQUOTE){
		if(funfortask(line,start)==1){
		return true;}
		else{return false;}
		}
		else{return true;}
	}
	else if(line[start]==(line[recursive_strlen(line,start)-1]) && line[start]== DQUOTE){
		return true;
	}
	else if(line[start]==line[count_chars_in_matched_dquote(line,start)+1]){
    int x = count_chars_in_matched_dquote(line,start)+3;
    check_quotes_matched(line,x);
	}
}
// Challenging Task: Task 5
unsigned int length_of_longest_consecutive_dquotes(const char line[], int start)
{
	
	
}
// DO NOT WRITE ANYTHING AFTER THIS LINE. ANYTHING AFTER THIS LINE WILL BE REPLACED
const int MAX_LENGTH = 1000;
int main()
{
	int option = 0;
	char line[MAX_LENGTH];
	do {
		cout << "Options:" << endl;
		cout << "0:  Test recursive_strlen()" << endl;
		cout << "1:  Test count_dquotes()" << endl;
		cout << "2:  Test find_first_dquote()" << endl;
		cout << "3:  Test count_chars_in_matched_dquote()" << endl;
		cout << "4:  Test check_quotes_matched()" << endl;
		cout << "5:  Test length_of_longest_consecutive_dquotes()" << endl;
		cout << "-1: Quit" << endl;
		cin >> option;
		cin.ignore();
		switch (option) {
			case 0:
			cout << "Testing recursive_strlen()" << endl;
			cout << "Enter line: ";
			cin.getline(line, MAX_LENGTH);
			cout << recursive_strlen(line, 0) << endl;
			break;
			case 1:
			cout << "Testing count_dquotes()" << endl;
			cout << "Enter line: ";
			cin.getline(line, MAX_LENGTH);
			cout << count_dquotes(line, 0) << endl;
			break;
			case 2:
			cout << "Testing find_first_dquote()" << endl;
			cout << "Enter line: ";
			cin.getline(line, MAX_LENGTH);
			cout << find_first_dquote(line, 0) << endl;
			break;
			case 3:
			cout << "Testing count_chars_in_matched_dquote()" << endl;
			cout << "Enter line: ";
			cin.getline(line, MAX_LENGTH);
			cout << count_chars_in_matched_dquote(line, 0) << endl;
			break;
			case 4:
			cout << "Testing check_quotes_matched()" << endl;
			cout << "Enter line: ";
			cin.getline(line, MAX_LENGTH);
			cout << check_quotes_matched(line, 0) << endl;
			break;
			case 5:
			cout << "Testing length_of_longest_consecutive_dquotes()" << endl;
			cout << "Enter line: ";
			cin.getline(line, MAX_LENGTH);
			cout << length_of_longest_consecutive_dquotes(line, 0) << endl;
			break;
			default:
			break;
		}
		cout << endl;
	} while (option != -1);
	return 0;
}
                        你考慮一下判斷的邏輯,需要哪些變數,然後把變數當作參數來設計遞迴函數...像這樣:
const tests = [
    '',
    'COMP2011',
    'Hello " World',
    '"""Hello World"""',
    'AAAAAA""BBB',
    '"""AA""""""',
    '"""""BBBBBBB"""'
];
main();
function main() {
    tests.forEach(s => {
        console.log(count(s, 0, 0, 0));
    });
    function count(s, sp, cl, ml) {
        if(sp < s.length) {
            if(s[sp] === '"') {
                if(++cl > ml) ml = cl;
            } else {
                if(cl > ml) ml = cl;
                cl = 0;
            }
            return count(s, ++sp, cl, ml);
        } else {
            return ml;
        }
    }
}
結果:
我是用Javascript寫,不過轉換應該不困難。(我發文以後有稍微改一下程式,不過結果圖是最早的版本)