0
It is known that the day of the week of a date provided between 1° March 1700 and 28 February 2100 can be determined by the following method:
n=int(365.25∗g)+int(30.6∗f)−621049+d
ds=round(frac(n/7)∗7)+Δ+1
g={a−1, m≤2
{a, m>2
f={m+13, m <= 2
{m+1, m>2
Δ={2,n<36523
{1,36523≤n<73048
{0,n≥73048
The day of the week (ds) is represented by 1 if it is Sunday, 2 if it is Monday, and so on. Do a program that reads the day, month and year and provides the day of the corresponding week;
I know that I should use modf() for the frac(n/7) to obtain the fractional part. So far I found no logic, because the day of the week always ends up being 01.
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main(){
int dia;
int mes;
int ano;
int ds;
int g;
int f;
int delta;
int n;
float frac;
float intpart;
cin >> dia >> mes >> ano;
if(mes > 2){
g = ano;
f = mes + 1;
}else if(mes <= 2){
g = ano - 1;
f = mes + 13;
}
n = int(365.25 * g) + int(30.6 * f) - 621049 + dia;
cout << n << endl;
if(n < 36523){
delta = 2;
}else if( 36523 <= n and n < 73048){
delta = 1;
}else if(n >= 73048){
delta = 0;
}
cout << delta << endl;
frac = modf(n/7, &intpart);
ds = round(frac * 7) + delta + 1;
cout << ds << endl;
switch (ds){
case 1:
cout << "domingo" << endl;
break;
case 2:
cout << "segunda-feira" << endl;
break;
case 3:
cout << "terca-feira" << endl;
break;
case 4:
cout << "quarta-feira" << endl;
break;
case 5:
cout << "quinta-feira" << endl;
break;
case 6:
cout << "sexta-feira" << endl;
case 7:
cout << "sabado" << endl;
}
return 0;
}
Here:
frac = modf(n/7, &intpart);
do orfrac = modf((float) n / 7, &intpart);
orfrac = modf(n / 7.0, &intpart);
to force the operation to floating point and not an operation between integers.– anonimo
thank you very much
– Alce de Terno