Chinaunix首页 | 论坛 | 博客
  • 博客访问: 936155
  • 博文数量: 119
  • 博客积分: 6248
  • 博客等级: 准将
  • 技术积分: 1419
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-08 14:14
文章分类

全部博文(119)

文章存档

2014年(1)

2012年(1)

2011年(2)

2010年(22)

2009年(81)

2008年(12)

分类: LINUX

2009-11-23 11:17:30

这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;
}


阅读(2245) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~

wiliiwin2012-10-13 19:06:21

xbbHistory: 代码真不怎么好。。。。.....
请指教

xbbHistory2012-09-15 23:41:58

代码真不怎么好。。。。

给个理由先2009-11-27 09:33:19

用儒略日分式简单很多 求出给定年(I),月(J),日(K)的儒略日: 儒略日= K - 32075 + 1461 * (I + 4800 + (J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100)/4

chinaunix网友2009-11-27 00:50:56

#Power shell写的,计算你已经活了多少天,呵呵, echo "日期格式 yyyy-mm-dd" $myinput=read-host $myliveday=([datetime]::Now - [datetime]$myinput).Days echo " $myinput 出生的这位朋友你好,你已经活了 $myliveday 天!"