求c++计算日期算法

来源:百度知道 编辑:UC知道 时间:2024/06/27 10:04:45
设计一个程序能计算日期的间隔,如输入两个日期别为2008-2-3 和 2008-3-9 计算相隔多少天,或2008-2-3 加上100天后的日期是多少。

我想知道算法思想,程序有最好

//不考虑闰年 (之前有误,以此为准)
#include <iostream>
using namespace std;
void main()
{
int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int y1,y2,m1,m2,d1,d2,temp;
int sum1=0;
int sum2=0;
int sum=0;
int i;
int j;
cout<<"请依次按年月日输入第一个日期:"<<endl;
cin>>y1>>m1>>d1;
cout<<"请依次按年月日输入第二个日期:"<<endl;
cin>>y2>>m2>>d2;
if(y1>y2)//让第二个日期为大的日期
{
temp=y1;y1=y2;y2=temp;
temp=m1;m1=m2;m2=temp;
temp=d1;d1=d2;d2=temp;
}
else
if(y1==y2)
{
if(m1>m2)
{
temp=y1;y1=y2;y2=temp;
temp=m1;m1=m2;m2=temp;
temp=d1;d1=d2;d2=temp;
}
else
if(d1>d2)
{
temp=y1;y1=y2;y2=temp;
temp=m1;m1=m2;m2=temp;
temp=d1;d1=d2;d2=temp;
}
}
for(i=1;i<=m1;i++)
{
sum1+=mon[i];
}
sum1=sum1-(mon[m1