This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
int y, m, d; | |
//number of days of each month | |
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
//choose the year that you want | |
int year = 2017; | |
//checking if february has 28 or 29 days | |
days[1] -= (y % 4) || (!(y % 100) && (y % 400)); | |
//goes through every month | |
for(m = 1; m <= 12; ++m){ | |
//get the number of days from the array for the corresponding month | |
d = days[m - 1]; | |
y = year; | |
//expression for converting a Gregorian date into a numerical day of the week | |
int day = days[m-1] - ((d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7); | |
//print the date of the month that is the last Sunday | |
printf("%d - %d - %d\n", day, m, year); | |
} | |
return 0; | |
} |
Day Of Week | Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday |
---|---|---|---|---|---|---|---|
Index Retured | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
Now that you know how the expression works and what it will return you, all that you need to do in order to get the date of last Sunday of the Month is calculate the Day of the Week for the last date of the Month and then subtract it to the last day of the same Month and voilá there is the date of the last Sunday.
Sem comentários:
Enviar um comentário