#include<stdio.h>
int getMonthDays(int, int);
int getWeekDay(int, int, int);
void printCalendar(int, int);
main()
{
int year, month, days, youbi;
int n,d,x;
printf("請以空白區隔,輸入西元年份與月份,如2006 3。\n");
while(1){
scanf("%d %d", &year, &month);
if(month > 12 || month < 1 )
printf("輸入錯誤,請重新輸入。\n") ;
else
break ;
}
days = getMonthDays(year, month);
youbi = getWeekDay(year, month, 1);
printf("西元%d年 %d月\n",year, month);
printf("----------------------------\n");
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
printf("----------------------------\n");
x=1 ;
for(n =0;n<youbi;n++)
printf(" ");
for( ; x<=days ; ){
for(n=youbi ; n<7 && x<=days ; n++, x++)
printf(" %2d ", x) ;
printf("\n") ;
youbi = 0 ;
}
system("pause");
}
int getMonthDays(int y, int m)
{
int dm;
switch(m){
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12:
dm= 31;
break;
case 4 : case 6 : case 9 : case 11:
dm=30;
break;
case 2 :
if(y % 4 == 0 && y % 100 != 0 y % 400 == 0)
dm = 29 ;
else
dm = 28;
break;
default:
dm = 0;
}
return dm;
}
int getWeekDay(int y, int m, int d)
{
int w;
if(m== 1 m==2){
y--;
m+=12;
}
w=(y + y/4 - y/100 + y/400 + (13*m+8)/5 + d) % 7;
return w;
}
沒有留言:
張貼留言