2012-11-30 39 views
1

這是我寫過的第一個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秒。我如何讓它循環?我似乎無法投入endl;任何地方都不會收到錯誤,儘管我知道我會在某個地方需要它。

任何幫助或提示指向我在正確的方向將不勝感激 - 我一直在這個代碼工作兩天(這是承認尷尬)。


編輯:

我的序列檢驗看起來像現在這樣:

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; 
+4

你的for循環失蹤的第一部分。 – chris

+0

這段代碼不會編譯,即使它做到了,它也不會做你聲稱它的事情 –

+0

我並不是說它做任何事情!我在尋求幫助。現在它正在爲我編譯 - 但是它重複了我的gs,我會在一分鐘內編輯我的文章。 – stringgy

回答

0

您的循環使用不正確的語法。你想:

for (i = 0; i < st.size() ; i++) { 
    ... 
} 

此外,你應該總是使用< size而不是<= size索引,因爲索引開始於0size-1結束。

+0

我從那裏開始,但是g ++給了我一個錯誤消息,我從循環外部刪除了i = 0,並將它放回到現在運行。謝謝! – stringgy

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'); 

現在它沒有做任何事情。

您應該使用的「<」,而不是「< =」,以避免字符串數組的錯誤索引(大小的字符串「大小」是指指數是從0到大小 - 1)

while (i <= st.size()) 

如果您已經檢查過這個符號不是c,C,g,G,t,T,a,A中的一個,所以你不需要再次檢查它,所以如果(st [i] == c,C,G,t ,T,a,A)是無用的。

但即使有這些更正你的代碼在邏輯上是錯誤的。

+0

謝謝!我不知道我是如何繼續尋找這個過去的! – stringgy

+1

順便說一下,符號'(st [i] == c,C,G,t,T,a,A)'不會比較st [i]和所有這些字符。逗號運算符是棘手的... –

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; 
    } 
} 
+0

想念別的? –

+0

@MooingDuck嗯?你認爲其他人失蹤了嗎? –

+0

'doit'看起來像一個錯誤,但經過進一步檢查,我意識到了它的意圖。這很奇怪。 –

相關問題