// profitFinder.cpp
// Determines the surface that is Krusty's profit based on animal parts
// and kinds of animals
#include <iostream>
#include <cmath>
using namespace std;
//This function greets the user
//Pre: none
//Post: A greeting is output to the screen
void greetUser();
//This function gets a number that is bounded from below from the user.e
//Pre: none
//Post: prompts the user for and returns an integer > lowBound
int getLowBoundedNumber(const int lowBound);
//This function gets the lower and upper bounds for animal parts and number
//of types of animals
//Pre: none
//Post: sets the pass by reference vars to the bounds supplied by the user
void getParameterBounds(int & numPartsLB, int & numPartsUB,
int & numAnimalsLB, int & numAnimalsUB);
//This fuction asks the user whether or not they want to stop generating output
//Pre: none
//Post: Prompts the user for a y or n. Returns true if the input
// corresponds to "no", false otherwise
bool shouldQuit();
//Calculates the profit function for a given number of animal parts and types
//Pre: animalParts and numAnimals must be positive integers
//Post: returns the expected profit from a burger made with the supplied
// parameters
//NOTE: positive means > 0, non-negative means >= 0
float maximizer(const int animalParts, const int numAnimals);
//This function formats the chart nicely.
//Pre: all inputs must be positive integers. partsUB > partsLB,
// numAnimUB > numAnimLB
//Post: Outputs a prettily formatted table to the screen
void formatChart(const int partsLB, const int partsUB,
const int numAnimLB, const int numAnimUB);
int main()
{
//variable declarations
int partsLowerBound, partsUpperBound;
int animalsLowerBound, animalsUpperBound;
//build the charts until the user says "STOP!"
greetUser();
do
{
getParameterBounds(partsLowerBound, partsUpperBound,
animalsLowerBound, animalsUpperBound);
cout << endl;
formatChart(partsLowerBound, partsUpperBound,
animalsLowerBound, animalsUpperBound);
cout << "----------" << endl;
} while (!shouldQuit());
return 0;
}
void greetUser()
{
cout << "Greetings! Welcome to the Profit Maximizerizationite 2.0!" << endl;
cout << "Where you learn just how cheap you really are!" << endl << endl;
return;
}
int getLowBoundedNumber(const int lowBound)
{
int input;
do
{
cout << "Please enter a number greater than " << lowBound << ": ";
cin >> input;
if (input <= lowBound)
cout << input << " is not greater than " << lowBound << ". Try again!\n";
} while(input <= lowBound);
return input;
}
void getParameterBounds(int & numPartsLB, int & numPartsUB,
int & numAnimalsLB, int & numAnimalsUB)
{
cout << "Bounds for number of animal parts" << endl;
numPartsLB = getLowBoundedNumber(0);
numPartsUB = getLowBoundedNumber(numPartsLB);
cout << "Bounds for number of types of animals" << endl;
numAnimalsLB = getLowBoundedNumber(0);
numAnimalsUB = getLowBoundedNumber(numAnimalsLB);
return;
}
bool shouldQuit()
{
char again;
cout << "Run again? (y/n): ";
do
{
cin >> again;
//Check that we either get a 'y', 'Y', 'n', 'N'
if (again != 'y' && again != 'Y' && again != 'n' && again != 'N')
cout << "Invalid response. (y/n) ";
} while (again != 'y' && again != 'Y' && again != 'n' && again != 'N');
//If they return a 'n' or 'N', we will want to quit. Thus, we return true
//if again is a 'y' or 'Y', we run again, so we return false
return (again == 'n' || again == 'N');
}
float maximizer(const int animalParts, const int numAnimals)
{
//function given in assignment. uses exp and pow from cmath
return animalParts * exp(-1*pow(numAnimals, 2.0) /
static_cast<float>(animalParts));
}
void formatChart(const int partsLB, const int partsUB,
const int numAnimLB, const int numAnimUB)
{
//format the output to ONLY 2 decimal places
//This is monies after all...
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Just like the ascii art!
for (int i = partsLB; i <= partsUB; i++)
{
for (int j = numAnimLB; j <= numAnimUB; j++)
cout << maximizer(i,j) << " ";
cout << endl;
}
cout << endl;
return;
}