91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言獲取兩個日期之間共有多少天

發布時間:2020-07-30 22:37:10 來源:網絡 閱讀:274 作者:Lee_1985 欄目:編程語言

最近做項目需要基于C語言實現一個獲取兩個日期之間共有多少天的函數,通過搜索一些資料,實現了int GetInternalDays(Date date1, Date date2)接口,該接口實現了data1和date2直間相差的天數。


/*****************************************************************************************
 * Copyright:Yue Workstation
 *
 * FileName:CountDate.c
 *
 * Function:count the days of two date
 *
 * Author:Abel Lee
 *
 * Created on:2011-4-18
 *
 * Log:
 * ***************************************************************************************
 *         2011-4-18 create.
 *****************************************************************************************/
#include <stdio.h>
#define TestMain main  //The toggle of main function
typedef struct date
{
    int year;
    int month;
    int day;
}Date;//Date struct
//The month of the date given have days in this year.
int daysInMonth[13] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
/*****************************************************************************************
 * Function Name:IsLeap
 *
 * Function:Whether the year is a Leap
 *
 * Input Parameter:year:The year need to be judged
 *
 * Return Value:1:Leap 0:No leap
 *
 * Author: Abel Lee
 *
 * Log:create on 2011-4-18
 ****************************************************************************************/
int IsLeap(const int year)
{
    return (((year % 4 == 0) && year % 100 != 0) || year % 400 == 0);
}
/****************************************************************************************
 * Function Name:CountDays
 *
 * Function:Count the days of in this year
 *
 * Input Parameter:CurrentDate:the day of in this year
 *
 * Return Value:The days of in this year
 *
 * Author: Abel Lee
 *
 * Log:create on 2011-4-18
 ****************************************************************************************/
int CountDays(const Date *currentDate)
{
    int sumDays = daysInMonth[currentDate->month] + currentDate->day;
    if(IsLeap(currentDate->year) && (currentDate->month >= 3))
    {
        sumDays++;
    }
    return sumDays;
}
/***************************************************************************************
 * Function Name:SwapDate
 *
 * Function:Swap the two dates
 *
 * Input Parameter:date1:The first date
 *                 date2:The second date
 * Return Value:None
 *
 * Author:Li
 *
 * Log:create on 2011-4-19
 **************************************************************************************/
void SwapDate(Date *date1,Date *date2)
{
    int tmp = 0;
    tmp = date1->year;
    date1->year = date2->year;
    date2->year = tmp;
    tmp = date1->month;
    date1->month = date2->month;
    date2->month = tmp;
    tmp = date1->day;
    date1->day = date2->day;
    date2->day = tmp;
    return;
}
/*************************************************************************************
 * Function Name:GetIntervalDays
 *
 * Function:Get the days between date1 and date2
 *
 * Input Parameter:date1:The first date
 *                 date2:The second date
 *
 * Return Value:The days between date1 and date2
 *              -1 is the date format is error
 *
 * Author:Abel Lee
 *
 * Log:create on 2011-4-19
 ************************************************************************************/
int GetIntervalDays(Date *date1,Date *date2)
{
    int days1 = 0;
    int days2 = 0;
    int sumDays = 0;
    int i = 0;
    int years = 0;
    //Parameter range check
    if(date1->year < 1900 || date1->year > 2038 || date2->year < 1900 || date2->year > 2038)
    {
        printf("The year range is 1900 ~ 2038/n");
        return -1;
    }
    if(date1->month > 12 || date1->month < 1 || date2->month > 12 || date2->month < 1)
    {
        printf("The month range is 1 ~ 12/n");
        return -1;
    }
    switch(date1->month)
    {
    case 2:
        if(IsLeap(date1->year))
        {
            if(date1->day < 1 || date1->day > 29)
            {
                printf("%d-%d range is 0 ~ 29/n",date1->year,date1->month);
                return -1;
            }
        }
        else
        {
            if(date1->day < 1 || date1->day > 28)
            {
                printf("%d-%d range is 0 ~ 28/n",date1->year,date1->month);
                return -1;
            }
        }
        break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        if(date1->day < 1 || date1->day > 31)
        {
            printf("%d-%d range is 0 ~ 31/n",date1->year,date1->month);
            return -1;
        }
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        if(date1->day < 1 || date1->day > 30)
        {
            printf("%d-%d range is 0 ~ 30/n",date1->year,date1->month);
            return -1;
        }
        break;
    default:
        printf("The format of date is error!/n");
        return -1;
    }
    switch(date2->month)
    {
    case 2:
        if(IsLeap(date2->year))
        {
            if(date2->day < 1 || date2->day > 29)
            {
                printf("%d-%d range is 0 ~ 29/n",date2->year,date2->month);
                return -1;
            }
        }
        else
        {
            if(date2->day < 1 || date2->day > 28)
            {
                printf("%d-%d range is 0 ~ 28/n",date2->year,date2->month);
                return -1;
            }
        }
        break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        if(date2->day < 1 || date2->day > 31)
        {
            printf("%d-%d range is 0 ~ 31/n",date1->year,date1->month);
            return -1;            
        }
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        if(date2->day < 1 || date2->day > 30)
        {
            printf("%d-%d range is 0 ~ 30/n",date2->year,date2->month);
            return -1;
        }
        break;
    default:
        printf("The format of date is error!/n");
        return -1;
    }
    //date2 must be newer than date1
    if(date1->year > date2->year)
    {
        SwapDate(date1,date2);
    }
    else if((date2->year == date1->year) && (date1->month > date2->month))
    {
        SwapDate(date1,date2);
    }
    else if((date2->year == date1->year) && (date1->month == date2->month) && (date1->day > date2->day))
    {
        SwapDate(date1,date2);
    }
    years = date2->year - date1->year;
    days1 = CountDays(date1);
    days2 = CountDays(date2);
    sumDays = days2 - days1;
    if(years == 1)
    {
        sumDays += 365;
    }
    else if(years > 1)
    {
        for(i = date1->year; i < date2->year; i++)
        {
            sumDays += 365;
            if(IsLeap(i))
            {
                sumDays++;
            }
        }
    }
    return sumDays;
}
int main(int argc,char *argv[])
{
    Date date1,date2;
    int year,month,day;
    printf("input the first date(eg. 2011 2 5):");
    scanf("%d%d%d",&year,&month,&day);
    date1.year = year;
    date1.month = month;
    date1.day = day;
    printf("input the second date(eg. 2015 9 10):");
    scanf("%d%d%d",&year,&month,&day);
    date2.year = year;
    date2.month = month;
    date2.day = day;
    printf("********%d********\n",GetIntervalDays(&date1,&date2));
    printf("Application run successfully!\n");
    return 0;
}

可見,一個如此簡單的功能,C語言卻需要這么多代碼,相比與Java/Python等更高級語言,確實開發效率相對較低!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

定州市| 河北区| 辽宁省| 扬中市| 屯昌县| 清涧县| 万州区| 石狮市| 商丘市| 莒南县| 新源县| 桐柏县| 西平县| 治多县| 靖江市| 宜章县| 石屏县| 靖州| 肇东市| 略阳县| 恭城| 文山县| 金堂县| 堆龙德庆县| 丰原市| 武威市| 盐边县| 贵港市| 武宁县| 隆尧县| 甘肃省| 六盘水市| 读书| 衡阳县| 沈阳市| 库尔勒市| 昌图县| 海宁市| 刚察县| 定陶县| 博野县|