Friday 9 August 2013

Draw A Calendar For User Defined Month

This is an extension of THIS program. This time, instead of just telling you the number of days and the first day in a given month, it will also draw a small calendar.

This requires that you include the header myconstdwin.h (Click on the name to find out how to create and use the header).

Please note that among the integers sent to the function
void draw_calendar(int year,char month[15],int start_day,int num_of_days,int x,int y); 
it accepts 2 integers x and y. They are used for the gotoxy co-ordinates. Meaning you can specify where on the output screen(the black DOS screen) the calendar will be drawn. You can send it different months and different co-ordinates to draw the calender for, say, an entire year too.




Here is the code. It's pretty long especially because of the grid :

#include <strings.h>
#include <myconstdwin.h>

void find_days(int month,int year,int *dates,int *day);
void draw_calendar(int year,char month[15],int start_day,int num_of_days,int x,int y);

main()
{
    int month,year,num_of_days,first_day;
    char day[10],month_char[15];

    printf("Enter Month (1 for Jan, 2 for Feb etc.):");
    scanf("%i",&month);
    printf("Enter Year:");
    scanf("%i",&year);
    find_days(month,year,&num_of_days,&first_day);

    switch(first_day)
    {
    case 1:
        strcpy(day,"Sunday");
        break;
    case 2:
        strcpy(day,"Monday");
        break;
    case 3:
        strcpy(day,"Tuesday");
        break;
    case 4:
        strcpy(day,"Wednesday");
        break;
    case 5:
        strcpy(day,"Thursday");
        break;
    case 6:
        strcpy(day,"Friday");
        break;
    case 7:
        strcpy(day,"Saturday");
        break;
    }

    switch(month)
    {
    case 1:
        strcpy(month_char,"January");
        break;
    case 2:
        strcpy(month_char,"February");
        break;
    case 3:
        strcpy(month_char,"March");
        break;
    case 4:
        strcpy(month_char,"April");
        break;
    case 5:
        strcpy(month_char,"May");
        break;
    case 6:
        strcpy(month_char,"June");
        break;
    case 7:
        strcpy(month_char,"July");
        break;
    case 8:
        strcpy(month_char,"August");
        break;
    case 9:
        strcpy(month_char,"September");
        break;
    case 10:
        strcpy(month_char,"October");
        break;
    case 11:
        strcpy(month_char,"November");
        break;
    case 12:
        strcpy(month_char,"December");
        break;
    }
    printf("It has %i days and the first day is a %s (%i)",num_of_days,day,first_day);
    draw_calendar(year,month_char,first_day,num_of_days,2,3);
    getch();
    return 0;
}

void find_days(int month,int year,int *dates,int *day)
{
    int leap_year,loop;
    int loop_day=2,loop_month=1,loop_year=1900,day_limit=31; //day is at 2 because it will start at monday etc.

    while(1)
    {
        /**First, if the month is 2 (february) find if the year is a leap year*/
        if((loop_year%4==0 && loop_year%100!=0) || (loop_year%400==0))
        {
            leap_year=1;
        }
        else
        {
            leap_year=0;
        }

        /**set up the months, with dates going up**/
        if(loop_month==1 || loop_month==3 || loop_month==5 || loop_month==7 || loop_month==8 || loop_month==10 || loop_month==12)
        {
            if(loop_month==month && loop_year==year)
            {
                *dates=31;
                *day=loop_day;
                break;
            }
            for(loop=0;loop<31;loop++)
            {
                loop_day++;
                if(loop_day==8)
                {
                    loop_day=1; //sunday
                }
            }
            loop_month++;
        }
        else if(loop_month==4 || loop_month==6 || loop_month==9 || loop_month==11)
        {
            if(loop_month==month && loop_year==year)
            {
                *dates=30;
                *day=loop_day;
                break;
            }
            for(loop=0;loop<30;loop++)
            {
                loop_day++;
                if(loop_day==8)
                {
                    loop_day=1; //sunday
                }
            }
            loop_month++;
        }
        else if(loop_month==2)
        {
            if(loop_month==month && loop_year==year)
            {
                if(leap_year==1)
                {
                    *dates=29;
                }
                else
                {
                    *dates=28;
                }
                *day=loop_day;
                break;
            }
            for(loop=0;loop<28+leap_year;loop++)
            {
                loop_day++;
                if(loop_day==8)
                {
                    loop_day=1; //sunday
                }
            }
            loop_month++;
        }
        if(loop_month==13)
        {
            loop_year++;
            loop_month=1;
        }
    }
}

