/******************************************* * Compute number of days between two dates * * ---------------------------------------- * * Ref.: "Mathématiques par l'informatique * * individuelle (1) par H. Lehning * * et D. Jakubowicz, Masson, Paris, * * 1982" [BIBLI 06]. * * * * C++ version by J-P Moreau, Paris. * * (www.jpmoreau.fr) * *******************************************/ /***************************************************************************** Note: The two dates must be between 01/01/1901 and 12/31/2099 to avoid treatment of special "secular" years, such as 1800, 1900, 2100... (2000, a multiple of 400, is a normal year!) The first date is M, D, Y N, number of days between first date and fictitious starting date 01/00/1901, is given by formula: N = INT(365,25*(Y-1901)+A(M)+D) where A(M) is given by table below: M 1 2 3 4 5 6 7 8 9 10 11 12 A(M) 0 31 59,25 90,25 120,25 151,25 181,25 212,25 243,25 273,25 304,25 334,25 First N is saved in R. The second date is again M, D, Y N is again computed with same formula. Final result is N - R. ******************************************************************************/ #include #include float A[13]; short M, // month number D; // day number int Y; // year number float N,R; // number of days void Calcul() { scanf("%d %d %d",&M, &D, &Y); N = (float) 365.25 * (Y - 1901) + A[M] + D; N = (float) floor(N); } void main() { A[1] = 0; A[2] = 31; A[3] = 59.25; A[4] = 90.25; A[5] = 120.25; A[6] = 151.25; A[7] = 181.25; A[8] = 212.25; A[9] = 243.25; A[10] = 273.25; A[11] = 304.25; A[12] = 334.25; printf("\n Compute number of days between two dates\n"); printf(" (from 01/01/1901 to 12/31/2099)\n\n"); printf(" Enter first date (M D Y): "); Calcul(); R = N; printf(" Enter second date (M D Y): "); Calcul(); R = N - R; printf("\n Number of days: %6.0f\n\n\n",R); } //end of file ndays.cpp