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
|
#include <iostream>
bool is_leap_year(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
int days_in_month(int year, int month) {
static int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if (month == 2 && is_leap_year(year))
return 29;
return days[month];
}
struct Date {
int year, month, day;
Date(int y, int m, int d) : year(y), month(m), day(d) {}
bool equal(Date d) const {
return d.year == year && d.month == month && d.day == day;
}
void inc() {
if (++day > days_in_month(year, month)) {
day = 1;
if (++month > 12) {
month = 1;
++year;
}
}
}
};
std::ostream& operator<<(std::ostream& out, const Date& d) {
return out << d.month << '/' << d.day << '/' << d.year;
}
void print_dates(Date from, Date to) {
std::cout << from << '\n';
do {
from.inc();
std::cout << from << '\n';
} while (!from.equal(to));
}
int main() {
print_dates(Date(2019,1,17), Date(2020, 1, 17));
}
| |