#include<stdio.h>
//====================================
//全局变量
//====================================
int month_day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
//======================================
//判断闰年
//======================================
int RunNian(int year)
{
if(year%400==0||(year%100!=0&&year%4==0))
{
return 1;
}
else
{
return 0;
}
}
//===================================
//计算当天距年初的天数
//===================================
int Month_Day_sum1(int year,int month, int day)
{
int i;
int Month_Day_sum=0;
if(RunNian(year))
{
month_day[1]=29;
}
else
{
month_day[1]=28;
}
for(i=0;i<month-1;i++)
{
Month_Day_sum+=month_day[i];
}
Month_Day_sum+=day;
return Month_Day_sum;
}
//=====================================
//计算时间差函数
//=====================================
int NianCha(int _year,int _month,int _day,int year,int month,int day)
{
int i,j;
int dayday=0;
int DaySum=0;
int MonthSum=0;
int YearSum=0;
for(i=_year;i<year;i++)
{
if(RunNian(i))
{
YearSum+=366;
}
else
{
YearSum+=365;
}
}
YearSum=YearSum-Month_Day_sum1(_year,_month,_day)+Month_Day_sum1(year,month,day);
return YearSum;
}
//=======================================
//主函数
//=======================================
void main(void)
{
int _year,_month,_day;
int year,month,day;
int dayday;
int m=1;
while(1)
{
printf("请输入开始的年,月,日:\n");
scanf("%d%d%d",&_year,&_month,&_day);
printf("请输入结束的年,月,日:\n");
scanf("%d%d%d",&year,&month,&day);
while((_year>year||(_year==year&&_month>month)||(_year==year&&_month==month&&_day>day)))
{
printf("你输入的开始时间与结束时间不正确,请重新输入:\n");
printf("请输入开始的年,月,日:\n");
scanf("%d%d%d",&_year,&_month,&_day);
printf("请输入结束的年,月,日:\n");
scanf("%d%d%d",&year,&month,&day);
}
dayday=NianCha( _year, _month, _day, year, month, day);
printf("%d年%d月%d日到%d年%d月%d日之间的时间差为:\t%d天。\n",_year,_month,_day,year,month,day,dayday);
printf("你是否想继续计算时间间隔?(任意键表示继续,'0'表示结束)\n");
scanf("%d",&m);
if(m==0)
{
break;
}
}
}
//====================================
//全局变量
//====================================
int month_day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
//======================================
//判断闰年
//======================================
int RunNian(int year)
{
if(year%400==0||(year%100!=0&&year%4==0))
{
return 1;
}
else
{
return 0;
}
}
//===================================
//计算当天距年初的天数
//===================================
int Month_Day_sum1(int year,int month, int day)
{
int i;
int Month_Day_sum=0;
if(RunNian(year))
{
month_day[1]=29;
}
else
{
month_day[1]=28;
}
for(i=0;i<month-1;i++)
{
Month_Day_sum+=month_day[i];
}
Month_Day_sum+=day;
return Month_Day_sum;
}
//=====================================
//计算时间差函数
//=====================================
int NianCha(int _year,int _month,int _day,int year,int month,int day)
{
int i,j;
int dayday=0;
int DaySum=0;
int MonthSum=0;
int YearSum=0;
for(i=_year;i<year;i++)
{
if(RunNian(i))
{
YearSum+=366;
}
else
{
YearSum+=365;
}
}
YearSum=YearSum-Month_Day_sum1(_year,_month,_day)+Month_Day_sum1(year,month,day);
return YearSum;
}
//=======================================
//主函数
//=======================================
void main(void)
{
int _year,_month,_day;
int year,month,day;
int dayday;
int m=1;
while(1)
{
printf("请输入开始的年,月,日:\n");
scanf("%d%d%d",&_year,&_month,&_day);
printf("请输入结束的年,月,日:\n");
scanf("%d%d%d",&year,&month,&day);
while((_year>year||(_year==year&&_month>month)||(_year==year&&_month==month&&_day>day)))
{
printf("你输入的开始时间与结束时间不正确,请重新输入:\n");
printf("请输入开始的年,月,日:\n");
scanf("%d%d%d",&_year,&_month,&_day);
printf("请输入结束的年,月,日:\n");
scanf("%d%d%d",&year,&month,&day);
}
dayday=NianCha( _year, _month, _day, year, month, day);
printf("%d年%d月%d日到%d年%d月%d日之间的时间差为:\t%d天。\n",_year,_month,_day,year,month,day,dayday);
printf("你是否想继续计算时间间隔?(任意键表示继续,'0'表示结束)\n");
scanf("%d",&m);
if(m==0)
{
break;
}
}
}