2013-03-22 65 views
-1

我寫了一個函數,它應該分析和使用從另一個函數產生的數據(字符串是特定的)並計算字符的百分比並打印結果。函數使用從另一個函數產生的字符串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; 
} 

參數「串線」據說是由以前的函數生成的字符串。但是它會識別從輸入文件流中讀取的行。

+3

請教育自己陣列和情況/ switch語句...還有,上了雙做++幾乎肯定不會做你期望什麼(我如果它真的會令人驚訝)。你需要使用int來代替。 – Patashu 2013-03-22 00:29:54

+1

我希望聽起來不令人反感,但是這個功能看起來真的很粗糙。 – Wug 2013-03-22 00:30:49

+0

好吧,你誠實,所以謝謝。我不是很擅長C++ lol – user2188311 2013-03-22 00:31:29

回答

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

謝謝。 OP的功能讓我哭了起來。 – 2013-03-22 00:56:40

+0

我在這個網站上看到的東西讓我覺得我的大腦想要從我的頭骨中挖出它的方式。這一個並沒有那麼糟糕。我承認,它可能是「在最多線路上」類別中的贏家。 – Wug 2013-03-22 00:57:49

+0

至少這個人付出了一些努力,這是真的。 – 2013-03-22 00:59:45

相關問題