void draw_calendar(int year,char month[15],int start_day,int num_of_days,int x,int y)
{
    int loop,a,b,c;
    char day[7][10]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

/**First Line**/
    gotoxy(x,y);
    printf("%c",218);
    for(loop=0;loop<27;loop++)
    {
        printf("%c",196);
    }
    printf("%c",191);

/**Second Line**/
    gotoxy(x,y+1);
    printf("%c",179);
    printf("%s, %i",month,year);
    gotoxy(x+28,y+1);
    printf("%c",179);

/**3rd Line**/
    gotoxy(x,y+2);
    printf("%c",195);
    for(loop=0;loop<7;loop++)
    {
        printf("%c%c%c%c",196,196,196,194);
    }
    printf("\b \b");
    printf("%c",180);

/**4Th line**/
    gotoxy(x,y+3);
    printf("%c",179);
    for(loop=0;loop<7;loop++)
    {
        printf("%s%c",day[loop],179);
    }
    printf("\b \b");
    printf("%c",179);

/**5th Line**/
    gotoxy(x,y+4);
    printf("%c",195);
    for(loop=0;loop<7;loop++)
    {
        printf("%c%c%c%c",196,196,196,197);
    }
    printf("\b \b");
    printf("%c",180);

/**6th line. If first day is 1, it would be x+2, 2 would be x+6 etc.**/
    for(loop=0;loop<=28;loop+=4)
    {
        gotoxy(x+loop,y+5);
        printf("%c",179);
    }

/**7th line**/
    for(loop=0;loop<=28;loop+=4)
    {
        gotoxy(x+loop,y+6);
        printf("%c",179);
    }

/**8th Line**/
    gotoxy(x,y+7);
    printf("%c",195);
    for(loop=0;loop<7;loop++)
    {
        printf("%c%c%c%c",196,196,196,197);
    }
    printf("\b \b");
    printf("%c",180);

/**9th line. If first day is 1, it would be x+2, 2 would be x+6 etc.**/
    for(loop=0;loop<=28;loop+=4)
    {
        gotoxy(x+loop,y+8);
        printf("%c",179);
    }

/**10th line**/
    for(loop=0;loop<=28;loop+=4)
    {
        gotoxy(x+loop,y+9);
        printf("%c",179);
    }

/**11th Line**/
    gotoxy(x,y+10);
    printf("%c",195);
    for(loop=0;loop<7;loop++)
    {
        printf("%c%c%c%c",196,196,196,197);
    }
    printf("\b \b");
    printf("%c",180);

/**12th line. If first day is 1, it would be x+2, 2 would be x+6 etc.**/
    for(loop=0;loop<=28;loop+=4)
    {
        gotoxy(x+loop,y+11);
        printf("%c",179);
    }

/**13th line**/
    for(loop=0;loop<=28;loop+=4)
    {
        gotoxy(x+loop,y+12);
        printf("%c",179);
    }

/**14th Line**/
    gotoxy(x,y+13);
    printf("%c",195);
    for(loop=0;loop<7;loop++)
    {
        printf("%c%c%c%c",196,196,196,197);
    }
    printf("\b \b");
    printf("%c",180);
/**15th line. If first day is 1, it would be x+2, 2 would be x+6 etc.**/
    for(loop=0;loop<=28;loop+=4)
    {
        gotoxy(x+loop,y+14);
        printf("%c",179);
    }

/**16th line**/
    for(loop=0;loop<=28;loop+=4)
    {
        gotoxy(x+loop,y+15);
        printf("%c",179);
    }

/**17th Line**/
    gotoxy(x,y+16);
    printf("%c",195);
    for(loop=0;loop<7;loop++)
    {
        printf("%c%c%c%c",196,196,196,197);
    }
    printf("\b \b");
    printf("%c",180);
/**18th line. If first day is 1, it would be x+2, 2 would be x+6 etc.**/
    for(loop=0;loop<=28;loop+=4)
    {
        gotoxy(x+loop,y+17);
        printf("%c",179);
    }

/**19th line**/
    for(loop=0;loop<=28;loop+=4)
    {
        gotoxy(x+loop,y+18);
        printf("%c",179);
    }

/**20th Line**/
    gotoxy(x,y+19);
    printf("%c",192);
    for(loop=0;loop<7;loop++)
    {
        printf("%c%c%c%c",196,196,196,193);
    }
    printf("\b \b");
    printf("%c",217);

/**The lines for the calendar have been drawn. Now Start putting in the numbers**/
    a=1;
    c=1;
    b=start_day;
    for(loop=x+2;loop<=x+26;loop+=4)
    {
        gotoxy(loop,y+5);
        if(b<=c)
        {
            printf("%i",a);
            a++;
        }
        c++;
    }

    /**Second line of numbers**/
    for(loop=x+2;loop<=x+26;loop+=4)
    {
        gotoxy(loop,y+8);
            printf("%i",a);
            a++;
    }

    /**Third Line Of Numbers**/
    for(loop=x+2;loop<=x+26;loop+=4)
    {
        gotoxy(loop,y+11);
            printf("%i",a);
            a++;
    }

    /**Fourth Line Of Numbers**/
    for(loop=x+2;loop<=x+26;loop+=4)
    {
        gotoxy(loop,y+14);
            printf("%i",a);
            a++;
    }

    /**Fifth Line Of Numbers**/
    for(loop=x+2;loop<=x+26;loop+=4)
    {
        gotoxy(loop,y+17);
        if(a<=num_of_days)
            printf("%i",a);
            a++;
    }

    /**If all the dates did not fit into the grid. Like say in Jan1,2011
    Go back to first line of numbers and fit them in there.**/
    if(a<num_of_days)
    {
        for(loop=x+2;loop<=x+26;loop+=4)
        {
        gotoxy(loop,y+5);
        if(a<=num_of_days)
            printf("%i",a);
            a++;
        }
    }
}



No comments:

Post a Comment