编写一个计算天数的程序,用户从键盘输入年,月,日,在屏幕中输出此日期是该年的第几天。
#include <stdio.h>
int leap(int year)//判断是否为闰年
{
if(year%4==0&&year%100!=0||year%400==0)
return 1;
else
return 0;
}
int number(int year,int month,int day)
{
int sum=0,i;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};//平年的天数
int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};//闰年的天数
if(leap(year)==1)
for(i=0;i<month-1;i++)
sum+=b[i];
else
for(i=0;i<month-1;i++)
sum+=a[i];
sum+=day;
return sum;
}
void main()
{
int year,month,day,d;
printf("请分别输入年月日:");
scanf("%d%d%d",&year,&month,&day);
printf("%d%d%d",year,month,day);
d=number(year,month,day);
printf("第%d天",d);
}