Solution

// Nathan Eloe PI DAY 2013

// timecalc.cpp

// A calculator to determine the amount of time Marge has left in a day

// after helping her deadbeat family

#include <iostream>

#include <string>

using namespace std;

const short MIN_MSM = 1;

const short MAX_MSM = 100;

const short MIN_BABY_SPAM = 0;

const short MAX_BABY_SPAM = 10;

const short MIN_HUNGER = 1;

const short MAX_HUNGER = 3;

const short MINUTES_PER_DAY = 600;

const string USER = "Marge";

const string ODD_MSG = "Lisa, try not to kill your brother.";

const string EVEN_MSG = "Have a wonderful day, Lisa!";

// Desc: These functions greet the user or tell them to go away

// Pre:

// Post: a greeting or sendoff is printed to the screen

void greet();

void signoff();

// Desc: Prints the amount of remaining time in a pretty manner

// Pre:

// Post: Outputs the remaining time couched in a message.

void displayResults(const short time);

// Desc: Gets a number between lbound and ubound

// Pre: lbound < ubound

// Post: Prompts the user for input until in range [lbound, ubound]

// Returns number in range [lbound, ubound]

short boundedInput(const short lbound, const short ubound);

// Desc: Gets the user's state of mind

// Pre:

// Post: Outputs prompts to the screen

// returns a number in the range (MIN_MSM, MAX_MSM)

short getStateOMind();

// Desc: Handles the user's service of Lisa

// Pre:

// Post: Outputs a message for the user to tell Lisa:

// ODD_MSG if mindState is ODD

// EVEN_MSG if mindState is EVEN

// Returns the amount of time spent on Lisa

short serveLisa(const short mindState);

// Desc: Handles the user's service of Maggie

// Pre:

// Post: Prompts the user for input (number cans of baby food)

// Returns the amount of time spent on Maggie

short serveMaggie(const short mindState);

// Desc: Handles the user's service of Homer

// Pre:

// Post: Prompts the user for input (Homer's hunger)

// Returns the amount of time spent on Homer

short serveHomer(const short mindState);

// Desc: Handles the user's service of Bart

// Pre: midState may not be zero

// Post: Returns the amount of time spent on Bart

short serveBart(const short mindState, const short totalTime);

// Desc: Determines the amount of time user has left to do her chores

// Pre:

// Post: Returns the amount of time user has left to do her chores

short userTime(const short totalTime);

// Desc: determines the degree of hunger of a person

// Pre:

// Post: Returns val*val - 1

short doh(const short val);

int main()

{

short timeSpent, stateOMind, timeLeft;

greet();

stateOMind = getStateOMind();

timeSpent = serveLisa(stateOMind);

timeSpent += serveMaggie(stateOMind);

timeSpent += serveHomer(stateOMind);

timeSpent += serveBart(stateOMind, timeSpent);

timeLeft = userTime(timeSpent);

displayResults(timeLeft);

signoff();

return 0;

}

void greet()

{

cout << "Good morning " << USER

<< "Let's get your family's morning started." << endl;

return;

}

void signoff()

{

cout << "Good bye " << USER << ". Good luck doing everything!" << endl;

return;

}

short boundedInput(const short lbound, const short ubound)

{

short input;

do

{

cout << "[" << lbound << "-" << ubound << "]: ";

cin >> input;

if (input < lbound || input > ubound)

cout << "Input must be between " << lbound << " and " << ubound

<< " inclusive. Please try again. ";

} while (input < lbound or input > ubound);

cout << endl;

return input;

}

short getStateOMind()

{

cout << "Please enter your state of mind this morning.\n";

return boundedInput(MIN_MSM, MAX_MSM);

}

short serveLisa(const short mindState)

{

const short BASE_TIME = 1;

cout << ((mindState % 2) ? ODD_MSG : EVEN_MSG) << endl << endl;

return BASE_TIME;

}

short serveMaggie(const short mindState)

{

const short BASE_TIME = 5;

short cans;

cout << "How many cans of baby food have you fed Maggie today? ";

cans = boundedInput(MIN_BABY_SPAM, MAX_BABY_SPAM);

return BASE_TIME * ((cans < mindState) ? cans : mindState);

}

short serveHomer(const short mindState)

{

const short BASE_TIME = 50;

short hunger;

short coeff = 1;

cout << "Dumb Homer needs food BADLY. How badly? " << endl;

cout << "1 = hungry, 2 = very hungry, 3 = Homerian hungry: ";

hunger = boundedInput(MIN_HUNGER, MAX_HUNGER);

if (hunger == 2)

coeff = 2;

else if (hunger == 3)

coeff = 5;

return BASE_TIME + doh(coeff * hunger) + ((hunger == 2) ? mindState : 0);

}

short doh(const short val)

{

return val * val - 1;

}

short serveBart(const short mindState, const short totalTime)

{

const short BASE_TIME = 25;

return BASE_TIME + (totalTime * totalTime) / mindState;

}

short userTime(const short totalTime)

{

const short BASE_TIME = 30;

return MINUTES_PER_DAY - totalTime - BASE_TIME;

}

void displayResults(const short time)

{

cout << USER << ", you have " << time << " minutes left today"

<< " to complete your chores." << endl;

return;

}