计算日期差的问题 求指点
计算日期差 就是在我输入的1999 4 1和2000 4 1时它输出了365 但是应该是366 求教怎么改……程序代码:
#include #define isPrime(year) ((year%4==0&&year%100!=0)||(year%400==0)) struct date { int year; int month; int day; }; int main(void) { int dateDiff(struct date mindate,struct date maxdate); struct date mindate,maxdate; int days; scanf("%i %i %i",&mindate.year,&mindate.month,&mindate.day); scanf("%i %i %i",&maxdate.year,&maxdate.month,&maxdate.day); days=dateDiff(mindate,maxdate); printf("%d\n",days); return 0; } int dateDiff(struct date mindate,struct date maxdate) { int days=0,j,flag; const int primeMonth[][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}}; struct date tmp; if ((mindate.year>maxdate.year)|| (mindate.year==maxdate.year&&mindate.month>maxdate.month)||(mindate.year==maxdate.year&&mindate.month==maxdate.month&&mindate.day>maxdate.day)) { tmp=mindate; mindate=maxdate; maxdate=tmp; } for(j=mindate.year;j<MAXDATE.YEAR;++J) days+=isPrime(j)?366:365; flag=isPrime(maxdate.year); for (j=1;j<MAXDATE.MONTH;J++) days+=primeMonth[flag][j-1]; flag=isPrime(maxdate.year); for (j=1;j<MINDATE.MONTH;J++) days-=primeMonth[flag][j-1]; days=days+maxdate.day-mindate.day; return days; }
[此贴子已经被作者于2017-6-21 10:17编辑过]