1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <ctime>
#include <iostream>
#include <random>
#include <string>
using namespace std;
class Fish {
private:
//You can ignore this line if you're not using C++11
// and replace the following refernces to prng with "rand()"
// found in the header <cstdlib>.
static default_random_engine prng; //Pseudo-Random Number Generator (C++11)
double weight; //weight of this fish, as a double
string species; //species of this fish, stored as a string
//Note: the constructor is private, as we don't need main() crateing fish this way.
Fish(double w, string s) : weight(w), species(s) {
}
public:
//Use to construct a fish of a random species, with random weight.
//Syntax in main(): Fish::catchFish()
static Fish catchFish() {
string speciesArray[] = {"Tilapia", "Catfish", "Goldfish"}; //array of possible species
string species = speciesArray[ prng() % 3 ]; //Pick a random element from ^this^ array.
//Real fishs' weights are (likely) normally distributed. Ignore this and use prng
// (like done above for the species), if you don't care about real life distributions.
// In that case replce the next two lines with the following line:
// weight = 10 + (prng() % 21);
normal_distribution<double> guass( 20.0, 3.0 );
double weight = guass(prng);
//Construct this fish using the private constructor, and return it.
return Fish(weight, species);
}
//calculate the price of this fish
double calculatePrice() const {
double price = 1000 + weight * 200; //calculate "raw" price (using the requested formula)
price = (int)(price * 100 + 0.5) / 100.0; //round price to the nearest cent
return price;
}
//accessor methods
double weigh() const {
return weight;
}
string identify() const {
return species;
}
};
//Define static Fish member variable: prng
// by seeding it with the current system time. (C++11)
// If you're not using C++11 and you replaced prng with rand()
// then ignore this line and replace it with a single call to
// srand( time(NULL) );
// in main(), before your first call to catchFish().
default_random_engine Fish::prng( time(NULL) );
//--------------------------------------------------------------------------------------------------
int main() {
//example: catch one fish and display its characteristics
Fish fishy = Fish::catchFish();
cout << "Species: " << fishy.identify() << '\n';
cout << "Weight: " << fishy.weigh() << " lbs\n";
cout << "Price: $" << fishy.calculatePrice() << '\n';
//example: catch 100,000 fish, and count how many were heavier then 30 lbs
// and lighter then 10 lbs
unsigned int N = 100000;
unsigned int heavyCount = 0;
unsigned int lightCount = 0;
for( unsigned int i = 0; i < N; i++ ) {
Fish f = Fish::catchFish();
if( f.weigh() > 30.0 )
heavyCount++;
else if( f.weigh() < 10.0 )
lightCount++;
}
cout << "\n[Out of " << N << "]\n";
cout << "Heavy Fish: " << heavyCount << " (" << (100.0 * heavyCount / N) << "%)\n";
cout << "Light Fish: " << lightCount << " (" << (100.0 * lightCount / N) << "%)\n";
return 0;
}
| |