/* Prints a calendar for the given year with every 2 months adjacent Michael Sanders 2024 https://busybox.neocities.org/notes/adjacent-calendars.txt Example... Jan Feb Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat 1 1 2 3 4 5 2 3 4 5 6 7 8 6 7 8 9 10 11 12 9 10 11 12 13 14 15 13 14 15 16 17 18 19 16 17 18 19 20 21 22 20 21 22 23 24 25 26 23 24 25 26 27 28 29 27 28 30 31 */ #include #include #define HEIGHT 6 // matrix height #define WIDTH 7 // matrix width #define OFFSET1 1 // offset 1st calendar #define OFFSET2 25 // offset 2nd calendar const int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; const char *WeekDays[12] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; const char *Months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; int isLeapYear(int year) { return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); } int dayOfMonth(int d, int m, int y) { // function stolen from Kenny McCormack - mucho thanks kind sir if (m < 3) { m += 12; y--; } return (d + (13*m-27)/5 + y + y/4 - y/100 + y/400) % 7; } void popluateMatrix(int year, int month, int calendar[HEIGHT][WIDTH]) { // initialize matrix with zeros for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) calendar[i][j] = 0; } int totalDays = daysInMonth[month - 1]; if (month == 2 && isLeapYear(year)) totalDays++; int day = 1; int firstDay = dayOfMonth(day, month, year); for (int week = 0; week < HEIGHT; week++) { for (int weekday = 0; weekday < WIDTH; weekday++) { if ((week == 0 && weekday < firstDay) || day > totalDays) calendar[week][weekday] = 0; else calendar[week][weekday] = day++; } } } void printTwoCalendars(int year, int month1, int month2) { int cal1[HEIGHT][WIDTH], cal2[HEIGHT][WIDTH]; popluateMatrix(year, month1, cal1); popluateMatrix(year, month2, cal2); printf("%*s%s%*s%s\n", OFFSET1, "", Months[month1 - 1], OFFSET2 + 3, "", Months[month2 - 1]); for (int i = 0; i < OFFSET1; i++) printf(" "); for (int i = 0; i < WIDTH; i++) printf("%s ", WeekDays[i]); for (int i = 0; i < OFFSET2 - OFFSET1 - 3 * WIDTH; i++) printf(" "); for (int i = 0; i < WIDTH; i++) printf("%s ", WeekDays[i]); printf("\n"); for (int i = 0; i < HEIGHT; i++) { int isEmptyRow1 = 1, isEmptyRow2 = 1; for (int j = 0; j < WIDTH; j++) { if (cal1[i][j] != 0) isEmptyRow1 = 0; if (cal2[i][j] != 0) isEmptyRow2 = 0; } if (!isEmptyRow1 || !isEmptyRow2) { for (int j = 0; j < OFFSET1; j++) printf(" "); for (int j = 0; j < WIDTH; j++) { if (cal1[i][j] == 0) printf(" "); else printf("%3d ", cal1[i][j]); } for (int j = 0; j < OFFSET2 - OFFSET1 - 3 * WIDTH; j++) printf(" "); for (int j = 0; j < WIDTH; j++) { if (cal2[i][j] == 0) printf(" "); else printf("%3d ", cal2[i][j]); } printf("\n"); } } } int main() { int year = 2022; printf("%*sCalendar for %d\n\n", OFFSET1, "", year); for (int month = 1; month <= 11; month += 2) { printTwoCalendars(year, month, month + 1); printf("\n"); // separate each pair of months } return 0; } // eof