#include #include #include #include #include using namespace std; int main(){ // Euler's constant const double e = 2.718128; // 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 double kAgeFactor = 0.0799; double kTotalCholesterolFactor = 1.382; double kHDLCholesterolFactor = -1.172; double kSystolicFactor = 3.137; double kCRPFactor = 0.18; double kSmokerFactor = 0.818; double kFamilyFactor = 0.438; double kComputeFactor = 0.98634; double kBSubtractFactor = 22.325; string input_filename; string output_filename; ifstream datafile; ofstream outputfile; int age = 0; int totalCholesterol = 0; int HDLCholesterol = 0; int systolicBP = 0; double CRP = 0.0; char smoker = ' '; bool isSmoker = false; char familyHistory = ' '; bool hasFamilyHistory = false; double B = 0.0; double riskPct = 0.0; double total = 0.0; double average = 0.0; int subjectCount = 0; int goodSubjectCount = 0; bool subjectOK = true; cout << "This program computes the Reynolds Risk Score for non-diabetic women." << endl; cout << "Information about the Reynolds Risk Score can be found at: " << endl; cout << "http://jama.jamanetwork.com/article.aspx?articleid=205528" << endl << endl; cout << "Please enter the name of the input file: "; getline (cin,input_filename); // open file if it is there datafile.open(input_filename.c_str()); if (!datafile) { cout << "Could not open file: "<< input_filename << " for reading." << endl; return 0; } // Now that we don't expect errors, open the output file for writing cout << "Enter the name of a file to write the output to: "; getline(cin, output_filename); outputfile.open(output_filename.c_str()); if (! outputfile.is_open()){ cout << "Could not open file: "<< output_filename << " for writing." << endl; return 0; } // Get rid of the header line that shows what the values are string junkHeader; getline(datafile,junkHeader); cout << fixed << setprecision(2); outputfile << fixed << setprecision(2); while (datafile >> age >> systolicBP >> CRP >> totalCholesterol >> HDLCholesterol >> smoker >> familyHistory){ subjectCount++; subjectOK = true; // cout << " " << subjectCount << " " << age << " " << systolicBP << " " << CRP << " " // << totalCholesterol << " " << HDLCholesterol << " " << smoker << " " // << familyHistory << endl; // Do range checking to make sure the values are reasonable // For a value that is out of range, throw an error and skip that line if ((age < kMinAge)||(age > kMaxAge)){ cout << "Age " << age << " out of range; skipping entry " << subjectCount << "." << endl; subjectOK = false; } if ((totalCholesterol < kMinTotalCholesterol)||(totalCholesterol > kMaxTotalCholesterol)){ cout << "TotalCholesterol " << totalCholesterol << " out of range; skipping entry " << subjectCount << "." << endl; subjectOK = false; } if ((HDLCholesterol < kMinHDLCholesterol)||(HDLCholesterol > kMaxHDLCholesterol)){ cout << "HDLCholesterol " << HDLCholesterol << " out of range; skipping entry " << subjectCount << "." << endl; subjectOK = false; } if ((CRP < kMinCRP)||(CRP > kMaxCRP)){ cout << "C-Reactive Protein " << CRP << " out of range; skipping entry " << subjectCount << "." << endl; subjectOK = false; } if ((systolicBP < kMinSystolicBP)||(systolicBP > kMaxSystolicBP)){ cout << "Blood Pressure " << systolicBP << " out of range; skipping entry " << subjectCount << "." << endl; subjectOK = false; } if (subjectOK){ goodSubjectCount++; // Check if the patient is a smoker if ((smoker == 'Y')||(smoker == 'y')){ isSmoker = true; } else { isSmoker = false; } // Check if the patient is a smoker if ((familyHistory == 'Y')||(familyHistory == 'y')){ hasFamilyHistory = true; } else { hasFamilyHistory = false; } // Compute the B factor, reseting it first so we don't // carry computation between subjects B = 0.0; // Compute the age part //cout << "Age factor: " << kAgeFactor * age << endl; B += kAgeFactor * age; // Compute the systolic blood pressure part //cout << "Systolic factor: " << kSystolicFactor * log (systolicBP) << endl; B += kSystolicFactor * log (systolicBP); // Compute the CRP part //cout << "CRP factor: " << kCRPFactor * log(CRP) << endl; B += kCRPFactor * log(CRP); // The total cholesterol part //cout << "Chol factor: " << kTotalCholesterolFactor * log(totalCholesterol) << endl; B += kTotalCholesterolFactor * log(totalCholesterol); // The HDL cholesterol part //cout << "HDL Factor: " << kHDLCholesterolFactor * log(HDLCholesterol) << endl; B += kHDLCholesterolFactor * log(HDLCholesterol); // take into account if they are smokers if (isSmoker){ //cout << "Smoker factor: " << kSmokerFactor << endl; B += kSmokerFactor; } // or if they had family history of heart disease if (hasFamilyHistory){ //cout << "Family factor: " << kFamilyFactor << endl; B += kFamilyFactor; } //cout << "B: " << B << endl; // Compute the overall percentage // double power = pow(e, B - kBSubtractFactor); double power = exp(B - kBSubtractFactor); //cout << "Power B-22.325: " << power << endl; riskPct = (1 - pow(kComputeFactor,power)) * 100; if (riskPct < 1) riskPct = 1; cout << "Subject " << setw(4) << subjectCount << " Risk percent: " << setw(6) << riskPct << endl; outputfile << "Subject " << setw(4) << subjectCount << " Risk percent: " << setw(6) << riskPct << endl; total += riskPct; } } if (0 == subjectCount){ cout << "No subjects in subject file." << endl; } else { average = total/goodSubjectCount; cout << "Average risk percent: " << setw(11) << average << endl; outputfile << "Average risk percent: " << setw(11) << average << endl; } return 0; }