/*
Name: calendar.c version 1.0
Author: Lynxcn(x-bit)
Description: 1752.10-9999.12
Date: 03-31-2004
Copyright: x-bit@163.com
*/
#include <stdio.h>
int main(void)
{
int year, month;
int leap(int year);
int get_days(int year, int month, int date);
int get_week(int year, int month, int date);
void calendar(int year, int month);
printf("Enter a year: ");
scanf("%d", &year);
while(year<0)
{
printf("! illegal year value: year must > 0\n");
printf("Enter a year: ");
scanf("%d", &year);
}
printf("Enter a month: ");
scanf("%d", &month);
while(month<1||month>12)
{
printf("! illegal month value: use 1-12\n");
printf("Enter a month: ");
scanf("%d", &month);
}
calendar(year, month);
return 0;
}
/* 判断润年 */
int leap(int year)
{
int leap=0;
if(year%4==0&&year%100!=0||year%400==0)
leap=1;
return(leap);
}
/* 天数 */
int get_days(int year, int month, int date)
{
int i, days=0;
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(leap(year))
m[2]=29;
for(i=1;i<month;i++)
days+=m[i];
days+=date;
return(days);
}
/* 星期几 */
int get_week(int year, int month, int date)
{
int week;
week=(year-1)+(year-1)/4-(year-1)/100+(year-1)/400;
week+=get_days(year, month, date);
week%=7;
return(week);
}
/** calendar **/
void calendar(int year, int month)
{
int i, j;
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(leap(year))
m[2]=29;
printf("\n%d.%d", year, month);
printf("\n***************************\n");
printf("Sun Mon Tue Wen Thu Fri Sat \n");
for(j=1; j<=get_week(year, month, 1)*4; j++)
printf(" ");
for(i=1;i<=m[month];i++)
{
printf("%d",i);
if(i>=10)
printf(" ");
else
printf(" ");
if(get_week(year, month, i)==6)
printf("\n");
}
printf("\n***************************\n");
}