// Programmer: Jennifer Leopold date: October 4, 2014
// File: hw6fncts.cpp
// Purpose: This file contains the definitions of the functions
// disease diagnosis program.
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "hw6.h"
using namespace std;
void greet()
{
cout << "\nWelcome to " << PROGRAM_NAME << endl;
return;
}
void outputProteinData(const proteinData pData)
{
cout << "Protein Data:\n";
cout << "\tProtein p1 = ";
if (pData.p1 == -1)
cout << COLLECTION_ERROR;
else cout << pData.p1 << endl;
cout << "\tProtein p2 = ";
if (pData.p2 == -1)
cout << COLLECTION_ERROR;
else cout << pData.p2 << endl;
cout << "\tProtein p3 = ";
if (pData.p3 == -1)
cout << COLLECTION_ERROR;
else cout << pData.p3 << endl;
cout << "\tProtein p4 = ";
if (pData.p4 == -1)
cout << COLLECTION_ERROR;
else cout << pData.p4 << endl;
cout << "\tProtein p5 = ";
if (pData.p5 == -1)
cout << COLLECTION_ERROR;
else cout << pData.p5 << endl;
return;
}
bool getProteinData(proteinData &collectedData)
{
short p1a = -1, p1b = -1; // Protein data values to be
float p2a= -1, p2b = -1; // collected; -1 means not
short p3 = -1; // yet actually collected
short p4a = -1, p4b = -1;
short p5 = -1;
bool p1Collected = false, // Easier way to keep track
p2Collected = false, // of whether each protein
p3Collected = false, // got collected than
p4Collected = false, // checking for -1 values
p5Collected = false;
if (toneWasHeard("A-flat"))
{
toneA(p1a, p2a, p3);
p1Collected = true;
p2Collected = true;
p3Collected = true;
}
if (toneWasHeard("middle-G"))
{
toneG(p2b, p4a);
p2Collected = true;
p4Collected = true;
}
if (toneWasHeard("C-flat"))
{
toneC(p1b, p4b, p5);
p1Collected = true;
p4Collected = true;
p5Collected = true;
}
// Note: When multiple tone tests can collect a
// protein, we want either the max or min of those
// values depending on which protein it is
collectedData.p1 = (p1a > p1b)? p1a : p1b;
collectedData.p2 =
((p2a < p2b) && (p2a != -1))? p2a : p2b;
collectedData.p3 = p3;
collectedData.p4 = (p4a > p4b)? p4a : p4b;
collectedData.p5 = p5;
// Overall data collection successful if all proteins
// were collected
return(p1Collected && p2Collected && p3Collected &&
p4Collected && p5Collected);
}
bool toneWasHeard(const string tone)
{
bool wasHeard;
// Sometimes tone will not be heard
wasHeard = ((rand() % 100) + 1) >
HOW_OFTEN_TONE_NOT_HEARD;
if (!wasHeard)
cout << "\nTone " << tone << " was not heard!\n\n";
return(wasHeard);
}
void toneA(short &p1, float &p2, short &p3)
{
p1 = getProteinP1();
p2 = getProteintP2();
p3 = rand() % MAX_VAL_FOR_P3;
return;
}
void toneG(float &p2, short &p4)
{
p2 = getProteintP2();
p4 = getProteintP4();
return;
}
void toneC(short &p1, short &p4, short &p5)
{
p1 = getProteinP1();
p4 = getProteintP4();
p5 = rand() % MAX_VAL_FOR_P5;
return;
}
short getProteinP1()
{
short p1 = rand() % MAX_VAL_FOR_P1;
if (p1 < 10)
p1 = 0; // p1 is undetectable when less than 10
return(p1);
}
float getProteintP2()
{
return(rand() % MAX_VAL_FOR_P1);
}
short getProteintP4()
{
return((rand() % MAX_VAL_FOR_P4) + 10);
}
bool determinePrognosis(const proteinData &pData)
{
bool hasDisease = false;
if (pData.p2 <= 14.6)
hasDisease = true;
else
{
if (pData.p5 < 2)
{
hasDisease = (pData.p4 > 20) &&
(pData.p4 < 40) &&
(pData.p3 > 6);
}
else // pData.p5 >= 2
{
hasDisease = (pData.p1 + pData.p2 + pData.p3) < 75;
}
}
return(hasDisease);
}
bool promptForBool(string prompt)
{
bool invalidInput; // true if input not 'y' or 'n'
char inputValue; // user's input
do
{
cout << prompt << " (y/n) ";
cin >> inputValue;
invalidInput = (inputValue != 'y') &&
(inputValue != 'n');
if (invalidInput)
cout << "Invalid input!\n";
} while (invalidInput);
return(inputValue == 'y');
}
void goodbye()
{
cout << "\nShutting down "
<< PROGRAM_NAME << endl;
cout << "Goodbye...\n";
return;
}