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
I barely pouserdstvoval and wrote everything with protections, and other input… Could you evaluate any comments, and my solution to this problem:
#include “stdafx.h”
#include
#include
int main()
{
setlocale (LC_ALL,”rus”);
using std::cout;
using std::cin;
Double daus_date = 0;
double month_date = 0;
double year_date = 0;
long long daus_date_int = 0;
long long month_date_int = 0;
long long year_date_int = 0;
//——————————————————————————————————————————-
//The first date
//——————————————————————————————————————————-
bool choice = false;
while (choice == false)
{
cout <> year_date;
(long long) year_date_int = year_date;
if (year_date_int >= 0)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
choice = false;
}
}
system("cls");
choice = false;
while (choice == false)
{
cout <> month_date;
(long long) month_date_int = month_date;
if (month_date_int == 1 || month_date_int == 2 || month_date_int == 3 || month_date_int == 4 || month_date_int == 5 || month_date_int == 6 || month_date_int == 7 || month_date_int == 8 || month_date_int == 9|| month_date_int == 10 || month_date_int == 11 || month_date_int == 12)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
choice = false;
}
}
system("cls");
choice = false;
while (choice == false)
{
cout <> daus_date;
(long long) daus_date_int = daus_date;
switch (month_date_int)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
{
if (daus_date_int >= 1 && daus_date_int <= 31)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
switch (month_date_int)
{
case 1:
cout << "В январе 31 day!\n\n";
break;
case 3:
cout << "В марте 31 day!\n\n";
break;
case 5:
cout << "В мае 31 day!\n\n";
break;
case 7:
cout << "В июле 31 day!\n\n";
break;
case 8:
cout << "В августе 31 day!\n\n";
break;
case 10:
cout << "В октябре 31 day!\n\n";
break;
case 12:
cout <= 1 && daus_date_int <= 30)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
switch (month_date_int)
{
case 4:
cout << "В апреле 30 days!\n\n";
break;
case 6:
cout << "В июне 30 days!\n\n";
break;
case 9:
cout << "В сентябре 30 days!\n\n";
break;
case 11:
cout << "В ноябре 30 days!\n\n";
break;
}
choice = false;
}
break;
}
case 2:
{
if (year_date_int % 4 == 0)
{
if (daus_date_int <= 29)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
cout << "В високосном году, in February 29 дней\n\n";
choice = false;
}
}
else
{
if (daus_date_int <= 28)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
cout << "В феврале 28 дней\n\n";
choice = false;
}
}
break;
}
}
}
system("cls");
long long one_daus_date = 0;
long long one_month_date = 0;
long long one_year_date = 0;
one_daus_date = daus_date_int;
one_month_date = month_date_int;
one_year_date = year_date_int;
//——————————————————————————————————————————-
//The second date
//——————————————————————————————————————————-
choice = false;
while (choice == false)
{
cout <> year_date;
(long long) year_date_int = year_date;
if (year_date_int >= 0)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
choice = false;
}
}
system("cls");
choice = false;
while (choice == false)
{
cout <> month_date;
(long long) month_date_int = month_date;
if (month_date_int == 1 || month_date_int == 2 || month_date_int == 3 || month_date_int == 4 || month_date_int == 5 || month_date_int == 6 || month_date_int == 7 || month_date_int == 8 || month_date_int == 9|| month_date_int == 10 || month_date_int == 11 || month_date_int == 12)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
choice = false;
}
}
system("cls");
choice = false;
while (choice == false)
{
cout <> daus_date;
(long long) daus_date_int = daus_date;
switch (month_date_int)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
{
if (daus_date_int >= 1 && daus_date_int <= 31)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
switch (month_date_int)
{
case 1:
cout << "В январе 31 day!\n\n";
break;
case 3:
cout << "В марте 31 day!\n\n";
break;
case 5:
cout << "В мае 31 day!\n\n";
break;
case 7:
cout << "В июле 31 day!\n\n";
break;
case 8:
cout << "В августе 31 day!\n\n";
break;
case 10:
cout << "В октябре 31 day!\n\n";
break;
case 12:
cout <= 1 && daus_date_int <= 30)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
switch (month_date_int)
{
case 4:
cout << "В апреле 30 days!\n\n";
break;
case 6:
cout << "В июне 30 days!\n\n";
break;
case 9:
cout << "В сентябре 30 days!\n\n";
break;
case 11:
cout << "В ноябре 30 days!\n\n";
break;
}
choice = false;
}
break;
}
case 2:
{
if (year_date_int % 4 == 0)
{
if (daus_date_int <= 29)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
cout << "В високосном году, in February 29 дней\n\n";
choice = false;
}
}
else
{
if (daus_date_int <= 28)
choice = true;
else
{
cout << "\nВы ввели недопустивое значение!\n";
cout << "В феврале 28 дней\n\n";
choice = false;
}
}
break;
}
}
}
system("cls");
long long two_daus_date = 0;
long long two_month_date = 0;
long long two_year_date = 0;
two_daus_date = daus_date_int;
two_month_date = month_date_int;
two_year_date = year_date_int;
//——————————————————————————————————————————-
//Score in the first days of the date of
//——————————————————————————————————————————-
double counter_daus_one_date = 0;
for (long long counter = 0; counter < one_year_date; counter++)
{
if (counter % 4 == 0 && counter != 0)
counter_daus_one_date += 366;
else
counter_daus_one_date += 365;
}
for (short counter = 0; counter <= one_month_date; counter++)
{
switch (counter)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
counter_daus_one_date += 31;
break;
case 4:case 6 :case 9: case 11:
counter_daus_one_date += 30;
break;
case 2:
if (one_year_date % 4 == 0)
counter_daus_one_date += 29;
else
counter_daus_one_date += 28;
break;
}
}
counter_daus_one_date += one_daus_date;
//——————————————————————————————————————————-
//Score days for the second date
//——————————————————————————————————————————-
double counter_daus_two_date = 0;
for (long long counter = 0; counter < two_year_date; counter++)
{
if (counter % 4 == 0 && counter != 0)
counter_daus_two_date += 366;
else
counter_daus_two_date += 365;
}
for (short counter = 0; counter <= two_year_date; counter++)
{
switch (counter)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
counter_daus_two_date += 31;
break;
case 4:case 6 :case 9: case 11:
counter_daus_two_date += 30;
break;
case 2:
if (two_year_date % 4 == 0)
counter_daus_two_date += 29;
else
counter_daus_two_date += 28;
break;
}
}
counter_daus_two_date += two_daus_date;
//——————————————————————————————————————————-
//Account number of days between dates
//——————————————————————————————————————————-
cout << "Первая дата: ";
if (one_daus_date < 10)
cout << '0' << one_daus_date << '.';
else
cout << one_daus_date << '.';
if (one_month_date < 10)
cout << '0' << one_month_date << '.';
else
cout << one_month_date << '.';
cout << one_year_date << '\n';
cout << "Вторая дата: ";
if (two_daus_date < 10)
cout << '0' << two_daus_date << '.';
else
cout << two_daus_date << '.';
if (two_month_date < 10)
cout << '0' << two_month_date << '.';
else
cout << two_month_date << '.';
cout << two_year_date << '\n';
if (counter_daus_two_date < counter_daus_one_date)
cout << "Количество дней между датами: " << counter_daus_one_date – counter_daus_two_date < counter_daus_one_date)
cout << "Количество дней между датами: " << counter_daus_two_date – counter_daus_one_date << '\n';
if (counter_daus_two_date == counter_daus_one_date)
cout << "Вы ввели две одинаковые даты!" << '\n';
_getch();
return 0;
}
excuse me, I do not know how this code issue in the style of C ++ (both decorated in a frame your decision).
Necessary to frame your code tegami code:
[code]
… your code here …
[/code]
Only instead of square brackets need to write a corner (insignia “less” and “more”).
And like this (without formatting) recorded by your example one, naturally, even watch will not…
With the same success all your code can be written in a single line at all (you can check, that while it is still up and running).
Here is a cultural version of my code:
#include "stdafx.h"
#include
#include
int main()
{
setlocale (LC_ALL,"rus");
using std::cout;
using std::cin;
double daus_date = 0;
double month_date = 0;
double year_date = 0;
long long daus_date_int = 0;
long long month_date_int = 0;
long long year_date_int = 0;
//-------------------------------------------------------------------------------------------------------------------------------
//Первая дата
//-------------------------------------------------------------------------------------------------------------------------------
bool choice = false;
while (choice == false)
{
cout <> year_date;
(long long) year_date_int = year_date;
if (year_date_int >= 0)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
choice = false;
}
}
system("cls");
choice = false;
while (choice == false)
{
cout <> month_date;
(long long) month_date_int = month_date;
if (month_date_int == 1 || month_date_int == 2 || month_date_int == 3 || month_date_int == 4 || month_date_int == 5 || month_date_int == 6 || month_date_int == 7 || month_date_int == 8 || month_date_int == 9|| month_date_int == 10 || month_date_int == 11 || month_date_int == 12)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
choice = false;
}
}
system("cls");
choice = false;
while (choice == false)
{
cout <> daus_date;
(long long) daus_date_int = daus_date;
switch (month_date_int)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
{
if (daus_date_int >= 1 && daus_date_int <= 31)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
switch (month_date_int)
{
case 1:
cout << "В январе 31 день!\n\n";
break;
case 3:
cout << "В марте 31 день!\n\n";
break;
case 5:
cout << "В мае 31 день!\n\n";
break;
case 7:
cout << "В июле 31 день!\n\n";
break;
case 8:
cout << "В августе 31 день!\n\n";
break;
case 10:
cout << "В октябре 31 день!\n\n";
break;
case 12:
cout <= 1 && daus_date_int <= 30)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
switch (month_date_int)
{
case 4:
cout << "В апреле 30 дней!\n\n";
break;
case 6:
cout << "В июне 30 дней!\n\n";
break;
case 9:
cout << "В сентябре 30 дней!\n\n";
break;
case 11:
cout << "В ноябре 30 дней!\n\n";
break;
}
choice = false;
}
break;
}
case 2:
{
if (year_date_int % 4 == 0)
{
if (daus_date_int <= 29)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
cout << "В високосном году, в феврале 29 дней\n\n";
choice = false;
}
}
else
{
if (daus_date_int <= 28)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
cout << "В феврале 28 дней\n\n";
choice = false;
}
}
break;
}
}
}
system("cls");
long long one_daus_date = 0;
long long one_month_date = 0;
long long one_year_date = 0;
one_daus_date = daus_date_int;
one_month_date = month_date_int;
one_year_date = year_date_int;
//-------------------------------------------------------------------------------------------------------------------------------
//Вторая дата
//-------------------------------------------------------------------------------------------------------------------------------
choice = false;
while (choice == false)
{
cout <> year_date;
(long long) year_date_int = year_date;
if (year_date_int >= 0)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
choice = false;
}
}
system("cls");
choice = false;
while (choice == false)
{
cout <> month_date;
(long long) month_date_int = month_date;
if (month_date_int == 1 || month_date_int == 2 || month_date_int == 3 || month_date_int == 4 || month_date_int == 5 || month_date_int == 6 || month_date_int == 7 || month_date_int == 8 || month_date_int == 9|| month_date_int == 10 || month_date_int == 11 || month_date_int == 12)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
choice = false;
}
}
system("cls");
choice = false;
while (choice == false)
{
cout <> daus_date;
(long long) daus_date_int = daus_date;
switch (month_date_int)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
{
if (daus_date_int >= 1 && daus_date_int <= 31)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
switch (month_date_int)
{
case 1:
cout << "В январе 31 день!\n\n";
break;
case 3:
cout << "В марте 31 день!\n\n";
break;
case 5:
cout << "В мае 31 день!\n\n";
break;
case 7:
cout << "В июле 31 день!\n\n";
break;
case 8:
cout << "В августе 31 день!\n\n";
break;
case 10:
cout << "В октябре 31 день!\n\n";
break;
case 12:
cout <= 1 && daus_date_int <= 30)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
switch (month_date_int)
{
case 4:
cout << "В апреле 30 дней!\n\n";
break;
case 6:
cout << "В июне 30 дней!\n\n";
break;
case 9:
cout << "В сентябре 30 дней!\n\n";
break;
case 11:
cout << "В ноябре 30 дней!\n\n";
break;
}
choice = false;
}
break;
}
case 2:
{
if (year_date_int % 4 == 0)
{
if (daus_date_int <= 29)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
cout << "В високосном году, в феврале 29 дней\n\n";
choice = false;
}
}
else
{
if (daus_date_int <= 28)
choice = true;
else
{
cout << "\nВы ввели недопустимое значение!\n";
cout << "В феврале 28 дней\n\n";
choice = false;
}
}
break;
}
}
}
system("cls");
long long two_daus_date = 0;
long long two_month_date = 0;
long long two_year_date = 0;
two_daus_date = daus_date_int;
two_month_date = month_date_int;
two_year_date = year_date_int;
//-------------------------------------------------------------------------------------------------------------------------------
//Счёт дней в первой дате
//-------------------------------------------------------------------------------------------------------------------------------
double counter_daus_one_date = 0;
for (long long counter = 0; counter < one_year_date; ++counter)
{
if (counter % 4 == 0 && counter != 0)
counter_daus_one_date += 366;
else
counter_daus_one_date += 365;
}
for (short counter = 0; counter < one_month_date; ++counter)
{
switch (counter)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
counter_daus_one_date += 31;
break;
case 4:case 6 :case 9: case 11:
counter_daus_one_date += 30;
break;
case 2:
if (one_year_date % 4 == 0)
counter_daus_one_date += 29;
else
counter_daus_one_date += 28;
break;
}
}
counter_daus_one_date += one_daus_date;
cout << counter_daus_one_date << " counter_one\n";
//-------------------------------------------------------------------------------------------------------------------------------
//Счёт дней во второй дате
//-------------------------------------------------------------------------------------------------------------------------------
double counter_daus_two_date = 0;
for (long long counter = 0; counter < two_year_date; ++counter)
{
if (counter % 4 == 0 && counter != 0)
counter_daus_two_date += 366;
else
counter_daus_two_date += 365;
}
for (short counter = 0; counter < two_month_date; ++counter)
{
switch (counter)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
counter_daus_two_date += 31;
break;
case 4:case 6 :case 9: case 11:
counter_daus_two_date += 30;
break;
case 2:
if (two_year_date % 4 == 0)
counter_daus_two_date += 29;
else
counter_daus_two_date += 28;
break;
}
}
counter_daus_two_date += two_daus_date;
cout << counter_daus_two_date << " counter_two\n";
//-------------------------------------------------------------------------------------------------------------------------------
//Счёт количества дней между датами
//-------------------------------------------------------------------------------------------------------------------------------
cout << "Первая дата: ";
if (one_daus_date < 10)
cout << '0' << one_daus_date << '.';
else
cout << one_daus_date << '.';
if (one_month_date < 10)
cout << '0' << one_month_date << '.';
else
cout << one_month_date << '.';
cout << one_year_date << '\n';
cout << "Вторая дата: ";
if (two_daus_date < 10)
cout << '0' << two_daus_date << '.';
else
cout << two_daus_date << '.';
if (two_month_date < 10)
cout << '0' << two_month_date << '.';
else
cout << two_month_date << '.';
cout << two_year_date << '\n';
if (counter_daus_two_date < counter_daus_one_date)
cout << "Количество дней между датами: " << counter_daus_one_date - counter_daus_two_date < counter_daus_one_date)
cout << "Количество дней между датами: " << counter_daus_two_date - counter_daus_one_date << '\n';
if (counter_daus_two_date == counter_daus_one_date)
cout << "Вы ввели две одинаковые даты!" << '\n';
_getch();
return 0;
}
#include
using namespace std;
int main()
{
int the,my,god;
int den_1,mes_1,god_1;
cout<<"vvedite pervuy datu"<<endl;
cout<<"vvedite den"<>the;
cout<<"vvedite mesyas"<>my;
cout<<"vvedite god"<>god;
cout<<"————————-"<<endl;
cout<<"vvedite tekushuy datu"<<endl;
cout<<"vvedite den"<>den_1;
cout<<"vvedite mesyas"<>mes_1;
cout<<"vvedite god"<>god_1;
double k=0;
int kol_d=0;
int kol_m=0;
int kol_g=0;
int sum_m=0;
int sum_d=0;
if(mes_1 == 1)
k=31;
else if(mes_1 == 2)
k=28;
else if (mes_1 == 3)
k=31;
else if (mes_1 == 4)
k=30;
else if (mes_1 == 5)
k=31;
else if (mes_1 == 6)
k=30;
else if (mes_1 == 7)
k=31;
else if (mes_1 == 8)
k=31;
else if (mes_1 == 9)
k=30;
else if (mes_1 == 10)
k=31;
else if (mes_1 == 11)
k=30;
else if(mes_1 == 12)
k=31;
kol_d=k-den;//We consider how many days until the completion of the previous one month the entered date
kol_m = 12-mes;//how many months before the end of the year
kol_g = god_1-(god+1);
sum_d=kol_d+den_1;
sum_m=kol_m+mes_1;
cout<<"dney"<<sum_d<<endl;
cout<<"mes"<<sum_m<<endl;
cout<<"godov"<<kol_g<<endl;
return 0;
}
#include "stdafx.h"
#include
#include
using namespace std;
int main()
{
setlocale (LC_ALL, "rus");
int day, month, year, finDay,finMonth,finYear, stData,finData, leapYear,monthDay = 0;
int monthArray[12]{ 31,28,31,30,31,30,31,31,30,31,30,31 };
while (true)
{
cout <> day >> month >> year;
if (year < 45 || monthArray[month] 12)
cout << "Ввод нверный, повторите ввод!" << endl;
else
break;
}
if (!(year % 4) && month < 3) leapYear = year / 4-1;
else leapYear = year / 4;
for (int i = 0; i < month; i++)
monthDay += monthArray[i];
stData = year * 365 + leapYear + monthDay + day;
monthDay = 0;
while (true)
{
cout <> finDay >> finMonth >> finYear;
for (int i = 0; i < finMonth; i++)
monthDay += monthArray[i];
if (!(finYear % 4) && month < 3) leapYear = finYear / 4-1;
else leapYear = finYear / 4;
finData = finYear * 365 + leapYear + monthDay + finDay;
if (finData <= stData)
cout << "Конечная дата не может быть раньше начальной\n";
else
break;
}
cout << "\n Между датами " << finData - stData << " дней\n";
_getch();
return 0;
}
Is it too zamudrili code? It can all be a lot easier to do with classes. It eventually OOP language and not some sort of BASIC.
OOP is not a panacea and, more often, not easy code, but it makes it more understandable and orderly. A much easier task can be done using the library API C or C ++ languages. But for beginners useful to show how algorithmically the calculations, you can use ready-made tools.
The prevalence of panacea, and most not easy code, but it makes it more understandable and orderly. Due to what really could be simplified code, so it is to use the API libraries C or C ++ to work with dates. But for beginners useful demonstration of computing algorithms, than using ready-made tools.
Here is my solution. Of course, there is no any protection and badly written code, tk. no normal naming and comments, but their task it copes.
Never! … Never use when working with your calendar “manual” representation of dates, calendar (blunt witted). For example the, what:
– 2016 – it is a leap year, 366 days…
– but!: 1900 (or 1800 etc.) – this is not leap years, 365 days, exceptions…
– but!: 2000 – again leap year, an exception to the previous rule…
To work with the calendar has a library : struct tm data type, time_t … mktime function(), localtime() … etc.