**
判断了两个日期之间的天数
** #include<stdio.h> #define Ipmaire(year) ((year%40)&&(year%100!=0)||year%4000) ///很少使用define宏,格式要记住可以当个函数使用感觉 struct date { int year; int day; int month; }; int datediff(struct date minday, struct date maxday); int main(void) { int day; struct date minday, maxday; printf(“Input this the time:”); scanf_s("%i-%i-%i",&minday.year,&minday.month,&minday.day ); printf(“Input other the time:”); scanf_s("%i-%i-%i", &maxday.year, &maxday.month, &maxday.day); day = datediff(minday, maxday); printf(“您离开学还有%d天”, day); return 0; } int datediff(struct date minday, struct date maxday) { int flag,days=0,i; int a[][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31} }; ///这个地方有点小瑕疵还没想好怎末该 if (minday.year > maxday.year || minday.year == maxday.year && minday.month > maxday.month || minday.year == maxday.year && minday.month == maxday.month && minday.day > maxday.day) { struct date temp; temp = minday; minday = maxday; maxday = temp; } ///通过比较来对minday和maxday函数进行交换 for (i = minday.year; i < maxday.year; i++) { days += Ipmaire(i) ? 366:365; } flag = Ipmaire(maxday.year); for (i = 1; i <=maxday.month; i++) { days += a[flag][i - 1]; } flag = Ipmaire(minday.year); for (i = minday.month; i > 0; i–) { days -= a[flag][i - 1]; } days = days + maxday.day - minday.day;
return days;
} 还行大神批评指正:
|