當前位置:首頁 » 編程語言 » c語言萬年歷編程

c語言萬年歷編程

發布時間: 2024-01-05 12:07:19

A. 用c語言編寫萬年歷,要求輸出任意一年的某月,能顯示出這個月的日歷

1、首先要判斷一個年份是閏年還是平年,用一個子程序來做。

B. C語言編寫萬年歷

#include"stdio.h"
void print(int);//函數聲明
int dobb(int);
int date(int,int,int);
void main()
{
int year=0;
printf("輸入列印的年份(XXXX)\n");
scanf("%d",&year);
print(year);
}
int dobb(int year)//閏年判斷
{
if((year%4==0 && year%100!=0)|| (year%400==0))
return(1);
else return(0);
}
int date(int year,int month,int day)//判斷該年某月某日是星期幾的計算
{
int leap,cn=0,sum;//cn計算的是從該年1月1日起,到達這一天的天數總和
leap=dobb(year);
switch(month-1)
{
case 11:cn+=30;
case 10:cn+=31;
case 9:cn+=30;
case 8:cn+=31;
case 7:cn+=31;
case 6:cn+=30;
case 5:cn+=31;
case 4:cn+=30;
case 3:cn+=31;
case 2:if(leap) cn+=29;
else cn+=28;
case 1:cn+=31;
default:cn+=day;
}
sum=year-1+(year-1)/4-(year-1)/100+(year-1)/400+cn;//這是核心的一條公式,返回值是星期幾(沒有為什麼)
return(sum%7);
}

void print(int year)//列印的核心演算法
{
int i,j,n,leap,days,k,count;
leap=dobb(year);//判斷是否閏年
days=date(year,1,1);//計算該年第一天是星期幾
printf("%d年\n",year);
for(i=1;i<=12;i++)
{
printf("%3d月份\n",i);
printf("\n");
printf("\t");
printf("%-8s%-8s%-8s%-8s%-8s%-8s%-8s\n","Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
if(days==7)
{
days=0;n=days;
}
else n=days;
printf("\t");
for(j=1;j<=8*days;j++)
printf(" ");
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:count=31;break;
case 4:
case 6:
case 9:
case 11:count=30;break;
case 2:if(leap) count=29;
else count=28; break;
}
for(k=1;k<=count;k++)//以下代碼是控制界面的對齊
{
printf("%-8d",k);
n++;
if(n==7)
{printf("\n");printf("\t");n=0;}
}
printf("\n\n");
days=n;
}
}

熱點內容
我的世界正版小游戲伺服器電腦版 發布:2025-10-14 02:33:44 瀏覽:327
哪些資料庫是免費的 發布:2025-10-14 02:05:40 瀏覽:722
php遠程oracle 發布:2025-10-14 01:49:49 瀏覽:577
linux獲取ipc 發布:2025-10-14 01:40:54 瀏覽:664
蘋果復雜密碼怎麼設置 發布:2025-10-14 01:29:54 瀏覽:965
csgo顯示伺服器ip的代碼 發布:2025-10-14 00:25:06 瀏覽:140
安卓手機沒網路還有流量怎麼回事 發布:2025-10-14 00:04:24 瀏覽:149
網頁特效源碼 發布:2025-10-13 23:59:11 瀏覽:848
編譯github 發布:2025-10-13 23:41:30 瀏覽:159
編程貓扣子 發布:2025-10-13 23:41:24 瀏覽:363