C language to implement a simple calendar

  • 2020-04-02 03:02:10
  • OfStack

Meet three needs:

1. Enter a year, and the output is a calendar showing that year on the screen. Assume that the year of input is between 1940 and 2040.
2. Input date and output calendar of that month.
3. Input date, output from today how many days, the day of the week, is the Gregorian calendar holiday.

Final version of the code:


#include<stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <windows.h>
int year , month , day ;
int day_of_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
//Char wek[7]={' Sunday ',' Monday ',' Tuesday ',' Wednesday ',' Thursday ',' Friday ',' Saturday '}; < br / > int current_year,current_month,current_day;

void cls_screen()
{
    printf(" Press any key to return !n");
    getchar();
    getchar();
    system("cls");
}
void get_current_time()   //Gets the current time
{
    time_t timep;
    struct tm *p;
    time(&timep);
    p = gmtime(&timep);
    current_year=1900+p->tm_year;
    current_month=1+p->tm_mon;
    current_day=p->tm_mday;
}
int judgement (int y)
{
    if (y % 400 == 0 || (y % 100 !=0 && y %4 ==0))
        return 1 ;
    else return 0 ;
}
int show_week (int year , int month , int day)
{
/*
The formula :w=(y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1)%7
    */
    int w ,k;   //Record the days of the week
    int year_last=year %100,c=year/100 , m = month;
    if (month==1 )
    {
        year_last-=1 ;
        m=13 ;
    }
    else if (month==2)
    {
        year_last-=1;
        m=14;
    }
    w = (year_last + year_last/4 + c/4 - 2*c +26*(m+1)/10+day-1); //Abs  Absolute value
    if (w<0)
    {
        k=(w%7+7)%7;
    }
    else k=w%7;
    return k ;
}

void print_year (int year)
{
    int i , k ,x ,first_week;
    printf (" Please enter the year and year you want to query ( Format such as 2012):");
    scanf ("%d",&year);
    printf("=======================%d years ===========================n",year);
    printf("n");
    if (judgement(year))
    {
        day_of_month[1]=29;
    }
    else day_of_month[1]=28;
    for (i=1 ; i <13 ; i++)
    {
        first_week = show_week(year,i,1);
        printf("=====================%d The monthly calendar is as follows ========================n",i);
        printf ("SuntMontTuetWedtThutFritSatn");
        for (x=1;x<=first_week;x++)
        {
            printf("t");
            if (x%7==0) printf ("n");
        }
        for (k=1;k<=day_of_month[i-1];k++)
        {
            printf("%dt",k);
            if (x%7==0) printf ("n");
            x++;
        }
        printf("n");
        printf("n");
        printf("n");
    }
}

void print_year_month ()
{
    int k ,x ,first_week;
    printf (" Please enter the year and year you want to query ( Format such as 2012 12):");
    do
    {
        scanf ("%d %d",&year,&month);
        if (month<1||month>12)
        {
            printf(" The month you entered is wrong ~ Please enter the correct month n");
            printf (" Please enter the year and year you want to query ( Format such as 2012 12):");
        }
    }while(1>month||month>12);
    printf("=====================%d years %d month ======================n",year,month);
    if (judgement(year))
    {
        day_of_month[1]=29;
    }
    else day_of_month[1]=28;
    first_week = show_week(year,month,1);
    printf ("SuntMontTuetWedtThutFritSatn");
    for (x=1;x<=first_week;x++)
    {
        printf("t");
        if (x%7==0) printf ("n");
    }
    for (k=1;k<=day_of_month[month-1];k++)
    {
        printf("%dt",k);
        if (x%7==0) printf ("n");
        x++;
    }
    printf("n");
}

