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

全部博文(119)

文章存档

2014年(1)

2012年(1)

2011年(2)

2010年(22)

2009年(81)

2008年(12)

分类: WINDOWS

2009-11-18 22:58:48

今天看到这么一个题让算下用户输入的年月日来判断是这一年的多少天,要注意闰年的情况,代码如下:
 

#include<iostream>
using namespace std;

struct sDate
{
    unsigned int nYear;
    unsigned int nMouth;
    unsigned int nDay;
};

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

int GetMouth( sDate *p )
{

    int nNum;
    switch ( p ->nMouth )
    {
    case 1:
        if ( 0 == p ->nDay || p ->nDay >= 32 )
        {
            cout<<"一月份天数输入错误了"<<endl; exit(0) ;
        }
        nNum = p ->nDay; break;
    case 2:
     if( true == GetYearType( p ->nYear ) )
        {
            if ( 0 == p ->nDay || p ->nDay >= 30 )
            {
                cout<<"二月份天数输入错误了"<<endl; exit(0) ;
            }
        }
        else
        {
            if ( 0 == p ->nDay || p ->nDay >= 29 )
            {
                cout<<"二月份天数输入错误了"<<endl; exit(0);
            }

        }
        
        nNum = 31 + p ->nDay; break;
    case 3:
        if ( 0 == p ->nDay || p ->nDay >= 32)
        {
            cout<<"三月份天数输入错误了"<<endl; exit(0);
        }
        if( true == GetYearType( p ->nYear ) )
        {
            
            nNum = 31 + 29 + p ->nDay ; break;
        }
        else
        {
            nNum = 31 + 28 + p ->nDay ; break;
        }
    case 4:

        if ( 0 == p ->nDay || p ->nDay >= 31)
        {
            cout<<"四月份天数输入错误了"<<endl; exit(0);
        }
        if( true == GetYearType( p ->nYear ) )
        {
            nNum = 31 + 29 + 31 + p ->nDay ; break;
        }
        else
        {
            nNum = 31 + 28 + 31 + p ->nDay ; break;
        }
    case 5:
        if ( 0 == p ->nDay || p ->nDay >= 32)
        {
            cout<<"五月份天数输入错误了"<<endl; exit(0);
        }
        if( true == GetYearType( p ->nYear ) )
        {
            nNum = 31 + 29 + 31 + 30 + p ->nDay; break;
        }
        else
        {
            nNum = 31 + 28 + 31 + 30 + p ->nDay; break;
        }
        
    case 6:
        if ( 0 == p ->nDay || p ->nDay >= 31)
        {
            cout<<"六月份天数输入错误了"<<endl; exit(0);
        }
        if( true == GetYearType( p ->nYear ) )
        {
            nNum = 31 + 29 + 31 + 30 + 31 + p ->nDay ; break;
        }
        else
        {
            nNum = 31 + 28 + 31 + 30 + 31 + p ->nDay ; break;
        }
        
    case 7:
        if ( 0 == p ->nDay || p ->nDay >= 32)
        {
            cout<<"七月份天数输入错误了"<<endl; exit(0);
        }
        if( true == GetYearType( p ->nYear ) )
        {
            nNum = 31 + 29 + 31 + 30 + 31 + 30 + p ->nDay; break;
        }
        else
        {
            nNum = 31 + 28 + 31 + 30 + 31 + 30 + p ->nDay; break;
        }
        
    case 8:
        if ( 0 == p ->nDay || p ->nDay >= 32)
        {
            cout<<"八月份天数输入错误了"<<endl; exit(0);
        }
        if( true == GetYearType( p ->nYear ) )
        {
            nNum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + p ->nDay ; break;
        }
        else
        {
            nNum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + p ->nDay ; break;
        }
        
    case 9:
        if ( 0 == p ->nDay || p ->nDay >= 31)
        {
            cout<<"九月份天数输入错误了"<<endl; exit(0);
        }
        if( true == GetYearType( p ->nYear ) )
        {
            nNum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + p ->nDay ; break;
        }
        else
        {
            nNum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + p ->nDay ; break;
        }
        
    case 10:
        if ( 0 == p ->nDay || p ->nDay >= 32)
        {
            cout<<"十月份天数输入错误了"<<endl; exit(0);
        }
        if( true == GetYearType( p ->nYear ) )
        {
            nNum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + p ->nDay ; break;
        }
        else
        {
            nNum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + p ->nDay ; break;
        }
        
    case 11:
        if ( 0 == p ->nDay || p ->nDay >= 31)
        {
            cout<<"十一月份天数输入错误了"<<endl; exit(0);
        }
        
        if( true == GetYearType( p ->nYear ) )
        {
            nNum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + p ->nDay; break;
        }
        else
        {
            nNum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + p ->nDay; break;
        }
        
    case 12:
        if ( 0 == p ->nDay || p ->nDay >= 32)
        {
            cout<<"十二月份天数输入错误了"<<endl; exit(0);
        }
        if( true == GetYearType( p ->nYear ) )
        {
            nNum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + p ->nDay; break;
        }
        else
        {
            nNum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + p ->nDay; break;
        }
        
    default:
        {
                cout<<"其他月份不是一年之内的";
        }
    }    
    return nNum;
}


int main()
{
    sDate Date = {0,0,0};
    sDate *P = &Date;
    
    cout<<"请输入年月日:"<<endl;
    cout<<"输入的年为:"<<endl;
    cin>>P->nYear;
    cout<<"输入的月为:"<<endl;
    cin>>P->nMouth;
    cout<<"输入的日为:"<<endl;
    cin>>P->nDay;
    if ( 0 == P->nYear )
    {
        cout<<"年不能为0"<<endl;
    }

    if ( true == GetYearType( P ->nYear ) )
    {
        cout<<"你输入的日期是当年的"<<GetMouth(P) <<"天"<<endl;;
    }
    else
    {
        cout<<"你输入的日期是当年的"<<GetMouth(P)<<"天"<<endl;
    }
    

    return 0;

}


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