求某个日期是这一年中的第几天

前几天在备赛时看到高斯日记这个问题,现在写了一个求某个日期是当年的第几天,程序很简单,直接上代码。
# include <iostream>
# include <string>
using namespace std;
# define OK 1;
# define ERROR 0;

int IsLeapYear(int Year){
   return ((Year%4==0&&Year%100!=0)||Year%400==0);
}

int DaysofYear(int Year,int Month,int Day){
    //1 3 5 7 8 10 12-31
    //4 6 9 11-30
    //闰年2-29
    //否则2-28
    int DayA[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int DayB[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    if(IsLeapYear(Year)){
        int Days=0;
        if(Month==1){
            Days=Day;
            return Days;
        }
        else{
            for(int Index=0;Index<Month-1;Index++){
                Days=Days+DayB[Index];
            }
            return Days+Day;
        }
    }
    else{
        int Days;
        if(Month==1){
            Days=Day;
            return Days;
        }
        else{
            for(int Index=0;Index<Month-1;Index++){
                Days=Days+DayA[Index];
            }
            return Days+Day;
        }
    }
}

int DateCheck(int Year,int Month,int Day){
    if((Year>=1)&&(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12)&&(Day>=1&&Day<=31)){
        return OK;
    }
    else if((Year>=1)&&(Month==4||Month==6||Month==9||Month==11)&&(Day>=1&&Day<=30)){
        return OK;
    }
    else{
        if((Year>=1)&&IsLeapYear(Year)&&(Day>=1&&Day<=29)){
            return OK;
        }
        else{
            if((Year>=1)&&Day>=1&&Day<=28){
                return OK;
            }
        }
    }
    return ERROR;
}

int main(void){
    int Year,Month,Day;
    bool Flag=true;
    while(Flag){
        cout<<"Enetr the Date as Format:yyyy mm dd:";
        cin>>Year>>Month>>Day;
        if(IsLeapYear(Year)){
            if(DateCheck(Year,Month,Day)){
                cout<<Year<<"-"<<Month<<"-"<<Day<<"is the No "<<DaysofYear(Year,Month,Day)<<endl;
                Flag=false;
            }
            else{
                cout<<"Date format is fault"<<endl;
                Flag=true;
            }
        }
        else{
            if(DateCheck(Year,Month,Day)){
                cout<<Year<<"-"<<Month<<"-"<<Day<<"is the No "<<DaysofYear(Year,Month,Day)<<endl;
                Flag=false;
            }
            else{
                cout<<"Date format is fault"<<endl;
                Flag=true;
            }
        }
    }
    cout<<IsLeapYear(Year)<<endl;
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值