vendredi 17 février 2017

I need to read data from file with numbers and then convert the numbers to letters. Am I going about this correctly?

I need to read data from file containing DNA study and then convert said numbers into corresponding letters. I've made a structure called DNAnt it contains 4 integers to store the number raid from file and a char to store the letter. An array of structures called allDNA stores the data. I have a function data2DNA that read the data from file assigns the corresponding character and then prints the result to a file. The letters are as follows: 1=A, 2=C, 3=G, 4=T Am I going about this the correct way?

Here's the link to the file: http://ift.tt/2kxPC5K

As well as actual task #2: http://ift.tt/2lfJA72

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

//Structures
struct DNAnt{
    int num_1;
    int num_2;
    int num_3;
    int num_4;
    char dna_strand;
};

//Function Prototypes
void data2DNA(DNAnt allDNA[], int SIZE);

int main() {
    int const SIZE = 80;
    DNAnt allDNA[SIZE];

    //Calling the function
    data2DNA(allDNA,SIZE);

    system("PAUSE");
    return 0;
}

void data2DNA(DNAnt allDNA[], int SIZE){
    ifstream infile;
    ofstream outfile;

    infile.open("data.txt"); //Opens file data.txt
    outfile.open("DNA.txt"); //Opens DNA.txt for writing

    if(infile.fail()){ //Checks for file opening.
        cout << "ERROR => File Failed to Open.";
    }

    while(!infile.eof()){
        for(int i=0; i<SIZE; i++){
            //Read data from file into array of structures.
            infile >>  allDNA[i].num_1 >> allDNA[i].num_2 >> allDNA[i].num_3 >> allDNA[i].num_4;

            //num_1
            if(allDNA[i].num_1 == 1){
                allDNA[i].dna_strand = 'A';
            }
            else if(allDNA[i].num_1 == 2){
                allDNA[i].dna_strand = 'C';
            }
            else if(allDNA[i].num_1 == 3){
                allDNA[i].dna_strand = 'G';
            }
            else{
                allDNA[i].dna_strand = 'T';
            }

            //num_2
            if(allDNA[i].num_2 == 1){
                allDNA[i].dna_strand = 'A';
            }
            else if(allDNA[i].num_2 == 2){
                allDNA[i].dna_strand = 'C';
            }
            else if(allDNA[i].num_2 == 3){
                allDNA[i].dna_strand = 'G';
            }
            else{
                allDNA[i].dna_strand = 'T';
            }

            //num_3
            if(allDNA[i].num_3 == 1){
                allDNA[i].dna_strand = 'A';
            }
            else if(allDNA[i].num_3 == 2){
                allDNA[i].dna_strand = 'C';
            }
            else if(allDNA[i].num_3 == 3){
                allDNA[i].dna_strand = 'G';
            }
            else{
                allDNA[i].dna_strand = 'T';
            }

            //num_4
            if(allDNA[i].num_4 == 1){
                allDNA[i].dna_strand = 'A';
            }
            else if(allDNA[i].num_4 == 2){
                allDNA[i].dna_strand = 'C';
            }
            else if(allDNA[i].num_4 == 3){
                allDNA[i].dna_strand = 'G';
            }
            else{
                allDNA[i].dna_strand = 'T';
            }

            //Write results to a file DNA.txt
            outfile << allDNA[i].dna_strand;
        }
    }

    infile.close();
    outfile.close();
}

Aucun commentaire:

Enregistrer un commentaire