You need to calculate the date that will come in M days c++
Completed the task for the next day's output. You need to calculate the date that will come in M days. With this problem, please help me with an explanation, if not difficult
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
int day, month, year, last;
cout << "Введите день, месяц, год: ";
cin >> day >> month >> year;
cout << "Ваша дата: " << day << "." << month << "." << year << endl;
last = 0;
if (month == 2)
{
if ((year % 4) != 0 && day == 28)
{
last = 1;
}
if ((year % 4) == 0 && day == 29)
{
last = 1;
}
}
else if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 30)
{
month++;
day = 0;
cout << "Последний день месяца!";
}
else if (day == 31) {
last = 1;
}
if (last == 1) {
cout << "Последний день месяца!";
day = 1;
if (month == 12) {
month = 1;
year++;
cout << "C наступающим!";
}
else
month++;
}
else
day++;
cout << "Завтра: " << day << "." << month << "." << year << endl;
getch();
} ```
0
1 answers
To your same question, @Harry gave advice to count through the Julian date.
Taken from this book and slightly adapted:
long JDay(int year, int month, int day) {
if (month <= 2) {
year--;
month += 12;
};
unsigned long J;
int A = year / 100;
A = 2 - A + (A / 4);
J = 1461L * long(year);
J /= 4L;
unsigned long K = 306001L * long(month + 1);
K /= 10000L;
J += K + day + 1720995L + A;
return J;
}
void GDate(long JD, int& y, int& m, int& d) {
unsigned long A = (JD * 4L - 7468865L) / 146097L;
A = (JD > 2299160) ? JD + 1 + A - (A / 4L) : JD;
long B = A + 1524;
long C = (B * 20L - 2442L) / 7305L;
long D = (C * 1461L) / 4L;
long E = (10000L * (B - D)) / 306001L;
d = int(B - D - ((E * 306001L) / 10000L));
m = int((E <= 13) ? E - 1 : E - 13);
y = int(C - ((m > 2) ? 4716 : 4715));
}
int main() {
int day, month, year;
cout << "Введите день, месяц, год: ";
cin >> day >> month >> year;
cout
<< setfill('0') << "Ваша дата: " << setw(2) << day << "."
<< setw(2) << month << "." << year << endl;
long jd = JDay(year, month, day);
for (int m = 1; m <= 1000; m *= 10) {
cout << "Дата через " << m << " дней: ";
GDate(jd + m, year, month, day);
cout << setw(2) << day << "." << setw(2) << month << "." << year <<
endl;
}
}
Here is an example: https://ideone.com/AWUOW2
You can even go back to 1582, if only the Gregorian calendar.
0
Author: Mikhailo, 2020-04-04 11:46:29