// Assignment 8 Solution - Fall 2020 #ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS #endif #include #include #include #include #include #include using namespace std; class Date { unsigned month, day, year; public: Date(); Date(unsigned, unsigned, unsigned); Date& operator++(); bool operator>(const Date&) const; bool operator==(const Date&) const; // void print() const; void letTimePass(); static const double DaysPerYear; static unsigned DaysPerMonth[13]; static Date getBirthday(); friend ostream& operator<<(ostream& out, const Date& date); }; 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 operator!() { isAlive = false; } }; class Population { Human** people; static const int OriginalPopulationSize; public: Population(); ~Population(); Human** getPeople() const { return people; } void examinePopulation(ostream&); int getNumberLiving() const; static int getOriginalPopulationSize() { return OriginalPopulationSize; } }; ostream& operator<<(ostream& out, const Population& pop); 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() { ofstream fout("c:/temp/ass8output.txt"); ostream* po; if (!fout) { cerr << "Cannot open c:/temp/ass8output.txt" << endl; exit(1); } po = &cout; Population World; (*po) << World; // let time pass until half of the world's Population dies do { TODAY.letTimePass(); World.examinePopulation(*po); } while (World.getNumberLiving() > Population::getOriginalPopulationSize() / 2); *po << World; } // 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; } Date& Date::operator++() { 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++; } return *this; } bool Date::operator>(const Date& d) const { return 10000*year+100*month+day>10000*d.year+100*d.month+d.day; } bool Date::operator==(const Date& d) const { return 10000*year+100*month+day==10000*d.year+100*d.month+d.day; } ostream& operator<<(ostream& out, const Date& date) { out << setfill('0'); out << setw(2) << date.month << '/' << setw(2) << date.day << '/' << setw(2) << date.year; out << setfill(' '); return out; } void Date::letTimePass() { int random = rand() % 365+1; int i; for (i = 0; i < random; i++) ++(*this); } // 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 > d2) { do { days++; ++d2; } while (!(d1== d2)); return days; } else { do { days++; ++d1; } while (!(d2==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); } ostream& operator<<(ostream& out, const Human& obj) { out << obj.get_name() << " was born on "; out << obj.get_birthday(); // birthday.print(); out << " is " << obj.age() << endl; return out; } 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; } ostream& operator<<(ostream& out, const Population& pop) { out << "================================================\n"; out << "Today is "; out << TODAY; out << endl; for (short i = 0; i< Population::getOriginalPopulationSize(); ++i) { if (pop.getPeople()[i] -> is_alive()) out << *(pop.getPeople()[i]); } out << "================================================\n"; return out; } void Population::examinePopulation(ostream& out) { 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(); !(*people[i]); out << TODAY; out << " " << 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; } return temp_birthday; }