亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

C語言

C語言判斷兩個日期只差的方法

時間:2024-08-30 08:22:53 C語言 我要投稿
  • 相關推薦

C語言判斷兩個日期只差的方法

  盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平臺的特性,以一個標準規(guī)格寫出的C語言程序可在許多電腦平臺上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業(yè)平臺。下面是小編為大家搜索整理的C語言判斷兩個日期只差的方法,希望能給大家?guī)韼椭?更多精彩內(nèi)容請及時關注我們應屆畢業(yè)生考試網(wǎng)!

  1.普通的寫法

  復制代碼 代碼如下:

  #include

  int leapyear(int year)

  {

  if((year%4==0 && year%100!=0) || year%400==0)

  return 1;

  else

  return 0;

  }

  int days(int *day1, int *day2)

  {

  int i=0;

  int *tmp;

  int diff = 0;

  const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

  if(day1[0] == day2[0])

  {

  if(day1[1] == day2[1])

  {

  diff = day1[2] - day2[2];

  diff = (diff < 0)?(-diff):diff;

  }

  else

  {

  if(day1[1] < day2[1]) //day1=1991-5-8 day2=1991-6-2

  {

  tmp = day1; //day1=1991-6-2 day2=1991-5-8

  day1 = day2;

  day2 = tmp;

  }

  for(i=day2[1]+1; i

  {

  diff += month[i];

  }

  diff += month[day2[1]] - day2[2] + day1[2];

  if(day2[1] <= 2="">2)

  if(leapyear(day2[0]))

  diff++;

  }

  }

  else

  {

  if(day1[0] < day2[0])

  {

  tmp = day1;

  day1 = day2;

  day2 = tmp;

  }

  for(i=day2[0]+1; i

  {

  if(leapyear(i))

  diff += 366;

  else

  diff += 365;

  }

  for(i=day2[1]+1; i<=12; i++) //day1=1992-1-1 day2=1991-1-1

  {

  diff += month[i];

  }

  diff += (month[day2[1]] - day2[2]);

  if(day2[1] <= 2)

  if(leapyear(day2[0]))

  diff++;

  for(i=1; i

  {

  diff += month[i];

  }

  diff += day1[2];

  if(day1[1] > 2)

  if(leapyear(day1[0]))

  diff++;

  }

  return diff;

  }

  int main()

  {

  int day1[3], day2[3];

  int day = 0;

  printf("輸入日期:");

  scanf("%d-%d-%d",&day1[0], &day1[1], &day1[2]);

  printf("輸入另一個日期:");

  scanf("%d-%d-%d",&day2[0], &day2[1], &day2[2]);

  day = days(day1, day2);

  printf("兩個日期之間共有%d天。n",day);

  return 0;

  }

  2.利用結構體,代碼更整潔一些

  復制代碼 代碼如下:

  #include

  typedef struct date

  {

  int year;

  int month;

  int day;

  }DATE;

  int leapyear(int year)

  {

  if((year%4==0 && year%100!=0) || year%400==0)

  return 1;

  else

  return 0;

  }

  int compare(DATE *d1, DATE *d2) //如果第一個日期比第二個日期大,交換日期

  {

  DATE *tmp;

  if(d1->year == d2->year) //年數(shù)相等

  {

  if(d1->month > d2->month) //月數(shù)相等

  {

  tmp = d1;

  d1 = d2;

  d2 = d1;

  }

  else if(d1->month == d2->month) //日期相等

  {

  if(d1->day > d2->day)

  {

  tmp = d1;

  d1 = d2;

  d2 = d1;

  }

  }

  }

  else if(d1->year > d2->year)

  {

  tmp = d1;

  d1 = d2;

  d2 = tmp;

  }

  return 0;

  }

  int diff(DATE *date1, DATE *date2)

  {

  int i;

  int diff = 0;

  const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

  if(date1->year == date2->year)

  {

  if(date1->month == date2->month)

  {

  diff = date2->day - date1->day;

  }

  else

  {

  for(i=date1->month+1; imonth; i++)

  {

  diff += month[i];

  }

  diff += month[date1->month] - date1->day + date2->day;

  if(leapyear(date1->year))

  if(date1->month <=2 date2-="">month >2)

  diff++;

  }

  }

  else

  {

  for(i=date1->year+1; iyear; i++)

  {

  if(leapyear(i))

  diff += 366;

  else

  diff += 365;

  }

  for(i=date1->month+1; i<=12; i++) //date1距離年末多少天

  {

  diff += month[i];

  }

  diff += month[date1->month] - date1->day;

  if(date1->month <= 2)

  if(leapyear(date1->year))

  diff++;

  for(i=1; imonth; i++) //date2距離年初多少天

  {

  diff += month[i];

  }

  diff += date2->day;

  if(date1->month > 2)

  if(leapyear(date2->year))

  diff++;

  }

  return diff;

  }

  int main()

  {

  int days = 0;

  DATE day1, day2;

  DATE *date1, *date2;

  date1 = &day1;

  date2 = &day2;

  printf("輸入日期:");

  scanf("%d-%d-%d",&(date1->year), &(date1->month), &(date1->day));

  printf("輸入另一個日期:");

  scanf("%d-%d-%d",&date2->year, &date2->month, &date2->day);

  compare(date1, date2);

  days = diff(date1, date2);

  printf("兩個日期之間共有%d天。n",days);

  return 0;

  }

【C語言判斷兩個日期只差的方法】相關文章:

判斷兩個日期之差的小程序09-19

C語言的reduce方法應用10-22

C語言的冒泡排序方法08-22

Java程序調(diào)用C/C++語言函數(shù)的方法07-31

C語言返回多個值的方法07-07

C語言入門的基本學習方法09-29

C語言socket編程使用方法06-20

c語言多維數(shù)組地址的表示方法07-31

C 語言產(chǎn)生隨機數(shù)的方法08-28

如何交換兩個文本內(nèi)容的C語言代碼09-21