• 计算两个日期相差多少天


     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 #define DayOfyear 365
     5 
     6 #define ISLEAP(year) ((year % 4 = 0 && year % 100 != 0) || year % 400 == 0)
     7 
     8 int isLeap(int year);
     9 int dayOftheYear(int year, int month, int day);
    10 int diff(int year1, int month1, int day1, int year2, int month2, int day2);
    11 int main()
    12 {
    13     int year1, month1,day1,
    14         year2,month2,day2;
    15 
    16     while(printf("input two dates:XXXX XX XX	 XXXX XX XX
    "),scanf_s("%d %d %d %d %d %d",&year1,&month1,&day1,&year2,&month2,&day2) == 6)
    17     {
    18         printf("The days between the two dates is %d
    ",diff(year1,month1,day1,year2,month2,day2));
    19     }
    20     system("pause");
    21     return 0;
    22 }
    23 //判断闰年
    24 int isLeap(int year)
    25 {
    26     if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    27         return 1;//为闰年
    28     else
    29         return 0;//不是闰年
    30 }
    31 
    32 //计算某个日期在该年第多少天
    33 int dayOftheYear(int year, int month, int day)
    34 {
    35     int sum = 0;//记录天数
    36     int index_month;//遍历月份的下标
    37     if(month < 1 || month > 12 || day < 1 || day > 31)
    38     {
    39         printf("The date is illegal
    ");
    40         system("pause");
    41         return 0;
    42     }
    43     for(index_month = 1; index_month < index_month; index_month++)
    44     {
    45         switch(index_month)
    46         {
    47         case 1:
    48         case 3:
    49         case 5:
    50         case 7:
    51         case 8:
    52         case 10:
    53         case 12:
    54             sum += 31;
    55             break;
    56         case 4:
    57         case 6:
    58         case 9:
    59         case 11:
    60             sum += 30;
    61             break;
    62         case 2:
    63             if(isLeap(year) == 1)
    64                 sum += 29;
    65             else
    66                 sum += 28;
    67             break;
    68         }
    69     }
    70     sum += day;
    71     return sum;
    72 }
    73 
    74 int diff(int year1, int month1, int day1, int year2, int month2, int day2)
    75 {
    76     int sum = 0;//记录两个日期的天数
    77     int index_year;//遍历年数
    78     int num_leap = 0;//记录闰年数
    79 
    80     for(index_year = year1; index_year < year2; index_year++)
    81     {
    82         if(isLeap(index_year) == 1)
    83             num_leap++;
    84     }
    85     sum = num_leap + (year2 - year1) * DayOfyear + dayOftheYear(year2,month2,day2) - dayOftheYear(year1,month1,day1);
    86     return sum;
    87 }
  • 相关阅读:
    C#进阶之路——10.C# 接口
    C#进阶之路——9.C# 抽象类
    C#进阶之路——8.C# 继承
    C#进阶之路——7.ASP.NET常用控件
    C#进阶之路——6.C#字符与字符串
    C#进阶之路——5.C#数组与集合
    C#进阶之路——4.C#类属性和方法
    C#进阶之路——3.C#应用程序编译与执行
    mongodb
    Mycat配置文件详解
  • 原文地址:https://www.cnblogs.com/cpsmile/p/4776502.html
Copyright © 2020-2023  润新知