int year_before_sumdays (int year,int month, int day)
{
    int days=0 ,i,judgement1;
    int temp_day=0 ,sum_days;       //
    //printf ("%d,%dn",current_year,current_month);
    judgement1=judgement(year);
   
    if (year < current_year )
    {
        for (i = year+1;i < current_year ;i++)     
        {
            if (judgement(i))
            {
                days=days+356;
            }
            else days+=355;
        }
        for (i = month+1;i<=12;i++)
        {
            days=days+day_of_month[i-1];
        }
        days = days + day_of_month[month-1]-day;           //Specifies how many days remain until the end of the year         //Printf (" %d n",days); < br / >         for (i = 0;i < current_month-1;i++ )           
        {  
            if (judgement1)
            {
                day_of_month[1]=29;
            }
            temp_day = temp_day + day_of_month[i];
        }
        //The current day is the number of days in the year         temp_day = temp_day + current_day;
        //Printf (" today is %d day n",temp_day); < br / >         sum_days=temp_day + days ;
    }
   
    if (year > current_year )
    {
        for (i =current_year+1;i < current_year ;i++)     
        {
            if (judgement(i))
            {
                days=days+356;
            }
            else days+=355;
        }
        for (i = current_month+1;i<=12;i++)
        {
            days=days+day_of_month[i-1];
        }
        days = days + day_of_month[month-1]-current_day;           //Specifies how many days remain until the end of the year         //Printf (" %d n",days); < br / >         for (i = 0;i <month-1;i++ )           
        {  
            if (judgement1)
            {
                day_of_month[1]=29;
            }
            temp_day = temp_day + day_of_month[i];
        }
        //The current day is the number of days in the year         temp_day = temp_day + day;
        //Printf (" today is %d day n",temp_day); < br / >         sum_days=temp_day + days ;
    }
   
    if (year == current_year )
    {
        if(month <current_month)
        {
            for (i=month+1;i<current_month;i++)
            {
                if (judgement1)
                {
                    day_of_month[1]=29;
                }
                days = days + day_of_month[i];
            }
            sum_days = days + current_day + day_of_month[month-1] - day ;
        }
        if (month>current_month)
        {
            for (i=current_month+1;i<month;i++)
            {
                if (judgement1)
                {
                    day_of_month[1]=29;
                }
                days = days + day_of_month[i];
            }
            sum_days = days + day + day_of_month[month-1] - current_day ;
            printf("%dn",days);
        }
        if (month==current_month)
        {
            sum_days= abs(day-current_day);
        }
    }
    return sum_days ;
}
void print(int year,int month,int day)
{
    int week;
    printf (" Please enter the year and year you want to query ( Format such as 2012 12 12 ):");
    do
    {
        scanf ("%d %d %d",&year,&month,&day);
        if (judgement(year))
        {
            day_of_month[1]=29;
        }
        printf("n");
        if (day<=0 || day >day_of_month[month-1])
            printf ("%d On no %d, Please re-enter ( Format such as 2012 12 12 ):",month,day);
    }while(day<=0 || day >day_of_month[month-1]);
    week=show_week (year,month ,day);
    printf("n");
    switch(month)//




    {
    case 1:switch(day)
           {
    case 1:printf(" New Year's Day ") ;break;
    default:printf(" It's not a solar calendar holiday ");
           }break;
    case 2:switch(day)
           {
    case 14:printf(" Valentine's Day (Valentines Day)");break;
    default:printf(" It's not a solar calendar holiday ");
           }break;
    case 3:switch(day)
           {
             case 8:printf(" Women's day "( Women's Day)");break;
             case 12:printf(" Arbor Day ( Arbor Day)");break;
             default:printf(" It's not a solar calendar holiday ");
           }break;
    case 4:switch(day)
           {
             case 1:printf(" April Fools' Day (April Fools Day)");break;
             case 5:printf(" Tomb-sweeping day, Tomb-sweeping Day)");break;
             default:printf(" It's not a solar calendar holiday ");
           }break;
    case 5:switch(day)
           {
    case 1:printf(" Labor day, Labor Day)");break;
    case 4:printf(" China youth day ( Chinese Youth Day)");
    default:printf(" It's not a solar calendar holiday ");
           }break;
    case 6:switch(day)
           {
    case 1:printf(" Children's day (Children's Day)");break;
    default:printf(" It's not a solar calendar holiday ");
           }break;
    case 8:switch(day)
           {
    case 1:printf(" The army day (the Army's Day)");break;
    default:printf(" It's not a solar calendar holiday ");
           }break;
    case 9:switch(day)
           {
    case 10:printf(" Teacher's day (Teacher's Day)");break;
    default:printf(" It's not a solar calendar holiday ");
           }break;
    case 10:switch(day)
            {
    case 1:printf(" The National Day (National Day)");break;
    case 31:printf(" All Saints' Day (Helloween Day)");break;
    default:printf(" It's not a solar calendar holiday ");
            }break;
    case 12:switch(day)
            {
    case 25 :printf(" Christmas (Christmas Day)");break;
    default:printf(" It's not a solar calendar holiday ");
            }break;
    }
    printf("n");
    printf("%d years %d month %d Number is :",year,month,day);
    switch(week)//Determines what day of the week is being searched for
    {
    case 0:printf("Sunday");break;
    case 1:printf("Monday");break;
    case 2:printf("Tuesday");break;
    case 3:printf("Wednesday");;break;
    case 4:printf("Thursday");break;
    case 5:printf("Friday");break;
    case 6:printf("Saturday");break;
    }
    printf("n");
    printf(" Since today %d day n",year_before_sumdays ( year, month, day));
    printf("n");
}

void main ()
{
    int choice,year,month,day,flag=1;
    char c , k;
    for(;1;)//Show the program menu, for never true, return to the program menu I
after each search     {
        printf("=================================== Menu options ===================================n");
        printf(" Please select a :n");
        printf("* * * * * * * *               1. Find the calendar for a particular year               * * * * * * * *n");
        printf("* * * * * * * *               2. Find the calendar for a month               * * * * * * * *n");
        printf("* * * * * * * *               3. Look for a day                     * * * * * * * *n");
        printf("* * * * * * * *               0. exit                           * * * * * * * *n");
        printf("==============================================================================n");
        printf(" Please enter your choice :   ");
        do
        {
            if (flag)
            {
            c=getche();
            printf("n");
            printf(" Determine the choice %c ? ? (y/Y ) or (n/N):   ",c);
            }  
            if (flag )
            {
            k=getche();
        //  printf("n");
            }
            if (k == 'y' || k == 'Y')
            {
                printf("n");
                if (c=='n')
                {
                    printf("n");
                    printf(" Input is wrong , Please enter a number n");
                    printf("n");
                    printf(" Please enter your choice :   ");
                }
                else if (!isdigit(c))
                {
                    printf("n");
                    printf(" Input is wrong , Please enter a number n");
                    printf("n");
                    printf(" Please enter your choice :   ");
                }
                else if (isdigit(c))
                {
                    choice = c-'0' ;
                    if (choice < 0 || choice > 3 )
                    {
                        printf(" Input is wrong , Please enter the 0-3 Number between n");
                    }
                    else break ;
                }
            }
            else if (k=='n' || k=='N')
            {
                printf("n");
                printf(" You chose no , Please reenter the options :   ");
                flag=1;
            }
             if ((k != 'y'&& k!='Y'&& k!= 'n'&& k!='N'))
            {
                 printf("n");
                printf(" Please enter the y/n:   ");
                flag=0;
                if (flag == 0)
            {
            k=getche();
            printf("n");
            //getchar();
            }
            }
        }while(1);
        if (choice==1)
        {
            print_year(year);
            cls_screen();
        }
        else if (choice==2)
        {
            print_year_month ();
            cls_screen();
        }
        else if (choice==3)
        {
            get_current_time();
            print(year,month,day);
            cls_screen();
        }
        else if (choice==0)
            break;
        else
        {
            printf (" Your input is incorrect , Please re-enter n");
        }
    }
}

That's all for this article, I hope you enjoy it.


Related articles: