C++ 使用从另一个函数C++;

C++ 使用从另一个函数C++;,c++,C++,我编写了一个函数,它应该分析和使用从另一个函数生成的数据(具体为字符串),计算字符的百分比,并打印出结果 此函数的类型为int,但上一个函数的类型为string。我想使用指针,但我不知道这是否是一个有效的转换 我也不相信把一个函数放在另一个函数参数中是有效的 这是一个几乎完整的函数 int percentages(string line) { int overalcount; double Leu, Phe, Ile, STA, Val, Ser, Pro, Thr, Ala,

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

此函数的类型为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;
}
int百分比(字符串行)
{
int超额计算;
双Leu、Phe、Ile、STA、Val、Ser、Pro、Thr、Ala、Tyr、STO、His、Gln、Asn、Lys、Asp、Glu、Cys、Trp、Arg、Gly;
整数百分比,百分比1,百分比2,百分比3,百分比4,百分比5,百分比6,百分比7;
整数百分比8、百分比9、百分比10、百分比11、百分比12、百分比13、百分比14;
国际百分比15,16,17,18,19,20;
int i;
对于(i=0;i+3cout在这里,取这个小得多的总函数:

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;
}
int百分比(字符串行)
{
地图词;
double count=line.length()/3;
对于(int i=0;i+3请您自己学习一下数组和case/switch语句……另外,在double上使用++几乎肯定不会达到您的预期(如果这样做的话,我会非常惊讶)你需要用一个int来代替。我希望听起来不是很讨厌,但是这个函数看起来真的很好。你说实话,所以谢谢。我不太擅长C++,这也是一个逻辑问题。