I was fiddling with a program that generates a calendar for a month but it is not working correctly with bisexual years since in March 1004 the first day begins Wednesday and the last Friday.
March 1004
Sun Mon Tue Wed Thu Fri Sat
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
already the month of February of 1004 is generating the calendar correctly
February 1004
Sun Mon Tue Wed Thu Fri Sat
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
But for the year 2004 it does not generate the calendar properly, because the first day of February 2004 is Sunday and the last one is also Sunday ..
February 2004
Sun Mon Tue Wed Thu Fri Sat
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
And in March of 1987 the first day falls on Sunday and the last on Tuesday. How do I resolve this error?
Sun Mon Tue Wed Thu Fri Sat
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
Follow the code:
#include <ctime>
#include <cstdio>
const char *months[]={
"\nJanuary","\nFebruary","\nMarch","\nApril","\nMay","\nJune","\nJuly","\nAugust","\nSeptember","\nOctober","\nNovember","\nDecember"
};
int month_days[] = {31, 28, 31, 30, 31, 30, 31 ,31 ,30, 31, 30, 31};
int first_day_year(int year)
{
int first_day;
int x;
int y;
int z;
x = (year - 1.)/4.0;
y = (year - 1.)/100.;
z = (year - 1.)/400.;
first_day = (year + x - y + z) %7;
return first_day;
}
int leapyear(int year)
{
if(__isleap(year))
month_days[2] = 29;
return 1;
else
month_days[1] = 28;
return 0;
}
int calendar(int month, int year, int first_day)
{
int i;
printf("%s %d\n\n", months[month], year);
printf("Sun Mon Tue Wed Thu Fri Sat\n");
for(i = 0; i < month; i++)
{
first_day = ((first_day + month_days[month])%7);
}
for(i = 1; i <= 1 + first_day * 5; i++)
{
printf(" ");
}
for(i = 1; i <= month_days[month]; i++)
{
printf("%2d", i);
if((i + first_day)%7 > 0)
printf(" ");
else
printf("\n ");
}
}
int main(void)
{
int year;
int month;
int first_day;
do{
printf("Enter a month (1 - 12): ");
scanf("%d", &month);
} while (month < 1 || month > 12);
do{
printf("Enter a year (1000 - 3000): ");
scanf("%d", &year);
} while (year < 1000 || year > 3000);
first_day = first_day_year(year);
leapyear(year);
calendar(month, year, first_day);
printf("\n");
return 0;
}