C语言实现两个日期间隔天数计算
整体思路:
1>给定闰年和平年相对应的数组
2>判断给定的起始日期和现在日期是否处于同一年,如果是同一年,判断这年是闰年还是平年
2.1>判断起始日期和现在日期是否在同一个月,如果是,间隔天数等于日期相减
2.2>起始日期和现在日期不在同一个月,利用for循环来相加间隔月份的天数+起始日期到起始月份的剩余天数+现在日期的天数即为间隔天数
3>起始日期和现在日期不在同一年的情况下,利用while循环以此判定间隔的年份是闰年还是平年,记数+
3.1>判断起始年份和现在年份是闰年还是平年
3.2>利用闰年和平年数组来计算起始日期和现在日期在本年的天数
3.3>间隔天数= 闰年计数器*366+平年计数器 *365+起始日期在起始年份的天数+现在日期在现在年份的天数
#include<stdio.h>
int main()
{
int star_year, star_month,star_day;
int now_year,now_month,now_day;
int i,r = 0 ,p = 0 ;
int total = 0;
int runnian[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
int pingnian[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
printf("input the star_year,star_month,star_day:(xxxx-xx-xx)");
scanf("%d-%d-%d",&star_year,&star_month,&star_day);
printf("input the now_year,now_month,now,day:(xxxx-xx-xx)");
scanf("%d-%d-%d",&now_year,&now_month,&now_day);
//判断是否在同一年
if(now_year-star_year == 0)
{
if((star_year % 4 == 0 && star_year % 100 != 0) || star_year % 4 == 0) //闰年
{
if(now_month - star_month == 0) //判断是否在同一个月
{
printf("total %d\n",now_day-star_day);
}
else
{
for(i = star_month + 1; i < now_month ; i++)//不在同一个月,相加间隔月数
{
total = total + runnian[i];
}
total = total + runnian[star_month]-star_day + now_day;
printf("total %d\n",total);
}
}
else //不是闰年的情况
{
if(now_month - star_month == 0)
{
printf("total %d",now_day-star_day);
}
else
{
for(i = star_month + 1; i < now_month ; i++)
{
total = total + pingnian[i];
}
total = total + pingnian[star_month]-star_day + now_day;
printf("total %d\n",total);
}
}
}
else// 不在同一年的情况
{
while(now_year > star_year + 1)//用计数器计算间隔年份闰年和平年的个数
{
if(((star_year + 1) % 4 == 0 && (star_year + 1) % 100 != 0) || (star_year + 1) % 4 == 0)
{
r++ ;
}
else
{
p++ ;
}
star_year ++ ;
}
//判断起始年和现在年是闰年还是平年,并计算
if((star_year % 4 == 0 && star_year % 100 != 0) || star_year % 4 == 0)
{
if((now_year % 4 == 0 && now_year % 100 != 0) || now_year % 4 == 0)
{
for(i = star_month + 1;i <= 12 ; i ++)
{
total = total + runnian[i];
}
total = total + runnian[star_month] - star_day;
for(i = 0 ; i < now_month ;i++)
{
total = total + runnian[i];
}
total = total + now_day + r * 366 + p * 365;
printf("total %d\n",total);
}
else
{
for(i = star_month + 1;i <= 12 ; i ++)
{
total = total + runnian[i];
}
total = total + runnian[star_month] - star_day;
for(i = 0 ; i < now_month ;i++)
{
total = total + pingnian[i];
}
total = total + now_day + r * 366 + p * 365;
printf("total %d\n",total);
}
}
else
{
if((now_year % 4 == 0 && now_year % 100 != 0) || now_year % 4 == 0)
{
for(i = star_month + 1;i <= 12 ; i ++)
{
total = total + runnian[i];
}
total = total + runnian[star_month] - star_day;
for(i = 0 ; i < now_month ;i++)
{
total = total + runnian[i];
}
total = total + now_day + r * 366 + p * 365;
printf("total %d\n",total);
}
else
{
for(i = star_month + 1;i <= 12 ; i ++)
{
total = total + runnian[i];
}
total = total + runnian[star_month] - star_day;
for(i = 0 ; i < now_month ;i++)
{
total = total + runnian[i];
}
total = total + now_day + r * 366 + p * 365;
printf("total %d\n",total);
}
else
{
for(i = star_month + 1;i <= 12 ; i ++)
{
total = total + runnian[i];
}
total = total + runnian[star_month] - star_day;
for(i = 0 ; i < now_month ;i++)
{
total = total + pingnian[i];
}
total = total + now_day + r * 366 + p * 365;
printf("total %d\n",total);
}
}
}
}
有什么错误欢迎大佬指正!谢谢

浙公网安备 33010602011771号