这2天看到一个关于计算你一共活了多少天的题目,看了之后写了下代码,感觉要珍惜生命了,呵呵 代码如下:
#include<iostream>
using namespace std;

struct Date
{
        int nYear;
        int nMouth;
        int nDay;
};

bool JudYear(int nYear)
{
        if( (nYear % 4 ==0 && nYear % 100 !=0) || nYear % 400 ==0)
        {
                return true;
        }
        else
        {
                return false;
        }
        
}

bool CheckDate( Date *p)

{    
        int aDay[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int aDay2[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};

        if ( (p ->nDay) >31 || (p ->nDay) <1 || (p -> nMouth) >12 || (p -> nMouth) <1 || (p -> nYear) <1)

        {
                return false;    
        }

                bool isLeapYear = JudYear( (p ->nYear));

                if (isLeapYear)

                {

                        if ( (p ->nDay) > aDay2[(p ->nMouth)])

                        {

                                return false;

                        }

                }

                else

                {

                        if ( (p ->nDay) > aDay[(p ->nMouth)])

                        {

                                return false;

                        }

                }



}

//获得现在年到生日年一共多少天

int GetYear(int nBeforYear,int nCurreYear)    
{
                int nSum = 0;
                int nNum = ( nCurreYear - nBeforYear ) * 365;
                for ( nBeforYear;nBeforYear < nCurreYear;nBeforYear++)
                {
                        if ( JudYear(nBeforYear) )
                        {
                                        nSum += 1;
                        }
                        else
                        {
                                nSum = nSum;
                        }
                }

                return ( nSum + nNum ) ;
}

//获取生日月份是一年中的多少天数

int GetBeforMouth(int nBeforMouth)
{
        int aDay[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int aDay2[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
        int nSum = 0;
        if ( JudYear(nBeforMouth) )
        {
                for ( int i = 1; i < nBeforMouth;i++)
                {
                        nSum += aDay2[i];
                }
        }
        else
        {
                for ( int i = 1; i < nBeforMouth;i++)
                {
                        nSum += aDay2[i];
                }
        }
        return nSum;

}


int main()
{
        Date BeforDate = {0};
        Date NowDate = {0};
        int nYear = 0;
        int nSum = 0;
        cout<<"请输入你的生日,请按年月日来进行输入"<<endl;
        cin>>BeforDate.nYear>>BeforDate.nMouth>>BeforDate.nDay;
        if ( ! CheckDate( &BeforDate) )
        {
                cout<<"输入日期有错误"<<endl;
        }
        cout<<"请输入当前的时间,请按年月日来进行输入"<<endl;
        cin>>NowDate.nYear>>NowDate.nMouth>>NowDate.nDay;
        if ( ! CheckDate( &NowDate ) )
        {
                cout<<"输入日期有错误"<<endl;
        }
        
        cout<<endl;
        nYear = GetYear(BeforDate.nYear,NowDate.nYear);
        nSum = nYear - GetBeforMouth(BeforDate.nMouth) + GetBeforMouth(NowDate.nMouth) - BeforDate.nDay + NowDate.nDay;
        cout<<"一共活了"<<nSum<<"天"<<endl;

        return 0;
}