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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int day(int , int ,int );
void calender(int , int);
bool is_leap(int);
int main()
{
int number_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
char month[12][12] = { "January","February","March","April","May","JUne","July","August","September","October","November","December"};
int start,m,y;
printf("Enter month/year: ");
scanf("%d/%d",&m,&y);
if(is_leap(y))
number_days[1] = 29; //toggle last day of February
printf("\n\n\t\t%s %d\n\n",month[m-1],y);
calender(day(1,m,y),number_days[m-1]); //day(1,m,y) returns the first day of the month
return 0;
}
int day(int d,int m, int y)
{
/*The following, invented by Mike Keith and published in Journal of Recreational Mathematics, Vol. 22, No. 4, 1990, p. 280, is conjectured to be the shortest expression of a day-of-the-week algorithm: */
return (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7 ;
}
void calender(int start, int number)
{
int i,j;
printf("%5cSun Mon Tue Wed Thu Fri Sat \n",' ');
for(i = 0; i < number; i++)
{
if(i %7 == 0)
printf("\n");
if(i < start)
{
printf("%6c",' ');
++number; //increase upper bound to compensate for each white space
}
else
printf("%6d",i-(start-1)); //subtract offset from i;
}
printf("\n\n");
}
bool is_leap(int year) //is the year is a leap year
{
if(year%400 == 0)
return true;
else if(year%100 == 0 )
return false;
else if(year %4 == 0)
return true;
else
return false;
}
|