日期运算代码如下:
1 #include <cstdio>
2 #include <cstdlib>
3
4 #define N 12
5
6 int main() 7 { 8 int a[N] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 9 int b[N] = { 0 }; 10 int year, mon, day, i, j, total; 11 for (i = 0; i < N; i++) 12 for (j = 0; j <= i; j++) 13 b[i] += a[j]; 14
15 while (scanf_s("%d%d%d", &year, &mon, &day) != EOF) 16 { 17 total = 0; 18 //for (i = 0; i < mon - 1; i++) 19 // total += a[i];
20 total += b[mon - 2]; 21 if (mon > 2) 22 total += year % 4 == 0 && year % 100 != 0 || year % 400 == 0; //判断闰年
23 total += day; 24 printf("%d-%02d-%02d is %dth\n", year, mon, day, total); 25 } 26 system("pause"); 27 return 0; 28 }