#include #include #include #include #include #include using namespace std; // Constants for range checking const int kMinAge = 20; const int kMaxAge = 79; const int kMinTotalCholesterol = 130; const int kMaxTotalCholesterol = 320; const int kMinHDLCholesterol = 20; const int kMaxHDLCholesterol = 100; const int kMinSystolicBP = 90; const int kMaxSystolicBP = 300; const double kMinCRP = 0.02; const double kMaxCRP = 20; // Constants for score const double kAgeFactor = 0.0799; const double kTotalCholesterolFactor = 1.382; const double kHDLCholesterolFactor = -1.172; const double kSystolicFactor = 3.137; const double kCRPFactor = 0.18; const double kSmokerFactor = 0.818; const double kFamilyFactor = 0.438; const double kComputeFactor = 0.98634; const double kBSubtractFactor = 22.325; //function prototypes char GetMenuSelection(); bool CheckFile(string); void LoadFile(string, vector &, vector &, vector &, vector &, vector &, vector &, vector &, vector &); void PrintSubjects(const vector, const vector, const vector, const vector, const vector, const vector, const vector, const vector); double ReynoldsScore (const int, const int, const double, const int, const int, const bool, const bool); void PrintSubject(const string, const int, const int, const double, const int, const int, const bool, const bool); double AverageReynolds(const int, const int, const vector, const vector, const vector, const vector, const vector, const vector, const vector); void FindSubject(const string, const vector, const vector, const vector, const vector, const vector, const vector, const vector, const vector); int main(){ char choice; bool finished = false; string filename; int minAge = kMinAge; int maxAge = kMaxAge; string subjectID; vector subjectIDs; vector subjectSystolics; vector subjectAges; vector subjectCRPs; vector subjectTotChols; vector subjectHDLChols; vector subjectSmokers; vector subjectHistories; cout << fixed << setprecision(2); while (!finished){ choice = GetMenuSelection(); switch (choice){ case'L': case'l': cout << "Enter name of file to load: "; getline(cin, filename); LoadFile(filename, subjectIDs, subjectAges, subjectSystolics, subjectCRPs, subjectTotChols, subjectHDLChols, subjectSmokers, subjectHistories); break; case'A': case'a': cout << endl << endl << "Average Reynolds score over all subjects is: " << AverageReynolds(kMinAge, kMaxAge, subjectAges, subjectSystolics, subjectCRPs, subjectTotChols, subjectHDLChols, subjectSmokers, subjectHistories) << endl << endl; break; case'R': case'r': cout << endl << endl << "Enter the minimum age of the group to average: "; cin >> minAge; cout << endl << endl << "Enter the maximum age of the group to average: "; cin >> maxAge; cout << endl << endl << "Average Reynolds score over subjects aged " << minAge << " to " << maxAge << " is: " << AverageReynolds(minAge, maxAge, subjectAges, subjectSystolics, subjectCRPs, subjectTotChols, subjectHDLChols, subjectSmokers, subjectHistories) << endl << endl; break; case'S': case's': cout << endl << endl << "Please enter the subject id: "; cin >> subjectID; FindSubject(subjectID, subjectIDs, subjectAges, subjectSystolics, subjectCRPs, subjectTotChols, subjectHDLChols, subjectSmokers, subjectHistories); break; case'P': case'p': PrintSubjects(subjectIDs, subjectAges, subjectSystolics, subjectCRPs, subjectTotChols, subjectHDLChols, subjectSmokers, subjectHistories); break; case'Q': case'q': finished = true; break; default: break; } } return 0; } //////////////// // Returns the choice the user selected after printing // the menu char GetMenuSelection(){ char choice; cout << endl << endl; cout << setw(45) << "Add a subject file" << setw (10) << "L" << endl; cout << setw(45) << "Compute the average Reynolds risk score" << setw(10) << "A" << endl; cout << setw(45) << "Compute the average Reynolds risk score" << endl << setw(45) << "for an age range" << setw(10) << "R" << endl; cout << setw(45) << "Search for a subject ID" << setw(10) << "S" << endl; cout << setw(45) << "Print all subjects" << setw (10) << "P" << endl; cout << setw(45) << "Exit the program" << setw (10) << "Q" << endl; cout << endl; cout << "Please enter your choice: "; cin >> choice; cin.clear(); string trash; getline (cin,trash); return choice; cout << endl << endl; } //////////////// // Checks a file to make sure all values are within limits. // Returns true if file is ok, false otherwise bool CheckFile(string filename){ string id; int age = 0; int totalCholesterol = 0; int HDLCholesterol = 0; int systolicBP = 0; double CRP = 0.0; char smoker = ' '; char familyHistory = ' '; bool file_good = true; ifstream file_to_check; file_to_check.open(filename.c_str()); if (!file_to_check){ cout << "Unable to open file " << filename << endl; return false; } string header; getline(file_to_check, header); while (file_to_check >> id >> age >> systolicBP >> CRP >> totalCholesterol >> HDLCholesterol >> smoker >> familyHistory){ if ((age < kMinAge)||(age > kMaxAge)){ cout << "Subject ID " << id << "age " << age << " out of range."<< endl; file_good = false; } if ((totalCholesterol < kMinTotalCholesterol)||(totalCholesterol > kMaxTotalCholesterol)){ cout << "Subject ID " << id << " Total Cholesterol " << totalCholesterol << " out of range." << endl; file_good = false; } if ((HDLCholesterol < kMinHDLCholesterol)||(HDLCholesterol > kMaxHDLCholesterol)){ cout << "Subject ID " << id << " HDLCholesterol " << HDLCholesterol << " out of range." << endl; file_good = false; } if ((CRP < kMinCRP)||(CRP > kMaxCRP)){ cout << "Subject ID " << id << "C-Reactive Protein " << CRP << " out of range." << endl; file_good = false; } if ((systolicBP < kMinSystolicBP)||(systolicBP > kMaxSystolicBP)){ cout << "Subject ID " << id << "Blood Pressure " << systolicBP << " out of range." << endl; file_good = false; } switch (smoker){ case 'Y': case 'y': case 'n': case 'N': break; default: cout << "Subject ID " << id << "Smoker entry " << smoker << " is bad." << endl; file_good = false; } switch (familyHistory){ case 'Y': case 'y': case 'n': case 'N': break; default: cout << "Subject ID " << id << "Family history " << familyHistory << " is bad." << endl; file_good = false; } } file_to_check.close(); return file_good; } //////////////// // Loads a file if the values are within range // void LoadFile(string filename, vector & ids, vector & ages, vector & systolics, vector & crps, vector & total_chol, vector & hdl_chol, vector & smokers, vector & histories) { string id; int age = 0; int totalCholesterol = 0; int HDLCholesterol = 0; int systolicBP = 0; double CRP = 0.0; char smoker = ' '; char familyHistory = ' '; int count = 0; if (!CheckFile(filename)){ cout << "Errors in subject file. No subjects added." << endl; return; } ifstream subject_file; subject_file.open(filename.c_str()); if (!subject_file){ cout << "Unable to open file " << filename << endl; return; } string header; getline(subject_file, header); while (subject_file >> id >> age >> systolicBP >> CRP >> totalCholesterol >> HDLCholesterol >> smoker >> familyHistory){ count++; ids.push_back(id); ages.push_back(age); systolics.push_back(systolicBP); crps.push_back(CRP); total_chol.push_back(totalCholesterol); hdl_chol.push_back(HDLCholesterol); if ((smoker == 'y')||(smoker == 'Y')){ smokers.push_back(true); } else { smokers.push_back(false); } if ((familyHistory == 'y')||(familyHistory == 'Y')){ histories.push_back(true); } else { histories.push_back(false); } } subject_file.close(); cout << "Loaded " << count << " subjects from " << filename << "." << endl; } //////////////// // Prints all subjects void PrintSubjects ( const vector ids, const vector ages, const vector systolics, const vector crps, const vector total_chol, const vector hdl_chol, const vector smokers, const vector histories) { // sort all the subjects by age, just for fun for (int age_for_sorting = kMinAge; age_for_sorting <= kMaxAge; age_for_sorting++){ for (int n = 0; n < ids.size(); n++){ if (ages[n] == age_for_sorting){ PrintSubject(ids[n], ages[n], systolics[n], crps[n], total_chol[n], hdl_chol[n], smokers[n],histories[n]); } } } } ////////////// // Find a particular subject by id and then print their data void FindSubject ( const string idToFind, const vector ids, const vector ages, const vector systolics, const vector crps, const vector total_chol, const vector hdl_chol, const vector smokers, const vector histories) { // sort all the subjects by age, just for fun for (int n = 0; n < ids.size(); n++){ if (ids[n] == idToFind){ PrintSubject(ids[n], ages[n], systolics[n], crps[n], total_chol[n], hdl_chol[n], smokers[n],histories[n]); } } } ////////// // Print a subject neatly void PrintSubject(const string id, const int age, const int systolic, const double crp, const int total_chol, const int hdl_chol, const bool smoker, const bool history){ cout << setw(8) << id << setw(4) << age << setw(5) << systolic << setw(7) << crp << setw(5) << total_chol << setw(5) << hdl_chol; if (smoker) { cout << setw(3) << 'Y' << " "; } else { cout << setw(3) << 'N' << " "; } if (history) { cout << setw(3) << 'Y' << " "; } else { cout << setw(3) << 'N' << " "; } cout << setw(7) << ReynoldsScore(age, systolic, crp, total_chol, hdl_chol, smoker,history) << endl; } ////////// // Compute the Renolds score for one subject double ReynoldsScore (const int age, const int systolic, const double crp, const int total_chol, const int hdl_chol, const bool smoker, const bool history){ double riskPct = 0.0; double B = 0.0; // Compute the age part B += kAgeFactor * age; // Compute the systolic blood pressure part B += kSystolicFactor * log (systolic); // Compute the CRP part B += kCRPFactor * log(crp); // The total cholesterol part B += kTotalCholesterolFactor * log(total_chol); // The HDL cholesterol part B += kHDLCholesterolFactor * log(hdl_chol); // take into account if they are smokers if (smoker){ B += kSmokerFactor; } // or if they had family history of heart disease if (history){ B += kFamilyFactor; } // Compute the overall percentage double power = exp(B - kBSubtractFactor); riskPct = (1 - pow(kComputeFactor,power)) * 100; if (riskPct < 1) riskPct = 1; return riskPct; } ////////// // Compute the average Reynolds risk score for an age group double AverageReynolds(const int minAge, const int maxAge, const vector ages, const vector systolics, const vector crps, const vector total_chol, const vector hdl_chol, const vector smokers, const vector histories){ double total = 0.0; int subjectCount = 0; for (int n = 0; n < ages.size(); n++){ if ((ages[n] >= minAge)&&(ages[n] <= maxAge)){ total += ReynoldsScore(ages[n],systolics[n],crps[n], total_chol[n],hdl_chol[n],smokers[n], histories[n]); subjectCount ++; } } return total/subjectCount; }