1.当选择1的时候,输入年,打印输入的这一年12月的日历。
2.当选择2的时候,输入年-月,打印输入这一年这一月的日历。
实现效果:
选择1时
*********************Please chose*************************
*************1.Print a year's calendar *******************
*************2.Print a month's calendar*******************
**********************************************************
your chose:>1
input(year)>2022
2022-1
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
2022-2
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28
2022-3
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
2022-4
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
2022-5
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
2022-6
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
2022-7
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
2022-8
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
2022-9
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
2022-10
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
2022-11
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
2022-12
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
选择2时
*********************Please chose*************************
*************1.Print a year's calendar *******************
*************2.Print a month's calendar*******************
**********************************************************
your chose:>2
input(year-month)>2020-2
2020-2
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
C语言根据日期判断星期几(使用基姆拉尔森计算公式)
算法如下:
基姆拉尔森计算公式
W= (d+2m+3(m+1)/5+y+y/4-y/100+y/400)%7
在公式中d表示日期中的日数,m表示月份数,y表示年数。
注意:在公式中有个与其他公式不同的地方
把一月和二月看成是上一年的十三月和十四月,
例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
以公元元年为参考,公元元年1月1日为星期一
改进后代码如下:
iY=2022, iM=7, iD=17 iWeekDay=0
iY=2022, iM=7, iD=18 iWeekDay=1
…
iY=2022, iM=7, iD=23 iWeekDay=6
int getWeekdayByYearday(int iY, int iM, int iD)
{
/*
iY=2022, iM=7, iD=17 iWeekDay=0
iY=2022, iM=7, iD=18 iWeekDay=1
...
iY=2022, iM=7, iD=23 iWeekDay=6
*/
int iWeekDay = -1;
if (1 == iM || 2 == iM)
{
iM += 12;
iY--;
}
iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / 100 + iY / 400) % 7;
return iWeekDay;
}
int getmonthday(int year,int month)
{
int day=0;
switch(month)
{
//当月是1,3,5,7,8,10,12时,天数返回31天
//当月是4,6,9,11时,天数返回30天
//当月是2月时,判断进行平闰年判断,
//如果是闰年返回29天,平年返回28天
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day=31;
break;
case 4:
case 6:
case 9:
case 11:
day=30;
break;
case 2:
{
if(year%400==0||(year%4==0&&year%100!=0))
{
day=29;
}
else
{
day=28;
}
break;
}
}
return day;
}
int printfmonth(int year,int month)
{
//week是某年某月的第一天的是星期几
int i,j,week=getWeekdayByYearday(year, month, 1);
printf("%d-%d\n",year,month);
printf("\tSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
//当小于等于第一天时,输出tab键进
for(i=0;i<=week;i++)
{
printf("\t");
}
//当小于等于月份天数时输出日历几号
for(i=1,j=week;i<=getmonthday(year,month);i++)
{
//如果日期等于7时进行换行并输入tab键对齐
//j赋值为0
if(j==7)
{
printf("\n");
printf("\t");
j=0;
}
printf("%d",i);//输入月份日期
printf("\t");
j++;
}
printf("\n");//当打完一个月份日历后进行换行
}
int main(int argc,const char * argv[])
{
int chose=0;
int ret=0;
int year,month;
int i;
//打印表头
printf("*********************Please chose*************************\n");
printf("*************1.Print a year's calendar *******************\n");
printf("*************2.Print a month's calendar*******************\n");
printf("**********************************************************\n");
printf("your chose:>");
//判断输入的选项个数是否为一个,不是的话输出输入错误。
ret=scanf("%d",&chose);
if(ret!=1)
{
printf("input chose error\n");
return -1;
}
while((getchar()!='\n'));//吃掉垃圾字符
//chose one时执行目的1
//chose two时执行目的2
switch(chose)
{
case one:
printf("input(year)>");
ret=scanf("%d",&year);
if(ret!=1)
{
printf("input error\n");
return -1;
}
while((getchar()!='\n'));
//判断输入的年份是否正确,不正确的话输出输入错误
if(year<=0)
{
printf("input error\n");
return -1;
}
//通过循环打印目的1的一年日历要求,12个月
for(i=1;i<=12;i++)
{
printfmonth(year,i);
}
break;
case two:
printf("input(year-month)>");
ret=scanf("%d-%d",&year,&month);
if(ret!=2)
{
printf("input error\n");
return -1;
}
while((getchar()!='\n'));
//判断输入的年和月是否正确,不正确输出输入错误
if(year<=0||month<0||month>12)
{
printf("input error\n");
return -1;
}
//打印输入的年-月的日历
printfmonth(year,month);
}
return 0;
}
#include
#define one 1
#define two 2
int getWeekdayByYearday(int iY, int iM, int iD)
{
int iWeekDay = -1;
if (1 == iM || 2 == iM)
{
iM += 12;
iY--;
}
iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / 100 + iY / 400) % 7;
return iWeekDay;
}
int getmonthday(int year,int month)
{
int day=0;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day=31;
break;
case 4:
case 6:
case 9:
case 11:
day=30;
break;
case 2:
{
if(year%400==0||(year%4==0&&year%100!=0))
{
day=29;
}
else
{
day=28;
}
break;
}
}
return day;
}
int printfmonth(int year,int month)
{
int i,j,week=getWeekdayByYearday(year, month, 1);
printf("%d-%d\n",year,month);
printf("\tSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
for(i=0;i<=week;i++)
{
printf("\t");
}
for(i=1,j=week;i<=getmonthday(year,month);i++)
{
if(j==7)
{
printf("\n");
printf("\t");
j=0;
}
printf("%d",i);
printf("\t");
j++;
}
printf("\n");
}
int main(int argc,const char * argv[])
{
int chose=0;
int ret=0;
int year,month;
int i;
//打印表头
printf("*********************Please chose*************************\n");
printf("*************1.Print a year's calendar *******************\n");
printf("*************2.Print a month's calendar*******************\n");
printf("**********************************************************\n");
printf("your chose:>");
ret=scanf("%d",&chose);
if(ret!=1)
{
printf("input chose error\n");
return -1;
}
while((getchar()!='\n'));
switch(chose)
{
case one:
printf("input(year)>");
ret=scanf("%d",&year);
if(ret!=1)
{
printf("input error\n");
return -1;
}
while((getchar()!='\n'));
if(year<=0)
{
printf("input error\n");
return -1;
}
for(i=1;i<=12;i++)
{
printfmonth(year,i);
}
break;
case two:
printf("input(year-month)>");
ret=scanf("%d-%d",&year,&month);
if(ret!=2)
{
printf("input error\n");
return -1;
}
while((getchar()!='\n'));
if(year<=0||month<0||month>12)
{
printf("input error\n");
return -1;
}
printfmonth(year,month);
}
return 0;
}