2016-09-23 142 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

谢谢....经验教训 –