寻找改进的初学者代码

时间:2018-04-05 03:07:30

标签: c++

我对C ++很新。下面的代码是我做的,功能很好。然而,它似乎相当长,我觉得它可以减少 - 也许是为了提高效率。

该代码的目的是将任何DNA序列翻译成其各自的氨基酸序列。它包括用户验证DNA序列的部分。使用输入的序列产生三个ORF。

虽然它很实用,但我的程序存在一些问题。

一,我想使氨基酸阵列(AA_string1,2和3)的大小尽可能大。没有具体的内存分配(所以我不必一直更改代码)并且没有浪费内存。这可以通过指针数组完成吗?

二,如果用户输入了不期望的内容(比如你应该输入1或0的2),我的程序会遇到“out_of_range”内存错误。我相信我可以通过在无效输入时添加错误消息并在错误发生之前返回到一行代码来解决此问题。我尝试使用“goto”命令来执行此操作,为我希望重置代码的地方布置各种标签。这似乎不起作用。无效输入时,我会继续遇到out_of_range内存错误。

如果您对我提到的问题或代码本身有任何建议,请在下面的评论中告诉我们。

P.S。我把星号放在我在问题中提到的代码行中。

int main()
{
// Key for converting DNA sequence to amino acid sequence.
string codons[64] = { "UUU","UUC","UUA","UUG","UCU","UCC","UCA","UCG","UAU","UAC","UAA","UAG","UGU","UGC","UGA","UGG",
    "CUU","CUC","CUA","CUG","CCU","CCC","CCA","CCG","CAU","CAC","CAA","CAG","CGU","CGC","CGA","CGG",
    "AUU","AUC","AUA","AUG","ACU","ACC","ACA","ACG","AAU","AAC","AAA","AAG","AGU","AGC","AGA","AGG",
    "GUU","GUC","GUA","GUG","GCU","GCC","GCA","GCG","GAU","GAC","GAA","GAG","GGU","GGC","GGA","GGG" };
string aminoAcids[64] = { "Phe(F)","Phe(F)","Leu(L)","Leu(L)", "Ser(S)","Ser(S)","Ser(S)","Ser(S)","Tyr(Y)","Tyr(Y)", "Stop(*)","Stop(*)", "Cys(C)","Cys(C)","Stop(*)", "Trp(W)",
    "Leu(L)","Leu(L)","Leu(L)","Leu(L)","Pro(P)","Pro(P)","Pro(P)","Pro(P)","His(H)","His(H)","Gln(Q)","Gln(Q)","Arg(R)","Arg(R)","Arg(R)","Arg(R)",
    "Ile(I)","Ile(I)","Ile(I)","Met(M)","Thr(T)","Thr(T)","Thr(T)","Thr(T)","Asn(N)","Asn(N)","Lys(K)","Lys(K)","Ser(S)","Ser(S)","Arg(R)","Arg(R)",
    "Val(V)","Val(V)","Val(V)","Val(V)","Ala(A)","Ala(A)","Ala(A)","Ala(A)","Asp(D)","Asp(D)","Glu(E)","Glu(E)","Gly(G)","Gly(G)","Gly(G)","Gly(G)" };

// Variable declaration.
string DNA_string;
*string AA_string1[100];*
*string AA_string2[100];*
*string AA_string3[100];*

// User inputs DNA sequence.
cout << "Enter DNA sequence here: " << endl;
cin >> DNA_string;
cout << "\n";

// Preparing for sequence conversion.
int dnaLength = DNA_string.length();
int numberofCodons = dnaLength / 3;
int i = 0;
int j = 0;
int baseError;
string baseCorrect;
bool isCorrect = 0;

// Verifies if DNA sequence is inputted correctly.
while (isCorrect == 0)
{
    // Enters codons into the amino acid string array.
    j = 0;
    while (j <= numberofCodons)
    {
        for (i = 0; i <= dnaLength; i += 3)
        {
            AA_string1[j] = DNA_string.substr(i, 3);
            j += 1;
        }
    }
    // Displays the DNA sequence as sets of codons.
    for (i = 0; i <= numberofCodons; i++)
    {
        cout << AA_string1[i] << " ";
        // Indicates base position.
        if ((i + 1) % 10 == 0)
        {
            cout << ((i + 1) * 3) << " ";
        }
    }

  *UserVerify:*
  // Asks user to verify sequence.
    cout << "\nIs the following sequence correct?\n";
    cout << "Type 1 for yes and 0 for no: ";
    cin >> isCorrect;
    *if (isCorrect != 0 && isCorrect != 1)
    {
        cout << "Invalid Input: Please enter 1 or 0.";
        goto UserVerify;
    }*
    else if (isCorrect == 0)
    {
        cout << "\nPlease enter the base position you would like to change: 
                ";
        cin >> baseError;
        cout << "What should the base be changed to? If it should be 
                deleted, type 0: ";
        cin >> baseCorrect;
        if (baseCorrect == "0")
        {
            DNA_string = DNA_string.erase(baseError - 1, 1);
        }
        else
        {
            DNA_string = DNA_string.replace(baseError - 1, 1, baseCorrect);
        }
    }
    cout << "\n";
}

*DNA_Conversion:*
// Converts DNA sequence into AA sequence over three ORFs.
// computeORF is a void function I made.
computeORF(AA_string1, 0, DNA_string, codons, aminoAcids);
computeORF(AA_string2, 1, DNA_string, codons, aminoAcids);
computeORF(AA_string3, 2, DNA_string, codons, aminoAcids);

system("Pause");
return 0;
}

1 个答案:

答案 0 :(得分:0)

您的程序存在一些问题。首先,索引范围从0 ... n-1。如果您有一个长度为10的数组,则该范围内的唯一索引是0 ... 9。基本上所有的&lt; =运营商都需要成为&lt;运算符(因为索引9是数组中第10个分配的点) 由于您以三步为单位进行迭代,因此需要将上限设置为较低:

 for (i = 0; i <= dnaLength; i += 3)

变为

for (i = 0; i < dnaLength - 3; i += 3)

此外,您可以使用指针进行此分配,但这只会影响算法速度,并且不允许动态分配的数组。如果您希望在C ++中更改列表的长度,我建议使用 std :: vector

声明一个指针:

int *myPointer = 10;

现在myPointer是一个指向值10存储位置的内存地址。

&myPointer;

将检索值10。


另外,关于

(isCorrect != 0 && isCorrect != 1)

请查看此链接:http://www.cplusplus.com/forum/beginner/139177/