#include <stdio.h>
void printCalender(int year, int first);
void printFirstTab(int month, int year);
int printDays(int month, int year, int leftDays);
void print_space();
void print_days(int day);
int daysOfMonth(int month, int year);
int IsLeafYear(int year);
void printCalender(int year, int first)
{
int month, leftDays;
for(month = 1 ; month <= 12 ; month++)
{
printFirstTab(month, year);
leftDays = printDays(month, year, first);
first = leftDays;
}
}
void printFirstTab(int month, int year)
{
switch(month)
{
case 1:
printf(" January %d \n", year);
break;
case 2:
printf(" February %d \n", year);
break;
case 3:
printf(" March %d \n", year);
break;
case 4:
printf(" April %d \n", year);
break;
case 5:
printf(" May %d \n", year);
break;
case 6:
printf(" June %d \n", year);
break;
case 7:
printf(" July %d \n", year);
break;
case 8:
printf(" August %d \n", year);
break;
case 9:
printf(" September %d \n", year);
break;
case 10:
printf(" October %d \n", year);
break;
case 11:
printf(" November %d \n", year);
break;
case 12:
printf(" December %d \n", year);
break;
}
printf("---------------------------------------------------\n");
printf("Mon Tue Wed Thu Fri Sat Sun\n");
}
int printDays(int month, int year, int leftDays)
{
int cnt = 0;
int day = daysOfMonth(month, year);
for(int i = 1 ; i <= leftDays ; i++)
{
print_space(); //print space leftDays times
cnt++;
}
for(int nowday = 1 ; nowday <= day ; nowday++) //print day from 1 to day
{
if(cnt == 7){
printf("\n");
cnt = 0;
}
print_days(nowday);
cnt++;
}
printf("\n\n");
return cnt;
}
void print_space()
{
printf("\t");
}
void print_days(int day)
{
printf("%d\t", day);
}
int daysOfMonth(int month, int year)
{
int days;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if(IsLeafYear(year) == 0) days = 29;
else days = 28;
break;
}
return days;
}
int IsLeafYear(int year)
{
if(year%4 == 0)
return 0; //True
if(year%100 == 0)
return 1; //False
if(year%400 == 0)
return 0; //True
else
return 1; //False
}