You need to write a program with a function that will calculate the day of the week by date. Dates are represented by a structure (year, month, day). In this program, I do not really understand the function int date (Date z), namely from 20 to 24 lines. I want to understand this function. Please explain these lines.

#include <iostream> #include <conio.h> #include <string> using namespace std; struct Date { int day; int month; int year; }; int date(Date z) { int a; int y; int m; int R; a = (14 - z.month) / 12; y = z.year - a; m = z.month + 12 * a - 2; R = 7000 + (z.day + y + y / 4 - y / 100 + y / 400 + (31 * m) / 12); return R % 7; } int main() { setlocale(LC_ALL, "Russian"); Date z; char C; string S[7] = { "Воскресенье", "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота" }; cin >> z.day >> z.month >> z.year; cout << S[date(z)] << endl; _getch(); return 0; } 
  • one
    Dig in the Internet / literature, as calculated Julian date ... - Harry
  • Thank you very much! - EgaNator

2 answers 2

I tried to explain the principle of this code in simple words, here:

 #include <iostream> #include <string> using namespace std; // Создает структуру "Date" с внутренними переменными (день, месяц, год). struct Date{ int day; int month; int year; }; int date(Date z){ // Создает переменные. int a; int y; int m; int R; // Вычисляет непонятный для меня номер (от 14 отнимает месяц и разделяет на 12). a = (14 - z.month) / 12; // От года отнимает номер. y = z.year - a; // Вычисляет код года (к месяцу прибовляется (12 умноженное на номер), и отминусовуется 2). m = z.month + 12 * a - 2; // Вычисляет число (к 7000 прибавляется (математическое уравнение вычисления дня недели)). R = 7000 + (z.day + y + y / 4 - y / 100 + y / 400 + (31 * m) / 12); // Возвращает номер дня недели (седьмой процент числа). return R % 7; } int main(){ // Устанавливает язык локализации "Русский". setlocale(LC_ALL, "Russian"); // Создает переменную даты (используя структуру "Date"). Date z; // Создает список дней недели. string S[7] = { "Воскресенье", "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота" }; // Принимает (день, месяц, год). cin >> z.day >> z.month >> z.year; // Отсылает в функцию "date" внесенную дату, и принимая номер дня ворачивает название. cout << S[date(z)] << endl; return 0; } 

I have _getch(); the function does not work, and I still do not understand why it is, so I removed it.

I here offer a more optimized version for the computer: add the library library #include <ctime> :

 #include <iostream> #include <string> #include <ctime> using namespace std; // Создает структуру "date" с внутренними переменными (день, месяц, год). struct date{ int day, month, year; }; int get_weekday(date i){ // Заносит дату в переменную. std::tm time_in = { 0, 0, 0, i.day+1, i.month+1, i.year }; // Конвертирует дату в time_t. std::time_t time_temp = std::mktime(&time_in); // Вносит time_t дату в переменную time_out. const std::tm * time_out = std::localtime(&time_temp); // Возрващаем из time_out день недели. return (int)time_out->tm_wday; } int main(){ // Устанавливает язык локализации "Русский". setlocale(LC_ALL, "Russian"); // Создает переменную даты (используя структуру "date"). date z; // Создает список дней недели. string S[7] = { "Воскресенье", "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота" }; // Принимает (день, месяц, год). cin >> z.day >> z.month >> z.year; // Отсылает в функцию "get_weekday" внесенную дату, и принимая номер дня ворачивает название. cout << S[get_weekday(z)] << endl; return 0; } 
  • Вычисляет високосный год или нет (от 14 отнимает месяц и делит на 12) How to understand a leap year using this formula or not? Judging by the link to the wiki about the Julian calendar, it's just a coeff. - froxxendsg pm
  • Oh, I was wrong, he does not check leap year or not. I fixed everything. - GUIMish

In this function, it is calculated whether a high year is a high year or not, since every 14 years, the day is added in February, that is, 29 days, and in non-high years, it will be 28 days. And it returns, the remainder of the division in this function, which will correspond to the day of the week. I will also answer for the previous person, the '_getch ()' function expects further actions from the user so that the console does not close after performing its function.