2
if(gene1A[20] == 'T' || gene2A[20] == 'T')
    outFile << "Person A is of 'Anemic' type." << endl;
else if(gene1A[20] == 'T' && gene2A[20] == 'T')
    outFile << "Person A if of 'Carrier' type." << endl;
else
    outFile << "Person A is of 'Normal' type." << endl;

if(gene1B[20] == 'T' || gene2B[20] == 'T')
    outFile << "Person B is of 'Anemic' type." << endl;
else if(gene1B[20] == 'T' && gene2B[20] == 'T')
    outFile << "Person B if of 'Carrier' type." << endl;
else
    outFile << "Person B is of 'Normal' type." << endl;

if(gene1C[20] == 'T' || gene2C[20] == 'T')
    outFile << "Person C is of 'Anemic' type." << endl;
else if(gene1C[20] == 'T' && gene2C[20] == 'T')
    outFile << "Person C if of 'Carrier' type." << endl;
else
    outFile << "Person C is of 'Normal' type." << endl;

if(gene1D[20] == 'T' || gene2D[20] == 'T')
    outFile << "Person D is of 'Anemic' type." << endl;
else if(gene1A[20] == 'T' && gene2A[20] == 'T')
    outFile << "Person D if of 'Carrier' type." << endl;
else
    outFile << "Person D is of 'Normal' type." << endl;

今のところ私のコードです。私がする必要があるのは、私が設定した配列に基づいて、人が貧血、キャリア、または正常である場合、「outFile」を出力することです。各配列の長さは 444 文字で、A、C、T、または O のいずれかです。T が遺伝子 1[] および/または遺伝子 2[] の 20 番目の位置にある場合、その人は貧血 (配列が 1 つしかない場合) またはキャリア (両方のアレイの場合)。

私が今持っているものは、それらを自動的に「通常」にします。if ステートメントが適切に設定されていないと思いますが、必要なのは、配列の 20 番目の値を参照し、それが == 'T' の場合、それらの「型」を出力することです。

注: コードに 19 ではなく 20 を入れていることに気付きました。その修正を行ったので、それを過ぎて見てください。

みんなありがとう!

4

1 に答える 1

1

(これは完全な答えではありませんが、コメントとして表現するのは難しく、結果として単純化された結果、とにかく答えにつながる可能性があります...)

機能分解はあなたの友達です:

const char* type(const char* gene1, const char* gene2) {
    return gene1[19] != 'T' ? "Normal" : gene2[19] == 'T' ? "Anemic" : "Carrier";
}
⋮
outFile << "Person A is of '" << type(gene1A, gene2A) << "' type." << endl;
outFile << "Person B is of '" << type(gene1B, gene2B) << "' type." << endl;
outFile << "Person C is of '" << type(gene1C, gene2C) << "' type." << endl;
outFile << "Person D is of '" << type(gene1D, gene2D) << "' type." << endl;

また、あなたがDさんに導入したようなバグを導入するのがはるかに難しくなり、導入したときに見つけやすくなります.

編集: @MarkBは私のロジックのエラーを指摘しました(元のロジックを読み違えました)。残念ながら、元のロジックは次の形式であるため、修正方法がわかりません。

     if A or  B then X
else if A and B then Y
else                 Z

(A and B) が true のときはいつでも (A or B) が true であるため、2 番目の句がトリガーされることはありません。最初に AND 句を使用する場合は、type()関数を次のように書き直すことができます。

const char* type(const char* gene1, const char* gene2) {
    bool t1 = gene1[19] == 'T';
    bool t2 = gene2[19] == 'T';
    return t1 && t2 ? "Anemic" : t1 || t2 ? "Carrier" : "Normal"  );
}

ちなみに、この関数は現在のコードの「サブ関数」(それが何を意味するにせよ) ではなく、単に関数の上で宣言されたフリー関数になります。OTOH、コンパイラが C++11 ラムダをサポートしている場合、実際type()には、問題の関数に対してローカルで関数を宣言できます。

auto type = [](const char* gene1, const char* gene2) -> const char * {
    …
};
于 2012-11-27T02:36:26.983 に答える