1

我已经写下了这段代码,我的程序目标是计算两个给定日期和时间之间的分钟数。让我们说一下之间的分钟差异:

14/1/2016 23:18
and
14/1/2004 23:18
is:
6,311,520.00 minutes 

这是我写的代码:

我在计算中有一些错误,我发现我的问题与正确答案最多相差 1440 分钟 - 由 excel 检查。我认为我的问题在于计算两个日期之间的 LEAP DAYS:

    #include <stdio.h>

    typedef struct {
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;     
    }time;
    time time1,time2;

long calcTime(time,time);
int calcDaysFromStart(int,int);
int leapcheck(int);

int main()
{

    printf("Hello\n");
    printf("For calculating the difference between two times:\n");
    printf("Enter the date for first time:\n");
    printf("Enter day:\n");
    scanf("%d",&time1.day);
    printf("Enter month:\n");
    scanf("%d",&time1.month);
    printf("Enter year:\n");
    scanf("%d",&time1.year);
    printf("Enter the exact hour for first time:\n");
    printf("Enter the hour:\n");
    scanf("%d",&time1.hour);
    printf("Enter the minutes:\n");
    scanf("%d",&time1.minute);
    printf("Enter the seconds:\n");
    scanf("%d",&time1.second);
    printf("-----------------------------------\n");
    printf("Enter the date for second time:\n");
    printf("Enter day:\n");
    scanf("%d",&time2.day);
    printf("Enter month:\n");
    scanf("%d",&time2.month);
    printf("Enter year:\n");
    scanf("%d",&time2.year);
    printf("Enter the exact hour for first time:\n");
    printf("Enter the hour:\n");
    scanf("%d",&time2.hour);
    printf("Enter the minutes:\n");
    scanf("%d",&time2.minute);
    printf("Enter the seconds:\n");
    scanf("%d",&time2.second);
    printf("-----------------------------------\n");
    printf("-----------------------------------\n");
    printf("The first time is: %d:%d:%d %d/%d/%d\n", time1.hour ,time1.minute ,time1.second ,time1.day, time1.month ,time1.year);
    printf("The second time is: %d:%d:%d %d/%d/%d\n", time2.hour ,time2.minute ,time2.second ,time2.day, time2.month ,time2.year);
    printf("The Difference between the two times in minutes is:%ld\n", calcTime(time1,time2));
    return 1;   
}


long calcTime(time time1,time time2)
{
    long t1,t2,totalDiff;
    long yearDiffeInMinutes = 0;
    int leapt1, leapt2,leapAdd;
    leapt1 = leapcheck(time1.year);
    leapt2 = leapcheck(time2.year);
    int daysFromStartt1, daysFromStartt2;
    daysFromStartt1 = calcDaysFromStart(time1.month,leapt1);
    daysFromStartt2 = calcDaysFromStart(time2.month,leapt2);
    t1 = time1.minute+time1.hour*60+time1.day*1440+daysFromStartt1*1440;
    t2 = time2.minute+time2.hour*60+time2.day*1440+daysFromStartt2*1440;

    if (time1.year>time2.year)
    {
        leapAdd = (time1.year-time2.year)/4;
        if ((leapt1==1) && (time1.month<3))
            leapAdd--;
        if((leapt2==1) && (time2.month>2))
            leapAdd--;
        printf("THE PARAM leapApp IS:%d\n",leapAdd);
        yearDiffeInMinutes = ((time1.year-time2.year)*525600+leapAdd*1440);
        totalDiff = yearDiffeInMinutes+(t1-t2);
        printf("The first time is bigger\n");
        return totalDiff;
    }
    else if(time2.year>time1.year)
    {
        leapAdd = (time2.year-time1.year)/4;
        if ((leapt2==1) && (time2.month<3))
            leapAdd--;
        if((leapt1==1) && (time1.month>2))
            leapAdd--;
        yearDiffeInMinutes = ((time2.year-time1.year)*525600+leapAdd*1440);
        totalDiff = yearDiffeInMinutes+(t2-t1);
        printf("The second time is bigger\n");
        return totalDiff; 
    }
    else if(t1>t2)/**both times are in the same year**/
    {
        printf("The first time is bigger\n");
        if ((leapt1==1) && (time1.month>2))
            if(time2.month<2)
                return (t1-t2+1440);
        return(t1-t2);
    }
    else if(t2>t1)
    {
        printf("The second time is bigger\n");
        if ((leapt2==1) && (time2.month>2))
            if(time1.month<2)
                return (t2-t1+1440);
        return (t2-t1);
    }
    else
    {
        printf("Both times are equals\n");
        return 0;
    }
}


/**check if the year is leap, return 0 if not a leap and 1 if a leap**/
int leapcheck(int year)
{
    if(year%400==0 || (year%100!=0 && year%4==0))
    {
        printf("THE YEAR %d IS LEAP\n",year);
        return 1;
    }
    printf("THE YEAR %d is NOT LEAP\n",year);
    return 0;
}



/**clalculate how many days past from start ofthe year**/
int calcDaysFromStart(int month, int leap)
{
    if (month==1)
        return 0;
    else if (month==2)
        return 31;
    else if (month==3)
        return (59+leap);
    else if (month==4)
        return (90+leap);
    else if (month==5)
        return (120+leap);
    else if (month==6)
        return (151+leap);
    else if (month==7)
        return (181+leap);
    else if (month==8)
        return (212+leap);
    else if (month==9)
        return (243+leap);
    else if (month==10)
        return (273+leap);
    else if (month==11)
        return (304+leap);
    else if (month==12)
        return (334+leap);
    else return -1;
}
4

3 回答 3

5

计算

leapAdd = (time1.year-time2.year)/4;

是错的。2003 年和 2005 年之间有闰年,但这会忽略它。此外,13 年的跨度可以包括三到四个闰年,而不会达到世纪复杂度。

正确的代码是

leapAdd = time1.year/4 - time2.year/4      // how many Caesarian leap years
        - time1.year/100 + time2.year/100  // Centuries
        + time1.year/400 - time2.year/400; // last correction.

(交换角色 if time2.year > time1.year)。

还,

t1 = time1.minute+time1.hour*60+time1.day*1440+daysFromStartt1*1440;

多算。每月的第一天,没有完整的一天要添加,所以应该是

(time1.day-1)*1440

同样(time1.hour-1)*60,迂腐地,也适用于会议记录。但是,由于超额计数是恒定的,因此不会影响时间差的计算。

于 2013-01-13T14:25:06.177 回答
2

有一个标准库函数可以做到这一点。

time_t seconds_begin, seconds_end;
struct tm breakdown;

breakdown.tm_year = 2004 - 1900;
breakdown.tm_mon = 0; /* january */
breakdown.tm_mday = 14;
breakdown.tm_hour = 23;
breakdown.tm_min = 18;

seconds_begin = mktime( & breakdown );

breakdown.tm_year = 2016 - 1900;

seconds_end = mktime( & breakdown );

printf( "%.2f minutes", (seconds_end - seconds_begin) / 60. );

https://ideone.com/D8djMo

6311520.00 分钟

在 Unix 系统上,我认为这比 Excel 更可靠。

于 2013-01-13T14:23:44.167 回答
0

if您的示例的快速解决方法是在and中添加以下行else

if(leapt1==1 && leapt2==1)
    leapAdd++;

但是这个代码会在更大的时间间隔内失败(包括 100 年和 400 年的闰例外)。所以,我建议你重写这部分代码;例如,您可以遍历间隔中的每一年,并检查它是否跳跃或共同,使用您的特殊外壳作为间隔的开始和结束)。

于 2013-01-13T14:16:04.933 回答