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