#include?<stdio.h>
#include?<time.h>
#include?<stdlib.h>
const?monthDay[]?=?{0,31,28,31,30,31,30,31,31,30,31,30,31};
int?isLeap(int?year)
{
if(year?%?4)?return?0;
if(year?%?400)?return?1;
if(year?%?100)?return?0;
return?1;
}
int?getWeek(int?year,?int?month,?int?day)
{
int?c,?y,?week;
if(month?==?1?||?month?==?2)?//判斷month是否為1或2
{
year--;
month+=12;
}
c=?year?/?100;
y?=?year?-?c?*?100;
week?=?(c?/?4)?-?2?*?c?+?(y?+?y?/?4)?+?(13?*?(month?+?1)?/?5)?+?day?-?1;
while(week?<?0)?{week?+=?7;}
week?%=?7;
return?week;
}
void?display(int?year,?int?month)
{
int?monthDays,?weekFirst,?i;
monthDays?=?monthDay[month]?+?(month==2isLeap(year)?:?0);
weekFirst?=?getWeek(year,?month,?1);
system("cls");
printf("?-------%4d年----%2d月-------\n",?year,?month);
printf("?星期日?星期壹?星期二?星期三?星期四?星期五?星期六\n");
for(i=0;?i<weekFirst;?i++)?printf("");
for(i=1;?i<=monthDays;?i++)
{
printf("%8d",?i);
weekFirst++;
if(weekFirst>=7)?{printf("\n");?weekFirst=0;}
}
}
void?main()
{
int?year,?month,?chr;
time_t?timer;
struct?tm?*tblock;
timer?=?time(NULL);
tblock?=?localtime(&timer);
year?=?tblock->tm_year?+?1900;
month?=?tblock->tm_mon?+1;
while(1)
{
display(year,?month);
chr?=?getch();
if(chr?==?0xe0)
{
chr?=?getch();
if(chr?==?0x4b)?/*?方向鍵(←)?*/
{
month?--;
if(month<1)?{month?=?12;?year--;}
}
else?if(chr?==?0x4d)?/*?方向鍵(→)?*/
{
month?++;
if(month>12)?{month?=?1;?year++;}
}
}
else?if(chr?==?'q'?||?chr?==?'Q')?break;
}
}