我编写了一个能够从文件中读取、输出和计算信息的代码。该文件本身是一个精心设计的 DNA 序列,称为“dnaSequence.txt”。通过我的代码读取文件中的数据时遇到了一些麻烦。我将所有数据放入 3 个不同的函数中,这些函数能够正确显示并创建我需要制作的文件,但我遇到的问题是这些函数无法读取文件。想知道我做错了什么。
我应该使用 ostream& type 参数,但不确定将它放在哪里。
int Adenine = 0;
int Thymine = 0;
int Cytosine = 0;
int Guanine = 0;
char numbNucs;
float totalNucleotides = 0;
// func 1:
void nucleotideCounterFunc (int Adenine, int Thymine, int Cytosine, int Guanine, char numbOfNucs, float totalNucleotides){
ifstream dnafile("DNA.txt");
if (dnafile){
cout << "DNA.txt can be read." << endl;
cout << "Proceeding to analysis of the DNA Sequence..." << endl;
} else {
cout << "Error opening dnaSequence.txt: Please try again" << endl;
}
cout << " " << endl;
while(dna_file >> numbNucs){
if (numbNucs == 'A')
{
Adenine++;
}
else if (numbNucs == 'T')
{
Thymine++;
}
else if (numbNucs == 'C'){
Cytosine++;
}
else if (numbNucs == 'G'){
Guanine++;
}
}
}
// func 2:
void formatOutputFunc (int Adenine, int Thymine, int Cytosine, int Guanine, float totalNucleotides){
totalNucleotides = Adenine + Thymine + Guanine + Cytosine;
cout << "DNA Sequence Analysis: " << endl;
cout << totalNucleotides << " nucleotides in the sequence" << endl;
cout << " " << endl;
cout << "Sequence breakdown: " << endl;
cout << "Adenine: " << Adenine << " " << (Adenine/totalNucleotides) * 100 << "%" << endl;
cout << "Thymine: " << Thymine << " " << (Thymine/totalNucleotides) * 100 << "%" << endl;
cout << "Cytosine: " << Cytosine << " " << (Cytosine/totalNucleotides) * 100 << "%" << endl;
cout << "Guanine: " << Guanine << " " << (Guanine/totalNucleotides) * 100 << "%" << endl;
}
// func 3:
void formatNewFileFunc(int Adenine, int Thymine, int Cytosine, int Guanine, float totalNucleotides){
ofstream dna_finished("Post_Analysis.txt");
if (dna_finished) {
cout << "DNA Sequence Analysis Complete" << endl;
}
dna_finished<< totalNucleotides << " nucleotides in the sequence" << endl;
dna_finished<< " " << endl;
dna_finished<< "Sequence breakdown: " << endl;
dna_finished<< "Adenine: " << Adenine << " " << (Adenine/totalNucleotides) * 100 << "%" << endl;
dna_finished<< "Thymine: " << Thymine << " " << (Thymine/totalNucleotides) * 100 << "%" << endl;
dna_finished<< "Cytosine: " << Cytosine << " " << (Cytosine/totalNucleotides) * 100 << "%" << endl;
dna_finished<< "Guanine: " << Guanine << " " << (Guanine/totalNucleotides) * 100 << "%" << endl;
dna_finished<< " " << endl;
}
int main()
{
nucleotideCounterFunc(Adenine, Thymine, Cytosine, Guanine, numbOfNucs, totalNucleotides);
cout << " " << endl;
formatOutputFunc(Adenine, Thymine, Cytosine, Guanine, totalNucleotides);
cout << " " << endl;
formatNewFileFunc(Adenine, Thymine, Cytosine, Guanine, totalNucleotides);
return 0;
}
至于我的输出:一切都是 ~nan% 很想知道我做错了什么。 我知道我应该使用 ostream& 参数类型但不确定如何集成它