C語言實現兩個日期間隔天數計算


整體思路:

1>給定閏年和平年相對應的數組

2>判斷給定的起始日期和現在日期是否處於同一年,如果是同一年,判斷這年是閏年還是平年

2.1>判斷起始日期和現在日期是否在同一個月,如果是,間隔天數等於日期相減

2.2>起始日期和現在日期不在同一個月,利用for循環來相加間隔月份的天數+起始日期到起始月份的剩余天數+現在日期的天數即為間隔天數

3>起始日期和現在日期不在同一年的情況下,利用while循環以此判定間隔的年份是閏年還是平年,記數+

3.1>判斷起始年份和現在年份是閏年還是平年

3.2>利用閏年和平年數組來計算起始日期和現在日期在本年的天數

3.3>間隔天數= 閏年計數器*366+平年計數器 *365+起始日期在起始年份的天數+現在日期在現在年份的天數

 

#include<stdio.h>
int main()
{
 int star_year, star_month,star_day;
 int now_year,now_month,now_day;
 int i,r = 0 ,p = 0 ;
 int total = 0;

 int runnian[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
 int pingnian[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};

 printf("input the star_year,star_month,star_day:(xxxx-xx-xx)");
 scanf("%d-%d-%d",&star_year,&star_month,&star_day);
 printf("input the now_year,now_month,now,day:(xxxx-xx-xx)");
 scanf("%d-%d-%d",&now_year,&now_month,&now_day);
//判斷是否在同一年
 if(now_year-star_year == 0)
 {
  if((star_year % 4 == 0 && star_year % 100 != 0) || star_year % 4 == 0) //閏年
  {
    if(now_month - star_month == 0) //判斷是否在同一個月
    {
      printf("total %d\n",now_day-star_day);
    }
    else
    {
     for(i = star_month + 1; i < now_month  ; i++)//不在同一個月,相加間隔月數
     {
      total = total + runnian[i];
     }
      total = total + runnian[star_month]-star_day + now_day;
      printf("total %d\n",total);
    }
  }
  else //不是閏年的情況
   {
     if(now_month - star_month == 0)
     {
      printf("total %d",now_day-star_day);
     }
     else
     {
      for(i = star_month + 1; i < now_month  ; i++)
      {
       total = total + pingnian[i];
      }
      total = total + pingnian[star_month]-star_day + now_day;
      printf("total %d\n",total);
         }
   }
 }

 else// 不在同一年的情況
 {
  while(now_year > star_year + 1)//用計數器計算間隔年份閏年和平年的個數
  {
   if(((star_year + 1) % 4 == 0 && (star_year + 1) % 100 != 0) || (star_year + 1) % 4 == 0)
   {
    r++ ;
   }
   else
   {
    p++ ;
   }
   star_year ++ ;
  }
  //判斷起始年和現在年是閏年還是平年,並計算
  if((star_year % 4 == 0 && star_year % 100 != 0) || star_year % 4 == 0)
  {
   if((now_year % 4 == 0 && now_year % 100 != 0) || now_year % 4 == 0)
    {
      for(i = star_month + 1;i <= 12 ; i ++)
        {
        total = total + runnian[i];
      }
        total = total + runnian[star_month] - star_day;
       for(i = 0 ; i < now_month ;i++)
       {
        total = total + runnian[i];
       }
        total = total + now_day + r * 366 + p * 365;
        printf("total %d\n",total);
    }
   else
    {
     for(i = star_month + 1;i <= 12 ; i ++)
     {
       total = total + runnian[i];
     }
      total = total + runnian[star_month] - star_day;
     for(i = 0 ; i < now_month ;i++)
      {
        total = total + pingnian[i];
      }
      total = total + now_day + r * 366 + p * 365;
      printf("total %d\n",total);
    }
  }
  else
    {
      if((now_year % 4 == 0 && now_year % 100 != 0) || now_year % 4 == 0)
       {
        for(i = star_month + 1;i <= 12 ; i ++)
         {
          total = total + runnian[i];
         }
           total = total + runnian[star_month] - star_day;
        for(i = 0 ; i < now_month ;i++)
         {
           total = total + runnian[i];
         }
        total = total + now_day + r * 366 + p * 365;
        printf("total %d\n",total);
       }
      else
       {
        for(i = star_month + 1;i <= 12 ; i ++)
           {
            total = total + runnian[i];
           }
           total = total + runnian[star_month] - star_day;
        for(i = 0 ; i < now_month ;i++)
         {
           total = total + runnian[i];
         }
        total = total + now_day + r * 366 + p * 365;
        printf("total %d\n",total);
       }
      else
       {
        for(i = star_month + 1;i <= 12 ; i ++)
           {
            total = total + runnian[i];
           }
        total = total + runnian[star_month] - star_day;
        for(i = 0 ; i < now_month ;i++)
          {
            total = total + pingnian[i];
          }
           total = total + now_day + r * 366 + p * 365;
           printf("total %d\n",total);
       }
    }
 }
}

  有什么錯誤歡迎大佬指正!謝謝

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM