c語言萬年歷編程
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;
}
}