这是我写过的第一个C ++程序,我无法理解操作数必须放入的顺序。这是一个类,但看起来我不应该使用作业标签。对不起,如果我这样做错了。
这是我的输入
// Get DNA string
string st;
cout << "Enter the DNA sequence to be analysed: ";
cin >> st;
这似乎工作正常,但我想我会把它包括在内,这就是我做错了。
这是我到目前为止检查输入是否只有C,T,A或G. 它贯穿整个程序并简单打印“请输入有效的sequnce1,请输入有效的序列2,等等。我确定我做的事情非常愚蠢,我无法理解。
// Check that the sequence is all C, T, A, G
while (i <= st.size()){
if (st[i] != 'c' && st[i] != 'C' && st[i] != 'g' && st[i] != 'G' && st[i] != 't' && st[i] != 'T' && st[i] != 'a' && st[i] != 'A');
cout << "Please enter a valid sequence" <<
i++;
else if (st[i] == c,C,G,t,T,a,A)
i++;
我的程序的后半部分是计算序列中的C和G的数量
for (i < st.size() ; i++ ;);
for (loop <= st.size() ; loop++;)
if (st[loop] == 'c')
{
count_c++;
}
else if (st[loop] == C)
{
count_c++;
}
else if (st[loop] == g)
{
count_g++;
}
else if (st[loop] == G);
{
count_g++;
}
cout << "Number of instances of C = " << count_c;
cout << "Number of instances of G = " << count_g;
似乎它没有循环,它将计算其中一个字母中的1个。我如何让它循环?我好像无法忍受;任何地方都没有收到任何错误,虽然我知道我会在某个地方需要它。
任何帮助或提示指出我正确的方向将不胜感激 - 我已经在这个代码上工作了两天(这是令人尴尬的承认)。
编辑:
我的序列检查器现在看起来像这样:
while (i < st.size() ) {
if (st[i] != c && st[i] != C && st[i] != g && st[i] !=G && st[i] !=t && st[i] !=T && st[i] !=a && st[i] != A)
cout << "Please enter a valid sequence" << "\n" << "\n";
i++;
}
我的计数器看起来像这样:
// Count the number of Cs and Gs
count_c = 0;
count_g = 0;
for (i = 0; i < st.size() ; i++) {
if ((st[i] == 'c') || (st[i] == 'C'))
count_c++;
else if ((st[i] == 'g')|| (st[i] == 'G'));
count_g++;
}
cout << "Number of instances of C = " << count_c;
cout << "Number of instances of G = " << count_g;
答案 0 :(得分:0)
你的for循环使用了错误的语法。你想要:
for (i = 0; i < st.size() ; i++) {
...
}
此外,您应始终使用< size
而不是<= size
进行索引,因为索引从0
开始,到size-1
结束。
答案 1 :(得分:0)
你应该删除“;”之后如果运营商:
if (st[i] != 'c' && st[i] != 'C' && st[i] != 'g' && st[i] != 'G' && st[i] != 't' && st[i] != 'T' && st[i] != 'a' && st[i] != 'A');
现在它没有做任何事情。
你应该使用“&lt;”而不是“&lt; =”以避免错误索引字符串数组(字符串大小“size”表示索引从0到大小 - 1)
while (i <= st.size())
如果您已经检查过该符号不是c,C,g,G,t,T,a,A之一,则不需要再次检查,所以如果(st [i] == c, C,G,t,T,a,A)没用。
但是,即使使用theese更正,您的代码在逻辑上也是错误的。
答案 2 :(得分:0)
让我们立即修复验证和计数:
bool sequencedna(const string &s, int &count_a, int &count_t, int &count_c, int &count_g)
{
for(int i = 0; i != s.length(); i++)
{
/* get the character at position i and convert it to uppercase */
char c = s[i];
if((c == 'C') || (c == 'c'))
count_c++;
else if((c == 'G') || (c == 'g'))
count_g++;
else if((c == 'T') || (c == 't'))
count_t++;
else if((c == 'A') || (c == 'a'))
count_a++;
else
return false; // invalid character in DNA sequence!
}
return true; // valid DNA sequence
}
void doit()
{
string st;
while(true)
{
cout << "Enter the DNA sequence to be analysed: ";
cin >> st;
int count_c = 0, count_g = 0, count_t = 0, count_a = 0;
if(sequencedna(st, count_a, count_t, count_c, count_g))
{
cout << "The DNA string has been sequenced. Counts " << endl
<< " Adenine: " << count_a << endl
<< " Thymine: " << count_t << endl
<< " Cytosine: " << count_c << endl
<< " Guanine: " << count_g << endl;
return;
}
cout << "Invalid sequence. DNA sequences may contains only A, T, C and G." << endl;
}
}