万年历程序要求:
1.当选择1的时候,输入年,打印输入的这一年12月的日历。
2.当选择2的时候,输入年-月,打印输入这一年这一月的日历。
实现效果:
选择1时
*********************Please chose**************************************1.Print a year's calendar ********************************2.Print a month's calendar*****************************************************************************your chose:>1input(year)>20222022-1Sun Mon Tue Wed Thu Fri Sat12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 312022-2Sun Mon Tue Wed Thu Fri Sat1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 282022-3Sun Mon Tue Wed Thu Fri Sat1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 312022-4Sun Mon Tue Wed Thu Fri Sat1 23 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 302022-5Sun Mon Tue Wed Thu Fri Sat1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 312022-6Sun Mon Tue Wed Thu Fri Sat1 2 3 45 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 302022-7Sun Mon Tue Wed Thu Fri Sat1 23 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 30312022-8Sun Mon Tue Wed Thu Fri Sat1 2 3 4 5 67 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728 29 30 312022-9Sun Mon Tue Wed Thu Fri Sat1 2 34 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 302022-10Sun Mon Tue Wed Thu Fri Sat12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 312022-11Sun Mon Tue Wed Thu Fri Sat1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 302022-12Sun Mon Tue Wed Thu Fri Sat1 2 34 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31
选择2时
*********************Please chose**************************************1.Print a year's calendar ********************************2.Print a month's calendar*****************************************************************************your chose:>2input(year-month)>2020-22020-2Sun Mon Tue Wed Thu Fri Sat12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 29
程序中所用到的函数:
1.参考公式:
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=0iY=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;}
2.得到某年某月有几天,并返回天数
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;}
3.通过传递的参数输出某年某一月的日历
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赋值为0if(j==7){printf("\n");printf("\t");j=0;}printf("%d",i);//输入月份日期printf("\t");j++;}printf("\n");//当打完一个月份日历后进行换行}
4.主函数
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时执行目的2switch(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 <stdio.h>#define one 1#define two 2int 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;}