// Assignment 8 Solution - Fall 2020 #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; class Date { unsigned month, day, year; public: Date(); Date(unsigned, unsigned, unsigned); void increment(); bool isGreaterThan(const Date&) const; bool isEqualTo(const Date&) const; void print() const; void letTimePass(); static const double DaysPerYear; static unsigned DaysPerMonth[13]; static Date getBirthday(); }; const double Date::DaysPerYear = 365.25; unsigned Date::DaysPerMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; Date getBirthday(); class Human { string name; Date birthday; bool isAlive; public: Human(const string& n, const Date& b); string get_name() const { return name; } const Date& get_birthday() const { return birthday; } bool is_alive() const { return isAlive; } unsigned short age() const; void die() { isAlive = false; } void print() const; }; class Population { Human** people; void determine_oldest() const; static const int OriginalPopulationSize; public: Population(); ~Population(); void print() const; void examinePopulation(); int getNumberLiving() const; static int getOriginalPopulationSize() { return OriginalPopulationSize; } }; const int Population::OriginalPopulationSize = 54; float rollTheDice(unsigned short age); Date TODAY; const string NAMES[] = { "Fadak","Adis","Kia","Adam","Ran","Jason","Brian","Hin Hang", "Swati","Alaxendar","Robert","Samar","Thi Thu Vy","Salomon","Marian","Nicolas", "Brian Ha","Wanjia","Dolly","Trien Bang","Suraj","Bereket","Ben", "Victor","Xinru","Zheng","Ken","Jason Kai","Ryan","Seyedamirhossei","Arrzu", "Nimra","Brian Ndiaye","Le Ngoc","Pham Bao Bao","Xuan Yen Tram","Kuangzhong","Joseph", "Het","Isha","Ahmad","Mehui","Tarandeep","Minrou","Lauren","Trang","Victoria", "An","Vinh","William","Benjamin","Hsin-Chieh","Jiasheng","Siyun" }; int main() { Population World; World.print(); // let time pass until half of the world's Population dies do { TODAY.letTimePass(); World.examinePopulation(); } while (World.getNumberLiving() > Population::getOriginalPopulationSize() / 2); World.print(); } // Creates today's date Date::Date() : month(0), day(0), year(0) { time_t now = time(NULL); tm* ptr = localtime(&now); month = ptr->tm_mon + 1; // note ptr->tm mon is current month% -1 day = ptr->tm_mday; year = ptr->tm_year + 1900; } Date::Date(unsigned m, unsigned d, unsigned y) : month(m), day(d),year(y) { if (y < 100) year += (year < 21) ? 2000 : 1900; } void Date::increment(void) { day++; // Leap year correction if (year%4 == 0) DaysPerMonth[2] = 29; else if (year%400 == 0) DaysPerMonth[2] = 29; else if (year%100 == 0) DaysPerMonth[2] = 28; else DaysPerMonth[2] = 28; if (day > DaysPerMonth[month]) { day = 1; month++; } if (month > 12) { month = 1; year++; } } bool Date::isGreaterThan(const Date& d) const { return 10000*year+100*month+day>10000*d.year+100*d.month+d.day; } bool Date::isEqualTo(const Date& d) const { return 10000*year+100*month+day==10000*d.year+100*d.month+d.day; } void Date::print() const { cout << setfill('0'); cout << setw(2) << month << '/' << setw(2) << day << '/' << setw(2) << year; cout << setfill(' '); } void Date::letTimePass() { int random = rand() % 365+1; int i; for (i = 0; i < random; i++) increment(); } // returns the number of days between d1 and d2 (same as d1 - d2) int difference_between_2_Dates(Date d1,Date d2) { int days = 0; if (d1.isGreaterThan(d2)) { do { days++; d2.increment(); } while (!d1.isEqualTo(d2)); return days; } else { do { days++; d1.increment(); } while (!d2.isEqualTo(d1)); return -days; } } Human::Human(const string& n,const Date& b) : name(n), birthday(b), isAlive(true) { } unsigned short Human::age() const { return static_cast (difference_between_2_Dates(TODAY,birthday)/Date::DaysPerYear); } void Human::print() const { cout << name << " was born on "; birthday.print(); cout << " is " << age() << endl; } Population::Population() : people(new Human*[OriginalPopulationSize]) { for (int i = 0; i < OriginalPopulationSize; ++i) { people[i] = new Human(NAMES[i],Date::getBirthday()); } } Population::~Population() { for (int i = 0; i < OriginalPopulationSize; ++i) delete people[i]; delete [] people; people = nullptr; } void Population::print() const { cout << "================================================\n"; cout << "Today is "; TODAY.print(); cout << endl; for (short i = 0; i< OriginalPopulationSize; ++i) { if (people[i] -> is_alive()) people[i] -> print(); } cout << "================================================\n"; } void Population::examinePopulation() { short i; for (i = 0; i < OriginalPopulationSize; i++) { // who lives and who dies? if (people[i]->is_alive()) { if (rollTheDice(people[i]->age())>.67) { people[i] ->die(); TODAY.print(); cout << " " << people[i]->get_name() << " died at the age of " << people[i] -> age() << endl; } } } } int Population::getNumberLiving() const { int count = 0; for (short i = 0; i < OriginalPopulationSize; i++) { if (people[i]->is_alive()) count++; } return count; } float rollTheDice(unsigned short age) { return static_cast(age)* (rand() % 100) / 10000.f; } Date Date::getBirthday() { const int number_of_days_in_100_years = 35625; int number_of_days_after_birthdate; Date temp_birthday(1,1,1920); // generate a random number between 0 and 36524 number_of_days_after_birthdate = rand() % number_of_days_in_100_years; // increment the temp_birthday a random number of days for (int j = 0; j < number_of_days_after_birthdate; ++j) { temp_birthday.increment(); } return temp_birthday; }