iT邦幫忙

0

關於Recursion 遞歸的問題 C++

  • 分享至 

  • xImage

https://ithelp.ithome.com.tw/upload/images/20200407/20125346Ct4F4m2WtI.png

我的task 4有點卡 ''"""'""""''''''" 這個應該輸出1 但我的輸出是0 /images/emoticon/emoticon02.gif

  • 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.
      */
/*
 * 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;
}

看更多先前的討論...收起先前的討論...
fillano iT邦超人 1 級 ‧ 2020-04-06 17:49:49 檢舉
第五題是要找最長的連續雙引號序列長度,你的寫法看起來只是計算所有雙引號數目?
lam814 iT邦新手 5 級 ‧ 2020-04-06 19:40:19 檢舉
對 找最長的連續雙引號序列長度
lam814 iT邦新手 5 級 ‧ 2020-04-06 19:41:08 檢舉
但我想不到怎樣寫
lam814 iT邦新手 5 級 ‧ 2020-04-06 19:41:08 檢舉
能給一下意見嗎?
huanwen iT邦新手 5 級 ‧ 2020-04-07 10:05:12 檢舉
可以從後面開始嗎? 每次分左右兩半處理
right = length_of_longest_consecutive_dquotes(line, MAX_LENGTH)
left = length_of_longest_consecutive_dquotes(line, MAX_LENGTH/2)

這樣會遇到最長字串出現在左中右三種case,在左右的選最大,中間的要加起來
lam814 iT邦新手 5 級 ‧ 2020-04-07 11:59:44 檢舉
感謝各位大神 已經用fillanofeng的方法做好了 現在只剩task4有點bug

''"""'""""''''''" 這個應該輸出1 但我的輸出是0
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
fillanofeng
iT邦新手 5 級 ‧ 2020-04-07 09:25:44
最佳解答

你考慮一下判斷的邏輯,需要哪些變數,然後把變數當作參數來設計遞迴函數...像這樣:

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

結果:
https://ithelp.ithome.com.tw/upload/images/20200407/201259040xJSJYiIfg.png

我是用Javascript寫,不過轉換應該不困難。(我發文以後有稍微改一下程式,不過結果圖是最早的版本)

lam814 iT邦新手 5 級 ‧ 2020-04-07 11:57:51 檢舉

感謝了!!!!!Task 5已經好了
Task 4有點bug 能幫忙看一看嗎?
''"""'""""''''''" 這個應該輸出1 但我的輸出是0/images/emoticon/emoticon02.gif

https://ithelp.ithome.com.tw/upload/images/20200407/20125346t1d7KzeQsE.png

我要發表回答

立即登入回答