C语言编程(程序怎样编写):给定年、月、日,计算该日是该年的第几天,然后计算该日是星期几?C语言怎样计算天数?
C语言编程(程序怎样编写):给定年、月、日,计算该日是该年的第几天,然后计算该日是星期几?
#include <stdio.h>
int mon[]={31,28,31,30,31,30,31,31,30,31,30,31};
int leap_year(int n)
{
if(n%400==0||(n%4==0 && n%100!=0))
return 1;
else
return 0;
}
int cal_dayCount(int year,int month,int day)
{
int i,sum;
for(i=0,sum=0;i<month-1;i++)
sum+=mon[i];
if(month>2)
sum+=day+leap_year(year);
else
sum+=day;
return sum;
}
int cal_weekday(int year,int month,int day)
{
int y, c, m, d;
int w;
if (month >= 3)
{
m = month;
y = year % 100;
c = year / 100;
d = day;
}
else
{
m = month + 12;
y = (year - 1) % 100;
c = (year - 1) / 100;
d = day;
}
w = y + y / 4 + c / 4 - 2 * c + (26*(m+1))/10 + d - 1;
if (w < 0)
{
w = 7 - (-w) % 7;
}
else
{
w = w % 7; //如果w等于0,表示为星期天
}
return w;
}
int main()
{
int year,month,day;
int dayCount=0,i;
printf("请输入当前的年,月,日: ");
scanf("%d-%d-%d",&year,&month,&day);
printf("该日是该年的第几天: %d\n",cal_dayCount(year,month,day));
printf("该日是星期几: %d\n",cal_weekday(year,month,day));
return 0;
}
C语言怎样计算天数?
【程序1】:main(){intday,month,year,sum,leap;printf("\npleaseinputyear,month,day\n");scanf("%d,%d,%d",&year,&month,&day);switch(month)/*先计算某月以前月份的总天数*/{case1:sum=0;break;case2:sum=31;break;case3:sum=59;break;case4:sum=90;break;case5:sum=120;break;case6:sum=151;break;case7:sum=181;break;case8:sum=212;break;case9:sum=243;break;case10:sum=273;break;case11:sum=304;break;case12:sum=334;break;default:printf("dataerror");break;}sum=sum+day;/*再加上某天的天数*/if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/leap=1;elseleap=0;if(leap==1&&month>2)/*如果是闰年且月份大于2,总天数应该加一天*/sum++;printf("Itisthe%dthday.",sum);}
day-added中文是什么意思?
A Leap Year consists of 366 days where February 29 is added as an extra day at the end of February.闰年有366天,在二月底添加额外的一天,2月29日就是the day added.