#include<stdio.h>
#include<math.h>
int f(int year,int month)
{
if (month<3)
{
return year-1;
}
else
{
return year;
}
}
int g(int month)
{
if (month<3)
{
return month+3;
}
else
{
return month+1;
}
}
int N_Value(int year,int month,int day)
{
int N = 1461*f(year,month)/4+153*g(month)/5+day;
return N;
}
void main()
{
int year1,mongth1,day1,year2,mongth2,day2,N1,N2,dN;
printf("Please input integer year,mongth,day,the first date:\n");
scanf("%d,%d,%d",&year1,&mongth1,&day1);
printf("Please input integer year,mongth,day,the seconddate:\n");
scanf("%d,%d,%d",&year2,&mongth2,&day2);
N1 = N_Value(year1,mongth1,day1);
N2 = N_Value(year2,mongth2,day2);
dN = abs(N1-N2);
printf("There are %d days between the two dates.\n",dN);
}
结果:
但是后期检验时,发现教材中计算日期N值的公式有Bug
// 计算两个日期之间所经过的天数
#include<stdio.h>
#include<math.h>
int f(int year,int month)
{
if (month<3)
{
return year-1;
}
else
{
return year;
}
}
int g(int month)
{
if (month<3)
{
return month+3;
}
else
{
return month+1;
}
}
int N_Value(int year,int month,int day)
{
int N = 1461*f(year,month)/4+153*g(month)/5+day;
return N;
}
void main()
{
int year1,mongth1,day1,year2,mongth2,day2,N1,N2,dN;
printf("Please input integer year,mongth,day,the first date:\n");
scanf("%d,%d,%d",&year1,&mongth1,&day1);
printf("Please input integer year,mongth,day,the seconddate:\n");
scanf("%d,%d,%d",&year2,&mongth2,&day2);
N1 = N_Value(year1,mongth1,day1);
N2 = N_Value(year2,mongth2,day2);
dN = abs(N1-N2);
printf("There are %d days between the two dates.\n",dN);
printf("N1 %d .\n",N1);
printf("N2 %d .\n",N2);
}