C语言。求指教!请帮我写一下程序注释解释一下。已知公元1年1月1日是星期一。计算输入的年月日是星期几。

2024-11-26 10:42:28
推荐回答(2个)
回答1:

int monthday[12]={0,31,59,90,120,151,181,212,243,273,304,334};//平常年份到该月1号之前共有多少天,1月1号之前0天 3月1号之前59天

int monthdayLeap[12]={0,31,60,91,121,152,182,213,244,274,305,335};//闰年到x月1号多少天
char * weekname[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};//是指针的数组,不是指针,数组的每个成员是一个指针指向星期几的英语,用于最后的输出
printf("Please input Year Month Day:");
scanf("%ld %ld %ld",&year,&month,&day);
totalday=(year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400;//计算迄今为止每年365天一共多少天加上闰年的个数(每个闰年增加天,被4整除是闰年 被100整除的不是 被400整除的是)
if((year%4==0)&&(year%100!=0)||(year%400==0))
totalday+=monthdayLeap[month-1];
else
totalday+=monthday[month-1];
totalday+=day;
week=totalday%7;//总共的天数对7求模得到余数
printf("%ld,%s\n",totalday,weekname[week]);
}

回答2:

#include
void main()
{
long year,month,day;
long totalday;
int week;
int monthday[12]={0,31,59,90,120,151,181,212,243,273,304,334};//monthday数组初始化
int monthdayLeap[12]={0,31,60,91,121,152,182,213,244,274,305,335};//monthdayLeap数组初始化
char * weekname[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};//这是个指针数组,你也可以看成一个二维数组
printf("Please input Year Month Day:");
scanf("%ld %ld %ld",&year,&month,&day);
totalday=(year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400;//总天数
if((year%4==0)&&(year%100!=0)||(year%400==0))
totalday+=monthdayLeap[month-1];
else
totalday+=monthday[month-1];
totalday+=day;
week=totalday%7;//知道了week,怎么输出星期几?//下面那条语句就是输出星期几的
printf("%ld,%s\n",totalday,weekname[week]);//这个就是输出星期几的地方
}