for循环没有传递值

时间:2016-09-23 10:08:35

标签: c++ string for-loop

我们正在实现一个函数,它将用户的输入字符串与固定的字符串变量进行比较。如果用户输入4个字母,那么我们将比较第一个索引中的这4个字母并循环直到结束。实施后,我无法获得变量" highest_score"的正确结果。我尝试在返回值的正上方添加一个print语句来测试high_score的值,结果我收到0。所以我想也许预期的价值没有通过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 个答案:

答案 0 :(得分: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;
}