Home / utk / cs102 / fa16 / labi / code_snippets / section3 / disease_test.cpp
Directory Listing
disease_input.txt
disease_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>

using namespace std;

const int NUM_DISEASE = 5;

typedef struct disease {
	string diseaseName;

	double populationWithDisease,
	       accuracyNoDisease,
		   accuracyWithDisease;
} DISEASE;

int read_from_file(ifstream &, DISEASE[]);
void reverse_array(DISEASE[], int);
void print_diseases(ofstream &, DISEASE[], int);

int main() {
	DISEASE my_diseases[NUM_DISEASE];
	string fname, result_name;
	int lines_read;

	//Get name of file
	cout << "Input Filename: ";
	cin >> fname;
	cout << "Output Filename: ";
	cin >> result_name;
	
	//Read in file
	ifstream fin;
	fin.open(fname);
	if (fin.fail()) {
		cout << "Unable to open file" << endl;
		return -1;
	}
	
	//Parse all lines in file
	lines_read = read_from_file(fin, my_diseases);
	fin.close();

	reverse_array(my_diseases, lines_read);

	ofstream fout;
	fout.open(result_name);
	if (fout.fail()) {
		cout << "Unable to open file" << endl;
		return -2;
	}

	print_diseases(fout, my_diseases, lines_read);

	fout.close();
}

int read_from_file(ifstream &fin, DISEASE my_diseases[]) {
	string line;
	int total = 0;
	while (getline(fin, line) && total < NUM_DISEASE) {
		if (line != "") {
			//Initialise istringstream
			istringstream sin;
			sin.str(line);
			
			//Read in data to the array
			sin >> my_diseases[total].diseaseName
			    >> my_diseases[total].populationWithDisease
			    >> my_diseases[total].accuracyNoDisease
			    >> my_diseases[total].accuracyWithDisease;
			
			//Increment number of diseases counter
			total += 1;
		}
	}
	return total;
}

void reverse_array(DISEASE my_diseases[], int count) {
	for (int i = 0; i < count / 2; i++) {
		//Set temporary variable to first element
		DISEASE temp;
		temp = my_diseases[i];
		
		//Swap
		my_diseases[i] = my_diseases[count - i - 1];
		my_diseases[count - i - 1] = temp;
	}
}

void print_diseases(ofstream &fout, DISEASE my_diseases[], int count) {
	for (int i = 0; i < count; i++) {
		ostringstream sout;
		sout << my_diseases[i].diseaseName
	}
}