#include //for cout #include //for the functions time and localtime #include "date.h" using namespace std; const int date::length[] { 0, //dummy, so that January will have subscript 1 31, //January 28, //February. Pretend there are no leap years. 31, //March 30, //April 31, //May 30, //June 31, //July 31, //August 30, //September 31, //October 30, //November 31 //December }; date::date(int m, int d, int y) : day(365 * y + d - 1) { for (int month {1}; month < m; ++month) { day += length[month]; } } date::date() //today's date { const time_t t {time(nullptr)}; const tm *const p = localtime(&t); day = 365 * (p->tm_year + 1900) + p->tm_mday - 1; for (int month {1}; month < p->tm_mon + 1; ++month) { day += length[month]; } } ostream& operator<<(ostream& ost, const date& d) { const int year {d.day / 365}; int dayOfYear {d.day % 365 + 1}; //1 to 365 inclusive int month {1}; for (; dayOfYear > date::length[month]; ++month) { dayOfYear -= date::length[month]; } return ost << month << "/" << dayOfYear << "/" << year; } istream& operator>>(istream& ist, date& d) { int month {0}; ist >> month; if (!ist) { //If the attempted input was not successful, return ist; } char c {'\0'}; ist >> c; if (!ist) { return ist; } if (c != '/') { ist.setstate(ios_base::failbit); //Make ist unhealthy. return ist; } int day; ist >> day; if (!ist) { return ist; } ist >> c; if (!ist) { return ist; } if (c != '/') { ist.setstate(ios_base::failbit); return ist; } int year; ist >> year; if (!ist) { return ist; } if (month < 1 || month > 12 || day < 1 || day > date::length[month]) { ist.setstate(ios_base::failbit); return ist; } //Now that we have valid data, put it into the data member. d.day = 365 * year + day - 1; for (int m {1}; m < month; ++m) { d.day += date::length[m]; } return ist; }