huangblue / Algorithm

计算机算法
0 stars 0 forks source link

求两个日期之间的天数 #6

Open huangblue opened 7 years ago

huangblue commented 7 years ago

In certain applications, particularly in the financial area, it is often necessary to calculate the number of elapsed days between two dates. For example, the number of days between July 2, 2005, and July 16, 2005, is obviously 14. But how many days are there between August 8, 2004, and February 22, 2005? This calculation requires a bit more thought. Luckily, a formula can be used to calculate the number of days between two dates. This is affected by computing the value of N for each of the two dates and then taking the difference, where N is calculated as follows: N = 1461 x f(year, month) / 4 + 153 x g(month) / 5 + day where f(year, month) = year - 1 if month <= 2 year otherwise g(month) = month + 13 if month <= 2 month + 1 otherwise

N: 1900年3月1日以后(含)不加 1800年3月1日以后(含)到1900年2月28日,加1 1700年3月1日以后(含)到1800年2月28日,加2

huangblue commented 7 years ago

// Program to determine the number of elapsed days between two dates.

include

include

struct date {   int month;   int day;   int year; };

int yearMonth (int year,int month) {   int n;

  if (month<=2){n=year-1;}   else {n=year;}   return n; }

int month (int month) {   int n;   if (month<=2){n=month+13;}   else {n=month+1;}   return n; }

int addnum (int year,int month,int day) {   if (year==1900 && month>=3 || year>1900){return 0;}   if (year==1800 && month>=3 || year>1800){return 1;}   if (year==1700 && month>=3 || year>1700){return 2;}   return -1; }

int numberOfDays (struct date d) {

  int yearMonth (int year,int month);   int month (int month);   int addnum (int year,int month,int day);

  int f,g,n;

  n=addnum(d.year,d.month,d.day);   if (n==-1){return -1;}

  f=yearMonth(d.year,d.month);

  g=month(d.month);

  return 1461f/4+153g/5+d.day+n;

}

int main (void) {

  int numberOfDays (struct date d);

  struct date day1, day2;   int n1,n2;

  printf ("Enter day1's date (mm dd yyyy): ");   scanf ("%i%i%i", &day1.month, &day1.day, &day1.year);  n1=numberOfDays(day1);   if (n1==-1){printf ("Day1 is out of range!\n");return -1;}

  printf ("Enter day2's date (mm dd yyyy): ");   scanf ("%i%i%i", &day2.month, &day2.day, &day2.year);

  n2=numberOfDays(day2);   if (n2==-1){printf ("Day2 is out of range!\n");return -1;}

  printf ("Elapse days:%i\n",n2-n1);

  return 0; }

huangblue commented 7 years ago

Enter day1's date (mm dd yyyy): 01 01 1741 Enter day2's date (mm dd yyyy): 01 01 2017 Elapse days:100807 Process returned 0 (0x0) execution time : 12.087 s Press any key to continue.

huangblue commented 7 years ago

Enter day1's date (mm dd yyyy): 01 01 1700 Day1 if out of range! Process returned -1 (0xFFFFFFFF) execution time : 5.288 s Press any key to continue.

huangblue commented 7 years ago

If you take the value of N as computed above, subtract 621,049 from it, and then take that result modulo 7, you get a number from 0 to 6 that represents the day of the week (Sunday through Saturday, respectively) on which the particular day falls. For example, the value of N computed for August 8, 2004, is 732,239 as derived previously. 732,239 – 621,049 gives 111,190, and 111,190 % 7 gives 2,indicating that this date falls on a Tuesday.

huangblue commented 7 years ago

// Program to determine the the day of the week.

include

include

struct date {   int month;   int day;   int year; };

int yearMonth (int year,int month) {   int n;

  if (month<=2){n=year-1;}   else {n=year;}   return n; }

int month (int month) {   int n;   if (month<=2){n=month+13;}   else {n=month+1;}   return n; }

int addnum (int year,int month,int day) {   if (year==1900 && month>=3 || year>1900){return 0;}   if (year==1800 && month>=3 || year>1800){return 1;}   if (year==1700 && month>=3 || year>1700){return 2;}   return -1; }

int numberOfDays (struct date d) {

  int yearMonth (int year,int month);   int month (int month);   int addnum (int year,int month,int day);

  int f,g,n;

  n=addnum(d.year,d.month,d.day);   if (n==-1){return -1;}

  f=yearMonth(d.year,d.month);

  g=month(d.month);

  return 1461f/4+153g/5+d.day+n;

}

int main (void) {

  int numberOfDays (struct date d);

  struct date day;   int n,w;

  printf ("Enter date (mm dd yyyy): ");   scanf ("%i%i%i", &day.month, &day.day, &day.year);   n=numberOfDays(day);   if (n==-1){printf ("Out of range!\n");return -1;}

  w=(n-621049)%7;

  printf ("%i/%i/%.2i is ",day.month,day.day, day.year % 100);   switch(w){   case 0:     printf("Sunday\n");    break;   case 1:     printf("Monday\n");    break;   case 2:    printf("Tuesday\n");    break;   case 3:    printf("Wednesday\n");     break;  case 4:     printf("Thursday\n");     break;  case 5:     printf("Friday\n");       break;   case 6:     printf("Saturday\n");  }   return 0; }

huangblue commented 7 years ago

Enter date (mm dd yyyy): 05 24 2017 5/24/17 is Wednesday Process returned 0 (0x0) execution time : 18.479 s Press any key to continue.