萬年歷程序要求:
1.當選擇1的時候,輸入年,打印輸入的這一年12月的日歷。
2.當選擇2的時候,輸入年-月,打印輸入這一年這一月的日歷。
實現(xiàn)效果:
選擇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時文章來源:http://www.zghlxwxcb.cn/news/detail-509198.html
*********************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
程序中所用到的函數(shù):
1.參考公式:
C語言根據(jù)日期判斷星期幾(使用基姆拉爾森計算公式)
算法如下:
基姆拉爾森計算公式
W= (d+2m+3(m+1)/5+y+y/4-y/100+y/400)%7
在公式中d表示日期中的日數(shù),m表示月份數(shù),y表示年數(shù)。
注意:在公式中有個與其他公式不同的地方
把一月和二月看成是上一年的十三月和十四月,
例:如果是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文章來源地址http://www.zghlxwxcb.cn/news/detail-509198.html
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;
}
2.得到某年某月有幾天,并返回天數(shù)
int getmonthday(int year,int month)
{
int day=0;
switch(month)
{
//當月是1,3,5,7,8,10,12時,天數(shù)返回31天
//當月是4,6,9,11時,天數(shù)返回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.通過傳遞的參數(shù)輸出某年某一月的日歷
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");
}
//當小于等于月份天數(shù)時輸出日歷幾號
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");//當打完一個月份日歷后進行換行
}
4.主函數(shù)
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:>");
//判斷輸入的選項個數(shù)是否為一個,不是的話輸出輸入錯誤。
ret=scanf("%d",&chose);
if(ret!=1)
{
printf("input chose error\n");
return -1;
}
while((getchar()!='\n'));//吃掉垃圾字符
//chose one時執(zhí)行目的1
//chose two時執(zhí)行目的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;
}
//通過循環(huán)打印目的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 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;
}
到了這里,關于用C語言實現(xiàn)萬年歷的代碼及思路(詳細教程)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!