-1

我写了一个函数,它应该分析和使用另一个函数产生的数据(具体为字符串),并计算字符的百分比并打印出结果。

此函数是 int 类型,但前一个函数是 string 类型。我正在考虑使用指针,但我不知道它是否是有效的转换。

我也不相信将一个函数放在另一个函数参数中是有效的。

这是一个几乎完整的功能

int percentages(string line)
{
    int overalcount;
    double Leu, Phe, Ile, STA, Val, Ser, Pro, Thr, Ala, Tyr, STO, His, Gln, Asn, Lys, Asp, Glu, Cys, Trp, Arg, Gly;
    int percentage, percentage1, percentage2, percentage3, percentage4, percentage5, percentage6, percentage7;
    int percentage8, percentage9, percentage10, percentage11, percentage12, percentage13, percentage14;
    int percentage15, percentage16, percentage17, percentage18, percentage19, percentage20;
    int i;

    for (i = 0; i + 3 < line.length(); i += 3)
    {
        overalcount++;

        if(line.substr(i, 3) == "Phe")
        {
            Phe++;
            if(!Phe == 0)
            {
                percentage = (Phe / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Leu")
        {
            Leu++;
            if(!Leu == 0)
            {
                percentage = (Leu / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Ile")
        {
            Ile++;
            if(!Ile == 0)
            {
                percentage = (Ile / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "STA")
        {
            STA++;
            if(!STA == 0)
            {
                percentage = (STA / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Val")
        {
            Val++;
            if(!Val == 0)
            {
                percentage = (Val / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Ser")
        {
            Ser++;
            if(!Ser == 0)
            {
                percentage = (Ser / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Pro")
        {
            Pro++;
            if(!Pro == 0)
            {
                percentage = (Pro / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Thr")
        {
            Thr++;
            if(!Thr == 0)
            {
                percentage = (Thr / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Ala")
        {
            Ala++;
            if(!Ala == 0)
            {
                percentage = (Ala / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Tyr")
        {
            Tyr++;
            if(!Tyr == 0)
            {
                percentage = (Tyr / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "STO")
        {
            STO++;
            if(!STO == 0)
            {
                percentage = (STO / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "His")
        {
            His++;
            if(!His == 0)
            {
                percentage = (His / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Gln")
        {
            Gln++;
            if(!Gln == 0)
            {
                percentage = (Gln / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Asn")
        {
            Asn++;
            if(!Asn == 0)
            {
                percentage = (Asn / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Lys")
        {
            Lys++;
            if(!Lys == 0)
            {
                percentage = (Lys / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Asp")
        {
            Asp++;
            if(!Asp == 0)
            {
                percentage = (Asp / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Glu")
        {
            Glu++;
            if(!Glu == 0)
            {
                percentage = (Glu / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Cys")
        {
            Cys++;
            if(!Cys == 0)
            {
                percentage = (Cys / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Trp")
        {
            Trp++;
            if(!Trp == 0)
            {
                percentage = (Trp / overalcount) * 100;
            }
        }
        else if(line.substr(i, 3) == "Arg")
        {
            Arg++;
            if(!Arg == 0)
            {
                percentage = (Arg / overalcount) * 100;
            }   
        }
        else if(line.substr(i, 3) == "Gly")
        {
            Gly++;
            if(!Gly == 0)
            {
                percentage = (Gly / overalcount) * 100;
            }   
        }
    }

    if(!percentage == 0)
    {
        cout << "Percentage of Phe: " <<percentage <<endl;
    }
    if(!percentage1 == 0)
    {
        cout << "Percentage of Leu: " <<percentage1 <<endl;
    }
    if(!percentage2 == 0)
    {
        cout << "Percentage of Ile: " <<percentage2 <<endl;
    }
    if(!percentage3 == 0)
    {
        cout << "Percentage of STA: " <<percentage3 <<endl;         
    }
    if(!percentage4 == 0)
    {
        cout << "Percentage of Val: " <<percentage4 <<endl;         
    }
    if(!percentage5 == 0)
    {
        cout << "Percentage of Ser: " <<percentage5 <<endl;
    }
    if(!percentage6 == 0)
    {
        cout << "Percentage of Pro: " <<percentage6 <<endl;
    }
    if(!percentage7 == 0)
    {
        cout << "Percentage of Thr: " <<percentage7 <<endl;
    }
    if(!percentage8 == 0)
    {
        cout << "Percentage of Ala: " <<percentage8 <<endl;
    }
    if(!percentage9 == 0)
    {
        cout << "Percentage of Tyr: " <<percentage9 <<endl;
    }

    return 0;
}

参数“string line”应该是前一个函数产生的字符串。但它是识别从输入文件流中读取的行。

4

1 回答 1

2

在这里,采用这个少得多的总函数:

int percentages(string line)
{
    map<string, int> words;
    double count = line.length() / 3;
    for (int i = 0; i + 3 < line.length(); i += 3)
        ++words[line.substr(i, 3)];

    string find[10] = {"Phe", "Leu", "Ile", "STA", "Val", "Ser", "Pro", "Thr", "Ala", "Tyr"};
    for (int i = 0; i < 10; ++i) 
        cout << "Percentage of " << find[i] << ": " << words[find[i]] / count << endl;
    return 0;
}
于 2013-03-22T00:49:01.217 回答