C Language Calendar Printing: Horizontal and Vertical Versions

Creating two versions of a calendar program with different printing formats.

Thanks to Hao Yingjun for his guidance. The calendar programming done by Hao Yingjun outputs the calendar by either going backward or forward based on fixed dates. My implementation is based on the premise that January 1st of the year is a Monday, using a formula to calculate and then output the print.

void calender_1() {    int year, baseweek;    int  i, day = 1, n = 0;    int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };    printf("Input year\r\n");    scanf("%d", &year);    baseweek = (5 * (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7 + 1; // Determine the first day of the input year.    days[2] = 28 + (((year % 100 == 0) && (year % 400 == 0)) || ((year % 100) && (year % 4 == 0))); // Calculate leap years: leap every four years, not leap in a century, or leap if divisible by 400.    // printf(" %d ", baseweek); // Check if correct.    do    {        n++;  // Month        printf("\r\n-------------------\r\n");        printf("   %d year %d month", year, n);        printf("\n-------------------\r\n");        printf("Sun Mon Tue Wed Thu Fri Sat\r\n");        printf("-------------------\n");        day = 1;        for (i = 1; i < 8; i++)        {            if (day > days[n]) break;            if (i == (baseweek - 1) % 7 + 1) // Key: day + 1, week number also + 1.            {                printf("%2d ", day);                day++;                baseweek++;            }            else printf("   ");            if (i == 7)            {                i = 0; // After inputting 7 times, change line. Reset i to 0 and increment from 1.                printf("\n");            }        }    } while (n < 12);}

The output result is as follows:

C Language Calendar Printing: Horizontal and Vertical Versions

#include<windows.h>void gotoxy(int x, int y) // Cursor positioning function {    COORD p; // Define structure variable p    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get current function handle    p.X = x; p.Y = y; // Pass the target position of the cursor to the structure    if (!SetConsoleCursorPosition(handle, p)) // Move cursor        printf("Cursor setting error");}
void calender_2() {    int year, baseweek;    int  i, day = 1, n = 0;    int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };    printf("Input year\r\n");    scanf("%d", &year);    baseweek = (5 * (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7 + 1; // Determine the first day of a given year.    days[2] = 28 + (((year % 100 == 0) && (year % 400 == 0)) || ((year % 100) && (year % 4 == 0)));    int set_y[13] = { 0,0,0,0,1,1,1,2,2,2,3,3,3 };   // For months 1, 2, 3, print in y=11*0+2; for months 4, 5, 6, print in y=1*11+2.    int set_x[13] = { 0,0,1,2,0,1,2,0,1,2,0,1,2 };  // For months 1, 2, 3, print in x=23*0; for months 4, 5, 6, print in x=23*1.    int base = 2;  // This indicates the second row in the y-coordinate system; y=0 prints "Output Year"; y=1 inputs year.    do    {        n++;        gotoxy(set_x[n] * 23, set_y[n] * 11 + base++); printf("-------------------");        gotoxy(set_x[n] * 23, set_y[n] * 11 + base++);   printf("   %d year %d month", year, n);        gotoxy(set_x[n] * 23, set_y[n] * 11 + base++);   printf("-------------------");        gotoxy(set_x[n] * 23, set_y[n] * 11 + base++);   printf("Sun Mon Tue Wed Thu Fri Sat");        gotoxy(set_x[n] * 23, set_y[n] * 11 + base++);   printf("-------------------");        gotoxy(set_x[n] * 23, set_y[n] * 11 + base++);        day = 1;        for (i = 1; i < 8; i++)        {            if (day > days[n]) break;            if (i == (baseweek - 1) % 7 + 1) // Key: day + 1, week number also + 1.            {                printf("%2d ", day);                day++;                baseweek++;            }            else printf("   ");            if (i == 7)            {                i = 0; // After inputting 7 times, change line. Reset i to 0 and increment from 1.                gotoxy(set_x[n] * 23, set_y[n] * 11 + base++);            }        }        base = 2;    } while (n < 12);}

C Language Calendar Printing: Horizontal and Vertical VersionsUse this function gotoxy () to set the cursor output position. This function is from the <windows.h> library. The parameters x and y control the console’s buffer size, which limits the cursor’s position. Otherwise, it would look like this.C Language Calendar Printing: Horizontal and Vertical Versions

Leave a Comment