#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; char GetMenuSelection(); class ReynoldsSubject { friend ostream &operator<<(ostream &, ReynoldsSubject); public: ReynoldsSubject(); ReynoldsSubject(string, int, int, double, int, int, char, char); bool inAgeRange(int, int); bool isSubjectID(string); double getReynoldsScore(); bool goodSubjectData(); private: string ID; int systolic; int age; double CRP; int totalChol; int HDLChol; bool smoker; bool history; double Reynolds; bool dataGood; void ComputeReynolds(); }; ReynoldsSubject::ReynoldsSubject(){ ID = ""; systolic = 0; age = 0; CRP = 0; totalChol = 0; HDLChol = 0; smoker = false; history = false; Reynolds = 0; dataGood = false; } ReynoldsSubject::ReynoldsSubject(string newID, int newAge, int newSystolic, double newCRP, int newTotalChol, int newHDLChol, char newSmoker, char newHistory){ //char sp = ' '; // cerr << newID << sp << newAge << sp << newSystolic << sp << newCRP << sp // << newTotalChol << sp << newHDLChol << sp << newSmoker << sp // << newHistory << endl; dataGood = true; if ((newAge < kMinAge)||(newAge > kMaxAge)){ cout << "Subject ID " << newID << "age " << age << " out of range."<< endl; dataGood = false; } if ((newTotalChol < kMinTotalCholesterol)||(newTotalChol > kMaxTotalCholesterol)){ cout << "Subject ID " << newID << " Total Cholesterol " << totalChol << " out of range." << endl; dataGood = false; } if ((newHDLChol < kMinHDLCholesterol)||(newHDLChol > kMaxHDLCholesterol)){ cout << "Subject ID " << newID << " HDLCholesterol " << HDLChol << " out of range." << endl; dataGood = false; } if ((newCRP < kMinCRP)||(newCRP > kMaxCRP)){ cout << "Subject ID " << newID << "C-Reactive Protein " << CRP << " out of range." << endl; dataGood = false; } if ((newSystolic < kMinSystolicBP)||(newSystolic > kMaxSystolicBP)){ cout << "Subject ID " << newID << "Blood Pressure " << systolic << " out of range." << endl; dataGood = false; } switch (newSmoker){ case 'Y': case 'y': smoker = true; break; case 'n': case 'N': smoker = false; break; default: cout << "Subject ID " << newID << "Smoker entry " << newSmoker << " is bad." << endl; dataGood = false; } switch (newHistory){ case 'Y': case 'y': history = true; break; case 'n': case 'N': history = false; break; default: cout << "Subject ID " << newID << "Family history " << newHistory << " is bad." << endl; dataGood = false; } ID = newID; systolic = newSystolic; age = newAge; CRP = newCRP; totalChol = newTotalChol; HDLChol = newHDLChol; ComputeReynolds(); } ostream &operator<<(ostream &output, ReynoldsSubject r){ output << fixed << setprecision(2); output << setw(8) << r.ID << setw(4) << r.age << setw(5) << r.systolic << setw(7) << r.CRP << setw(5) << r.totalChol << setw(5) << r.HDLChol; if (r.smoker) { output << setw(3) << 'Y' << " "; } else { output << setw(3) << 'N' << " "; } if (r.history) { output << setw(3) << 'Y' << " "; } else { output << setw(3) << 'N' << " "; } output << setw(7) << r.Reynolds; return output; } void ReynoldsSubject::ComputeReynolds(){ // 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; 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(totalChol); // The HDL cholesterol part B += kHDLCholesterolFactor * log(HDLChol); // 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; Reynolds = riskPct; } bool ReynoldsSubject::inAgeRange(int minAge, int maxAge) { if ((age >= minAge) && (age <= maxAge)) return true; else return false; } bool ReynoldsSubject::isSubjectID(string checkThis) { if (ID == checkThis) return true; else return false; } double ReynoldsSubject::getReynoldsScore(){ return Reynolds; }; bool ReynoldsSubject::goodSubjectData(){ return dataGood; } class ReynoldsCohort{ public: ReynoldsCohort(); void loadCohort(string); bool checkCohort(); void printCohort(); void findSubject(string); double averageReynolds(); double averageReynolds(int, int); private: vector cohort; }; ReynoldsCohort::ReynoldsCohort(){ cohort.clear(); } void ReynoldsCohort::loadCohort(string filename){ string id; int age = 0; int totalCholesterol = 0; int HDLCholesterol = 0; int systolicBP = 0; double CRP = 0.0; char smoker = ' '; char familyHistory = ' '; 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){ ReynoldsSubject subject(id, age, systolicBP, CRP, totalCholesterol, HDLCholesterol, smoker, familyHistory); cohort.push_back(subject); } } bool ReynoldsCohort::checkCohort(){ bool retval = true; for (int i = 0; i < cohort.size(); i++){ if (!cohort[i].goodSubjectData()){ retval = false; break; } } return retval; } void ReynoldsCohort::printCohort(){ for (int age_for_sorting = kMinAge; age_for_sorting <= kMaxAge; age_for_sorting++){ for (int i = 0; i < cohort.size(); i++){ if (cohort[i].inAgeRange(age_for_sorting,age_for_sorting)){ cout << cohort[i] << endl; } } } } void ReynoldsCohort::findSubject(string subjectID){ for (int i = 0; i < cohort.size(); i++){ if (cohort[i].isSubjectID(subjectID)) cout << cohort[i] << endl; } } double ReynoldsCohort::averageReynolds(){ double total = 0.0; for (int i = 0; i < cohort.size(); i++){ total += cohort[i].getReynoldsScore(); } if (cohort.size() > 0){ return total/cohort.size(); } else { return 0; } } double ReynoldsCohort::averageReynolds(int min, int max){ double total = 0.0; int count = 0; for (int i = 0; i < cohort.size(); i++){ if (cohort[i].inAgeRange(min, max)){ total += cohort[i].getReynoldsScore(); count++; } } if (count > 0){ return total/count; } else { return 0; } } int main(){ bool finished = false; ReynoldsCohort cohort; char choice; string filename; int minAge = 0; int maxAge = 0; string subjectID; cout << fixed << setprecision(2); while (!finished){ choice = GetMenuSelection(); switch (choice){ case'L': case'l': cout << "Enter name of file to load: "; getline(cin, filename); cohort.loadCohort(filename); if (cohort.checkCohort()){ cout << "Load successful, data is good." << endl; } else { cout << "Load successful, but data is out of range." << endl; } break; case'A': case'a': cout << endl << endl << "Average Reynolds score over all subjects is: " << cohort.averageReynolds() << 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: " << cohort.averageReynolds(minAge, maxAge) << endl << endl; break; case'S': case's': cout << endl << endl << "Please enter the subject id: "; cin >> subjectID; cohort.findSubject(subjectID); break; case'P': case'p': cohort.printCohort(); 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) << "Load a cohort 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 in cohort" << 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; }