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
|
#include <iostream>
#include <cmath>
using namespace std;
class date
{
private:
int m {};
int d {};
int y {};
int days {};
int countLeapYears() const;
public:
date(int, int, int);
int getdays() const;
int operator-(const date&) const;
};
int main()
{
int day {}, month {}, year {};
char c {};
cout << "Enter a start date (m/d/y): ";
cin >> month >> c >> day >> c >> year;
const date start(month, day, year);
cout << "Enter an end date (m/d/y): ";
cin >> month >> c >> day >> c >> year;
const date end(month, day, year);
const int duration {end - start};
cout << "The number of days between those two dates are: " << duration << '\n';
}
date::date(int a, int b, int c) : m(a), d(b), y(c) {
static const int monthDays[13] {0, 31, 28, 31, 30, 31, 30, 31, 31, 31, 31, 30, 31};
days = {y * 365 + d};
for (int i = 0; i < m - 1; ++i)
days += monthDays[i];
days += countLeapYears();
}
int date::countLeapYears() const
{
int years {y};
if (m <= 2)
--years;
return years / 4 - years / 100 + years / 400;
}
int date::getdays() const
{
return days;
}
int date::operator-(const date& d) const {
return days - d.getdays();
}
|