?? calendar.cpp
字號:
/*輸入年份year,輸出該年日歷*/
#include <stdio.h>
#define SPACE " "
int year; //全局變量
int isleap( int y);
int isleap( int y)
{
return (y%400==0||(y%4==0 && y%100!=0));
}
int firstday(int y) //返回某年1月1日是星期幾
{
long n=y*365;
int i;
for(i=1;i<y;i++)
n+=isleap(i);
return n%7;
}
int daysofmonth(int m)
{
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31;
case 4:
case 6:
case 9:
case 11: return 30;
case 2: if(isleap(year))
return 29;
else return 28;
}
return 0;
}
void main()
{
int i,j,weekday;
printf("請輸入年份:");
scanf("%d",&year);
weekday=firstday(year);/*weekday表示當月第一天是星期幾*/
printf("\n************* %d年 *************\n",year);
for(i=1;i<=12;i++)
{
printf("\n%2d月 日 一 二 三 四 五 六\n",i);
printf(" ");
for(j=1;j<=weekday;j++)
printf(" ");
for(j=1;j<=daysofmonth(i);j++)
{
printf("%2d ",j);
if((j+weekday)%7==0)
{
printf("\n");
printf(" ");
}
}
weekday=(weekday+daysofmonth(i)%7)%7;
}
printf("\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -