2016-09-23 117 views
0

我們正在實現一個函數,它將用戶的輸入字符串與固定字符串變量進行比較。如果用戶輸入4個字母,那麼我們將比較來自第一個索引的這4個字母並循環直到結束。執行後,我無法爲變量「highest_score」獲得正確的結果。我試着在返回值上面添加一個print語句來測試highest_score的值,並且我收到0作爲結果。所以我在想,也許預期的值沒有通過for循環,我不知道是否有人能夠幫助我理解爲什麼值沒有通過for循環,並修復它的提示!非常感謝你。值未通過for循環

/********************************************************************************* 
calcSimilarity() function will take two arguments that are both strings. The 
function calculates the Hamming distance and returns the similarity score. This 
function should only calculate the similarity if the two strings are the same length, 
otherwise return 0. 
**********************************************************************************/ 
float calcSimilarity (string str_1,string str_2){ 
    float hammer_distance; 

    /*Lenth check*/ 
    if (str_1.length() != str_2.length()){ 
     cout << "String length not equal, please enter again: " << endl; 
    } 

    /*Hammer distance*/ 
    for (int i = 0; i < str_1.length(); i++) 
    { 
     if (str_1[i] != str_2[i]){ 
      hammer_distance += 1; 
     } 
    } 

    /*Similarity score*/ 
    float similarity_score = (str_2.length() - hammer_distance)/str_2.length(); 


    return similarity_score; 
} 

/*********************************************************************************** 
compareDNA() function should take two arguments that are both strings. The 
function should calculate the similarity score for each substring of the DNA 
(substring should be same length as user_input) and return the best similarity 
score found across all the possible substrings. Use the calcSimilarity() function 
described above. 
**********************************************************************************/ 
float compareDNA(string DNA, string user_input){ 
    float current_score = 0; 
    float highest_score = 0; 
    float final_score; 
    string substring; 
    /*Loop through each segment and calculating for the highest score*/ 
    for (int i = 0; i < DNA.length()-user_input.length(); i++){ 
     substring = DNA.substr(i,user_input.length()); 
     current_score = calcSimilarity(user_input,substring); 
     if (current_score > highest_score){ 
      highest_score = current_score; 
     } 
    } 
    cout << highest_score << endl; 
    return highest_score; 
} 
+1

你能不能也發佈代碼calcSimilarity()? – user3286661

+0

好的,只是更新了! –

+4

'hammer_distance'未初始化 –

回答

1

正確的代碼:

/********************************************************************************* 
calcSimilarity() function will take two arguments that are both strings. The 
function calculates the Hamming distance and returns the similarity score. This 
function should only calculate the similarity if the two strings are the same length, 
otherwise return 0. 
**********************************************************************************/ 
float calcSimilarity (string str_1,string str_2){ 
    float hammer_distance = 0; // Initialize this variable 

    /*Lenth check*/ 
    if (str_1.length() != str_2.length()){ 
     cout << "String length not equal, please enter again: " << endl; 
    } 

    /*Hammer distance*/ 
    for (int i = 0; i < str_1.length(); i++) 
    { 
     if (str_1[i] != str_2[i]){ 
      hammer_distance += 1; 
     } 
    } 

    /*Similarity score*/ 
    float similarity_score = (str_2.length() - hammer_distance)/str_2.length(); 


    return similarity_score; 
} 

/*********************************************************************************** 
compareDNA() function should take two arguments that are both strings. The 
function should calculate the similarity score for each substring of the DNA 
(substring should be same length as user_input) and return the best similarity 
score found across all the possible substrings. Use the calcSimilarity() function 
described above. 
**********************************************************************************/ 
float compareDNA(string DNA, string user_input){ 
    float current_score = 0; 
    float highest_score = 0; 
    float final_score; 
    string substring; 
    /*Loop through each segment and calculating for the highest score*/ 

    // Use i <= instead of i < 
    for (int i = 0; i <= DNA.length()-user_input.length(); i++){ 
     substring = DNA.substr(i,user_input.length()); 
     current_score = calcSimilarity(user_input,substring); 
     if (current_score > highest_score){ 
      highest_score = current_score; 
     } 
    } 
    cout << highest_score << endl; 
    return highest_score; 
} 
+0

謝謝....經驗教訓 –