计算相差几天

#include<iostream>
#include<cmath>
using namespace std;
struct Date
{
    int year;
    int month;
    int day;
} date1;
int main()
{
    Date date;
    Date date1;
      int days3=0,days4=0,days5;
        int l,m;
    cout<<"input year,month,day:";
    cin>>date.year>>date.month>>date.day;
    cin>>date1.year>>date1.month>>date1.day;
    if(abs(date.year-date1.year)>0)
    {
        int days=0;
        int i;
        if(date.month==1)
            days=days+date.day;
        else
            for(i=1; i<date.month; i++)
            {
                if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                    days=days+31;
                else if(i==4||i==6||i==9||i==11)
                    days=days+30;
                else if(i==2)
                    days=days+29;
            }
        days=days+date.day;
        if(date.month>=3&&(date.year%4!=0||date.year%400!=0))
            days=365-days+1;
        else
            days=365-days;
        cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"<<days<<"天."<<endl;
        int days1=0;
        int k;
        if(date1.month==1)
            days1=days1+date.day;
        else
            for(k=1; k<date1.month; k++)
            {
                if(k==1||k==3||k==5||k==7||k==8||k==10||k==12)
                    days1=days1+31;
                else if(k==4||k==6||k==9||k==11)
                    days1=days1+30;
                else if(k==2)
                    days1=days1+29;
            }
        days1=days1+date.day;
        if(date.month>=3&&(date.year%4!=0||date.year%400!=0))
            days1=days1-1;
        int j,z=0,days2=0;
        days2=days2+(abs(date.year-date1.year)-1)*365;
        for(j=0; j<abs(date.year-date1.year); j++)
        {
            if((date.year+1+j)%4==0||date.year%400==0)
                z++;
        }
        days2=days+days1+z+days2;
        cout<<"相差"<<days2<<"天"<<endl;
    }
    else
        {
            if(date.month==1)
            days3=days3+date.day;
        else
            for(l=1; l<date.month; l++)
            {
                if(l==1||l==3||l==5||l==7||l==8||l==10||l==12)
                    days3=days3+31;
                else if(l==4||l==6||l==9||l==11)
                    days3=days3+30;
                else if(l==2)
                    days3=days3+29;
            }
        days3=days3+date.day;
        if(date.month>=3&&(date.year%4!=0||date.year%400!=0))
        days3=days3-1;
        if(date1.month==1)
            days4=days4+date1.day;
        else
            for(m=1; m<date1.month; m++)
            {
                if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
                    days4=days4+31;
                else if(m==4||m==6||m==9||m==11)
                    days4=days4+30;
                else if(m==2)
                    days4=days4+29;
            }
        days4=days4+date1.day;
        if(date1.month>=3&&(date1.year%4!=0||date1.year%400!=0))
        days4=days4-1;
        days5=abs(days4-days3);
        cout<<"相差"<<days5<<"天";
}
        return 0;
}
<img src="http://img.blog.csdn.net/20141228225538825?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVhbmd5b25nMDAw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="http://img.blog.csdn.net/20141228225647546?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVhbmd5b25nMDAw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

你可能感兴趣的:(计算相差几天)