欢迎大家测试本人的代码,测试方法:用本人的代码算出相差的天数 然后去http://www.ehs.washington.edu/rso/calculator/activity_calc.shtm计算天数之差
见附件二:在date1与date2输入两日期点calculate即可都算出相差的天数,日期输入格式为:月/日/年#include<iostream>
int TotalDays(int nYear,int nMonth,int nDay);
using namespace std;
int main()
{
int i = 0;
int nYear1 ,nYear2 = 0;
int nMonth1,nMonth2 = 0;
int nDay1 ,nDay2 = 0;
Input :
cout<<"第一个日期年份:";
cin>>nYear1;
cout<<"第一个日期月份:";
cin>>nMonth1;
cout<<"第一个日期天:";
cin>>nDay1;
cout<<"第二个日期年份:";
cin>>nYear2;
cout<<"第二个日期月份:";
cin>>nMonth2;
cout<<"第二个日期天:";
cin>>nDay2;
int nSubDate = 0;
if(( nMonth1 <= 12)&&( nDay1 <= 31)&&( nMonth2 <= 12 )&&( nDay2 <= 31))
{
nSubDate = TotalDays( nYear1, nMonth1, nDay1)-TotalDays( nYear2,nMonth2,nDay2);//调用nYear年nMoth月nDay日总天数计算函数
cout<<nYear1<<"年"<<nMonth1<<"月"<<nDay1<<"日与" <<nYear2<<"年"<<nMonth2<<"月"<<nDay2<<"日相差";
cout<<nSubDate<<"天"<<endl;
}
else
{
cout<<"输入不合法,请重新输入!";
goto Input;
}
return 0;
}
int TotalDays(int nYear,int nMonth,int nDay) //nYear年nMoth月nDay日总天数计算函数
{
int nTotalDdays = 0; //总天数
int nYearBase = 0;
int i = 0;
for( nYearBase= 0; nYearBase<=nYear-1 ; nYearBase++) //判断从(nYear-1年有多少天)
{
if( (nYearBase%4 == 0) && (nYearBase%100!=0)||(nYearBase%400 == 0) ) //闰年有多少年
{
i ++;//累积闰年的个数
}
}
nTotalDdays = i*366+(nYear-1-i)*365; //闰年天数+非闰年天数
if( (nYear%4 == 0 ) && ( nYear%100!= 0 )||( nYear%400 == 0 )) //nYear年是闰年:从nYear年1月1日到 nMonth月nDay日共有多少天
{
switch( nMonth-1)
{
case 0: nTotalDdays += nDay; //一月nDay日的天数
break;
case 1: nTotalDdays += nDay+31; //二月nDay日的天数
break;
case 2: nTotalDdays += nDay+60;
break;
case 3: nTotalDdays += nDay+91;
break;
case 4: nTotalDdays += nDay+121;
break;
case 5: nTotalDdays += nDay+152;
break;
case 6: nTotalDdays += nDay+182;
break;
case 7: nTotalDdays += nDay+213;
break;
case 8: nTotalDdays += nDay+244;
break;
case 9: nTotalDdays += nDay+273;
break;
case 10: nTotalDdays += nDay+305;
break;
case 11: nTotalDdays += nDay+335;
break;
default:
break;
}
}
else //nYear年非闰年:、/nYear年是闰年:从nYear年1月1日到 nMonth月nDay日共有多少天
{
switch( nMonth-1)
{
case 0: nTotalDdays+= nDay;
break;
case 1: nTotalDdays += nDay+31;
break;
case 2: nTotalDdays += nDay+59;
break;
case 3: nTotalDdays += nDay+90;
break;
case 4: nTotalDdays += nDay+120;
break;
case 5: nTotalDdays += nDay+151;
break;
case 6: nTotalDdays += nDay+181;
break;
case 7: nTotalDdays += nDay+212;
break;
case 8: nTotalDdays += nDay+243;
break;
case 9: nTotalDdays += nDay+273;
break;
case 10: nTotalDdays += nDay+304;
break;
case 11: nTotalDdays += nDay+334;
break;
default:
break;
}
}
return nTotalDdays;
}
|