计算任意两个日期的间隔天数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 int m[13]; 3 int year(int i) 4 { 5 if(i%400==0||i%4==0&&i%100!=0) 6 return 1; 7 else 8 return 0; 9 } 10 int sum(int y,int mon,int d) 11 { 12 13 14 int ans=0,i; 15 if(year(y)) 16 m[2]=29; 17 else 18 m[2]=28; 19 for(i=1;i<y;i++) 20 { 21 if(year(i)) 22 ans+=366; 23 else 24 ans+=365; 25 } 26 for(i=1;i<mon;i++) 27 { 28 ans+=m[i]; 29 } 30 ans+=d; 31 return ans; 32 33 } 34 int jug(int y,int mon,int d) 35 { 36 if(year(mon)) 37 m[2]=29; 38 else 39 m[2]=28; 40 if(m[mon]<d||y<1) 41 return 0; 42 else 43 return 1; 44 45 } 46 int main() 47 { 48 m[1]=m[3]=m[5]=m[7]=m[8]=m[10]=m[12]=31; 49 m[4]=m[6]=m[9]=m[11]=30; 50 int y,m1,d,yy,mm1,dd; 51 scanf("%d %d %d",&y,&m1,&d); 52 scanf("%d %d %d",&yy,&mm1,&dd); 53 if(!jug(y,m1,d)||!jug(yy,mm1,dd)) 54 { 55 printf("error\n"); 56 return 0; 57 } 58 int s1=sum(y,m1,d); 59 int s2=sum(yy,mm1,dd); 60 printf("%d\n",s2-s1); 61 return 0; 62 }