c++ 对于合并了switch语句的多个while循环,如何分隔while循环的各个值?

ntjbwcob  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(109)

试图计算我的dnaSequence.txt文件中腺嘌呤、鸟嘌呤、胞嘧啶和胸腺嘧啶的数量。我的while循环遇到了问题。我可以让它识别序列中的各个字符,但是当我试图计算它们时,由于某种原因,所有的值都聚集在一起。当我试图添加break时;语句仍然不起作用,显示为0。我尝试为每个语句使用单独的while循环。
将感谢一些帮助。只是需要一个指针在正确的大方向。如果我能得到这个扭结理顺,我应该能够做的其余程序罚款。

#include <iostream> 
#include <fstream> 
#include <iomanip> 

using namespace std;
                
int main() { 
    // we are reading file dnaSequence.txt 
    // goals: 
    // # of nucleotides within sequence (how many ATC and G together) 
    // # % of the code each is (how many A's, C's etc)
                
    // we want the numb values to be Int b/c we want the actual amount
    int numbOfAdenine;
    int numbOfThymine;
    int numbOfCytosine;
    int numbOfGuanine;
                
    // we want our percent values to be float b/c we want their values to be decimals: allows for more accuracy
    float percOfAdenine;
    float percOfThymine;
    float percOfCytosine;
    float percOfGuanine;
                
    // allows us to calculate our final total
    float totalNucleotides;
                
    // allows us to count the characters
    char numbOfNucleotidesAdenine;
    char numbOfNucleotidesThymine;
    char numbOfNucleotidesCytosine;
    char numbOfNucleotidesGuanine;
                
    // want to open our dnaSequence file
    // -- remember spelling: ifstrem not infstream
    ifstream dna_file("dnaSequence.txt");
                
    // checks if file is being read
    if (dna_file){
        cout << "dnaSequence.txt can be read." << endl;
        cout << "Proceeding to analysis of the DNA Sequence..." << endl;
    } else {
        cout << "Error opening dnaSequence.txt: Please try again" << endl;
    }
                
    // Reading the nucleotides of the File
    cout << " " << endl;
                
    while (dna_file >> numbOfNucleotidesAdenine) {
        switch (numbOfNucleotidesAdenine)
            case 'A':
            numbOfAdenine++;
    }
                
    while (dna_file >> numbOfNucleotidesThymine) {
        switch (numbOfNucleotidesThymine)
            case 'T':
            numbOfThymine++;
    }
                
    while (dna_file >> numbOfNucleotidesCytosine) {
        switch (numbOfNucleotidesCytosine)
            case 'C':
            numbOfCytosine++;
    }
                    
    while (dna_file >> numbOfNucleotidesGuanine) {
        switch (numbOfNucleotidesGuanine)
            case 'G':
            numbOfGuanine++;
    }
                    
    cout << numbOfAdenine;
    cout << numbOfThymine;
    cout << numbOfCytosine;
    cout << numbOfGuanine;
                
    return 0;
}

我希望得到我的每个输入的值:
~8000 ~6000 ~5000 ~5000
只是想知道我做错了什么。

gz5pxeao

gz5pxeao1#

正如在注解中提到的,你需要初始化你的变量。如果不这样做,它们的初始值是未定义的,所以它们的值在递增时也是未定义的。
例如

int numbOfAdenine  = 0;
    int numbOfThymine  = 0;
    int numbOfCytosine = 0;
    int numbOfGuanine  = 0;

然后,您可以在一个循环中递增它们。

char code = '\0';
while (dna_file >> code) {
    switch (code) {
        case 'A':
        numOfAdenine++;
        break;

        case 'G':
        numfGuanine++;
        break;
    
        // etc.
    }
}

这也是应用std::map的绝佳位置。

#include <iostream>
#include <map>
#include <string>

int main() {
    std::string test = "AACGT";
    std::map<char, int> m;
    for (auto ch : test) {
        m[ch]++;
    }

    for (auto &[k, v] : m) {
        std::cout << k << ": " << v << std::endl;
    }

    return 0;
}

输出:

A: 2
C: 1
G: 1
T: 1

相关问题