Home / utk / cs102 / fa16 / labi / code_snippets / section4 / 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

const int MAX_DISEASES = 5;

typedef struct disease {
	string diseaseName;

	double percentageWithDisease,
		   accuracyWithoutDisease,
		   accuracyWithDisease;
} Disease;

int read_diseases_from_file(ifstream &, Disease[]);
void reverse_array(Disease[], int);

int main() {
	string fname, oname;
	Disease my_diseases[MAX_DISEASES];
	int line_count;

	cout << "Input Filename: ";
	cin >> fname;
	cout << "Output Filename: ";
	cin >> oname;

	ifstream fin;
	fin.open(fname);
	if (fin.fail()) {
		cout << "Unable to open file" << endl;
		return -1;
	}

	line_count = read_diseases_from_file(fin, my_diseases);
	fin.close();

	reverse_array(my_diseases, line_count);

	for (int i = 0; i < line_count; i++) {
		double probability = ((1 - my_diseases[i].percentageWithDisease) * (1 - my_diseases[i].accuracyWithoutDisease)) / (((1 - my_diseases[i].percentageWithDisease) * (1 - my_diseases[i].accuracyWithoutDisease)) + (my_diseases[i].percentageWithDisease * my_diseases[i].accuracyWithDisease));
		cout << my_diseases[i].diseaseName << " - " << (1 - probability) << endl;
	}
	
	//Have a nice day.
	return 0;
}

int read_diseases_from_file(ifstream &fin, Disease my_diseases[]) {
	string line;
	int i = 0;
	while (getline(fin, line) && i < MAX_DISEASES) {
		if (line != "") {
			istringstream sin;
			sin.clear();
			sin.str(line);
	
			sin >> my_diseases[i].diseaseName
			    >> my_diseases[i].percentageWithDisease
				>> my_diseases[i].accuracyWithoutDisease
				>> my_diseases[i].accuracyWithDisease;
	
			i++;
		}
	}
	return i;
}

void reverse_array(Disease my_diseases[], int count) {
	for (int i = 0; i < count / 2; i++) {
		Disease temp;
		temp = my_diseases[i];
		my_diseases[i] = my_diseases[count - i - 1];
		my_diseases[count - i - 1] = temp;
	}
}