函数使用从另一个函数生成的字符串
Function using string produced from another function C++
我已经编写了一个函数,该函数应该分析和使用从另一个函数产生的数据(具体为字符串),并计算字符的百分比,并打印出结果。
这个函数的类型是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"应该是前一个函数产生的字符串。但是它会识别从输入文件流中读取的行。
这里,以这个更简单的函数为例:
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;
}
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何从void函数输出字符串
- 使用用户定义函数的字符串反转
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- 在c++构造函数中使用随机字符串生成器
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- C++:如何使函数只返回作为列表一部分的字符串
- 如何在C++中的同一函数中使用字符串和双精度
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 一个函数,用于查找字符串1包含字符串2 c++的次数
- 对函数 PlaySound 使用字符串变量
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- Visual c ++,使用字符串引用/指针调用 dll 函数
- 构造函数字符串参数未设置窗口标题
- C++默认构造函数:字符串参数与字符串参数()
- 使用函数C++字符串输入
- c++模板函数字符串类型不匹配