The task is simple: Find the number of days between two dates. Provide account leap years.
To practice the two options:
- One of the boundary dates described only a year. That is, the start date is entered in full (for example 25.12.2015), and the second only a year (for example 2016). It counts the days before 01.01.2016
- Both the date of full – describes day month year.
Here is an example of a, partially crucial first simplified version:
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 | #include <iostream> using namespace std; int DaysCount(int yearBegin, int monthBegin, int dayBegin, int yearCurrent){ int y, m, e, d, a, b, c, res = 0; a = yearCurrent - yearBegin; // Разница в годах b = a / 4; // Кол-во високосных c = a - b; // Кол-во невысокосных d = b * 366 + c * 365; // Предварительное кол-во дней до начала года d -= (yearCurrent % 4) ? 1 : 0; // +1 день, если год високосный // Считаем месяцы for (e = 1; e < monthBegin; e++) { // Если февраль и високосный: if (e == 2) d -= (yearCurrent % 4) ? 28 : 29; // Если по 30 дней else if (e == 4 || e == 6 || e == 9 || e == 11) d -= 30; else // Если по 31 день в месяце d -= 31; }; /* Для корректировки, если расчет идет от последнего месяца года до задаваемого граничного года можно использовать примерно такое условие, дабы не прибавлять, а вычитать количество дней */ if (monthBegin != 12 && a == 1) return dayBegin + d; else return d - dayBegin; } int main() { cout << DaysCount(2015, 12, 15, 2016) << endl; } |
Here the function DaysCount() It receives in the first three parameters of the start date (year, month, day) and the last parameter boundary (year, 1-th day of January,)
Solve any possible way, even though the cycle with something else. But the decision of the second embodiment does not show :) Let it be homework. Good luck!
Questions to ask in the comments