原创:计算两日期相差的天数
欢迎大家测试本人的代码,测试方法:用本人的代码算出相差的天数 然后去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;
case11: 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;
case11: nTotalDdays += nDay+334;
break;
default:
break;
}
}
return nTotalDdays;
}
编译份给我吧我老婆问我我们认识多少天了我算也算不出 编译份给我吧我老婆问我我们认识多少天了我算也算不出
Tale 发表于 2009-6-27 15:55 http://www.52pojie.cn/images/common/back.gif
呵呵,满足你的要求,自己下载 显然,楼主的工作只是个体力活儿(公历的日期总是简单的).
楼主要是能弄可以计算农历日期之差的......
呵呵 那就厉害了 记得以前用C语言写过类似的题目 看看拿来玩下!
页:
[1]