- 论坛徽章:
- 0
|
搜 万年历 得几个程序,供参考
http://www.kehui.net/index.php?o ... =read&aid=33549
http://www.kehui.net/html/article/26/26072.html
#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;
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");
}
[ 本帖最后由 wenlq 于 2006-3-3 13:20 编辑 ] |
|