两日期间的天数
#include "stdio.h"
int days[2][13]={365,31,28,31,30,31,30,31,31,30,31,30,31,
366,31,29,31,30,31,30,31,31,30,31,30,31};
int Leap(int year) //判断是否为闰年
{
return ( (year%400==0) || (year%4==0 && year%100!=0));
}
int Deal(int dat0,int dat1)
{
int i,temp,sum0,sum;
int year0,month0,day0,year1,month1,day1;
sum0 = sum = 0;
year0 = dat0/10000; month0 = dat0/100%100; day0 = dat0%100;
year1 = dat1/10000; month1 = dat1/100%100; day1 = dat1%100;
/**//*************************************************************/
/**//* 在起始日期和终止日期在同一年 */
/**//*************************************************************/
if((year1-year0)==0)
{
if((month1-month0)==0)
return (day1-day0);
else
{
temp = Leap(year0);
sum = days[temp][month0] - day0 +1;
for(i=month0+1;i<=month1-1;i++)
sum = sum + days[temp][i];
sum =sum + day1;
return sum;
}
}
/**//************************************************************************/
/**//* 在起始日期和终止日期不在同一年 */
/**//************************************************************************/
//计算开始年剩余的天数
temp = Leap(year0);
sum0 = sum0 + (days[temp][month0] - day0) + 1;
for (i=month0+1; i<=12; i++)
sum0 = sum0 + days[temp][i];
sum = sum +sum0;
sum0 = 0;
//计算中间年份的总天数
for (i=year0+1;i<=year1-1;i++)
{
temp = Leap(i);
sum = sum + days[temp][0];
}
//计算结束年过去的天数
temp = Leap(year1);
for(i=1;i<=month1-1;i++)
sum0 = sum0 + days[temp][i];
sum0 = sum0 + day1;
sum = sum + sum0;
return sum;
}
void main()
{
int dat0,dat1,sum;
FILE *fp = NULL;
if(fp = fopen("riqi.dat","r"))
{
while (fscanf(fp,"%d%d",&dat0,&dat1)==2)
{
if(dat0==0 && dat1==0) break;
sum = Deal(dat0,dat1);
printf("天数为:%d ",sum);
}
}
fclose(fp);
}
int days[2][13]={365,31,28,31,30,31,30,31,31,30,31,30,31,
366,31,29,31,30,31,30,31,31,30,31,30,31};
int Leap(int year) //判断是否为闰年
{
return ( (year%400==0) || (year%4==0 && year%100!=0));
}
int Deal(int dat0,int dat1)
{
int i,temp,sum0,sum;
int year0,month0,day0,year1,month1,day1;
sum0 = sum = 0;
year0 = dat0/10000; month0 = dat0/100%100; day0 = dat0%100;
year1 = dat1/10000; month1 = dat1/100%100; day1 = dat1%100;
/**//*************************************************************/
/**//* 在起始日期和终止日期在同一年 */
/**//*************************************************************/
if((year1-year0)==0)
{
if((month1-month0)==0)
return (day1-day0);
else
{
temp = Leap(year0);
sum = days[temp][month0] - day0 +1;
for(i=month0+1;i<=month1-1;i++)
sum = sum + days[temp][i];
sum =sum + day1;
return sum;
}
}
/**//************************************************************************/
/**//* 在起始日期和终止日期不在同一年 */
/**//************************************************************************/
//计算开始年剩余的天数
temp = Leap(year0);
sum0 = sum0 + (days[temp][month0] - day0) + 1;
for (i=month0+1; i<=12; i++)
sum0 = sum0 + days[temp][i];
sum = sum +sum0;
sum0 = 0;
//计算中间年份的总天数
for (i=year0+1;i<=year1-1;i++)
{
temp = Leap(i);
sum = sum + days[temp][0];
}
//计算结束年过去的天数
temp = Leap(year1);
for(i=1;i<=month1-1;i++)
sum0 = sum0 + days[temp][i];
sum0 = sum0 + day1;
sum = sum + sum0;
return sum;
}
void main()
{
int dat0,dat1,sum;
FILE *fp = NULL;
if(fp = fopen("riqi.dat","r"))
{
while (fscanf(fp,"%d%d",&dat0,&dat1)==2)
{
if(dat0==0 && dat1==0) break;
sum = Deal(dat0,dat1);
printf("天数为:%d ",sum);
}
}
fclose(fp);
}