C 언어에서 날짜를 입력하여 요일을 계산하는 함수

19965 단어 C 언어
#include<stdio.h>

#define CURRENT_YEAR 2015

#define CURRENT_MON  6

#define CURRENT_DAY  21

enum month{Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};

int year;        //     

enum month mon;    //     

int day;        //    

int total_yday=0;    //      

int total_mday=0;    //       

int total_day =0;    //     

int plus_mday[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//      

int i,j;

void greater_date()    //      

{

    total_yday=0;    //        

    total_mday=0;    //         

    total_day =0;    //       



    for(i=CURRENT_YEAR+1;i<year;i++)//      

    {

        if((i%4==0 && i%100!=0) || (i%400 == 0))

            total_yday=total_yday+366;

        else

            total_yday=total_yday+365;

    }





    if((CURRENT_YEAR%4==0 && CURRENT_YEAR%100!=0) || CURRENT_YEAR%400 ==0)//          

        plus_mday[2]=29;



    for(j=CURRENT_MON+1;j<13;j++)    //           

    {

        total_mday=total_mday+plus_mday[j];    

    }

    total_mday=total_mday+plus_mday[CURRENT_MON]-CURRENT_DAY;    //             total_mday,tot

    plus_mday[2]=28;    //        28



    if((year%4==0 && year%100!=0) || year%400==0)        //           

        plus_mday[2]=29;

    for(j=1;j<mon;j++)

    {

        total_mday=total_mday+plus_mday[j];

    }

    total_mday=total_mday+day;

    total_day=total_yday+total_mday;



    total_day=total_day%7;

}



void smaller_date()    //      

{

    total_yday=0;    //        

    total_mday=0;    //         

    total_day =0;    //       



    for(i=year+1;i<CURRENT_YEAR;i++)

    {

        if((i%4==0 && i%100!=0) || (i%400 == 0))

            total_yday=total_yday+366;

        else

            total_yday=total_yday+365;

    }



    if((year%4==0 && year%100!=0) || year%400==0)        //           

        plus_mday[2]=29;



    for(j=mon+1;j<13;j++)    //         

    {

        total_mday=total_mday+plus_mday[j];    

    }

    total_mday=total_mday+plus_mday[mon]-day;    //             total_mday,tot

    plus_mday[2]=28;    //        28



    if((CURRENT_YEAR%4==0 && CURRENT_YEAR%100!=0) || CURRENT_YEAR%400 ==0)    //          

        plus_mday[2]=29;



    for(j=1;j<CURRENT_MON;j++)    //              

    {

        total_mday=total_mday+plus_mday[j];

    }

    total_mday=total_mday+CURRENT_DAY;

    total_day=total_yday+total_mday;



    total_day = (7-total_day%7)%7;

}



void equal_date()    //         

{

    total_yday=0;    //        

    total_mday=0;    //         

    total_day =0;    //       



    if(mon<CURRENT_MON)//        

    {

        for(i=mon+1;i<CURRENT_MON;i++)

        {

            total_mday=total_mday+plus_mday[i];

        }

        total_mday=total_mday+plus_mday[mon]-day+CURRENT_DAY;

        total_day=total_mday;

        total_day=(7-total_day%7)%7;

    }

    if(mon>CURRENT_MON)    //        

    {

        for(i=CURRENT_MON+1;i<mon;i++)

        {

            total_mday=total_mday+plus_mday[i];

        }

        total_mday=total_mday+plus_mday[CURRENT_MON]-CURRENT_DAY+day;

        total_day=total_mday;

        total_day=total_day%7;

    }

    if(mon==CURRENT_MON)

    {

        if(day>=CURRENT_DAY)

        {

            total_day=day-CURRENT_DAY;

            total_day%=7;

        }

        if(day<CURRENT_DAY)

        {

            total_day=CURRENT_DAY-day;

            total_day=(7-total_day%7);

        }

    }

}



void get_date()    //    

{

    year=0;

    day=0;

    mon=0;



    printf("Input year:");

    scanf("%d",&year);

    int i,j;

    getchar();

    while(1)    //get month

    {

        printf("Input month:");

        scanf("%d",(int*)&mon);

        getchar();

        if(mon>0&&mon<=12)

            break;

    }

    while(1)    //get day

    {

        printf("Input day:");

        scanf("%d",&day);

        getchar();

        if(day>0&&day<=31)

            break;

    }



}



void output_weekday()

{

        switch(total_day)

        {

            case 1:

                printf("%d %d %d     
",year,mon,day); break; case 2: printf("%d %d %d
",year,mon,day); break; case 3: printf("%d %d %d
",year,mon,day); break; case 4: printf("%d %d %d
",year,mon,day); break; case 5: printf("%d %d %d
",year,mon,day); break; case 6: printf("%d %d %d
",year,mon,day); break; case 0: printf("%d %d %d
",year,mon,day); break; default: break; } } int main() { while(1) { get_date(); if(year>CURRENT_YEAR) greater_date(); if(year<CURRENT_YEAR) smaller_date(); if(year==CURRENT_YEAR) equal_date(); output_weekday(); } }

좋은 웹페이지 즐겨찾기