Calendars are integral part of our lives. All of us need to remember
dates of scheduled meetings, girlfriend's birth day and of course for
schedules of coding, debugging and testing. Being a developer it always
interests me to develop my own piece of code to generate a calendar,
capable of displaying months-years-date-day etc. (that too in the pretty
format) for next few thousand years. Well, it is not a very very big
mathematics. One golden rule is all you need to know and you are done. I
developed this calendar application in the year 2002 (under C), which
you can easily translate to C++ or any language you love to work in.
I have used legacy C compiler (Turbo) but you may compile the same on
GCC (hopefully) correctly. The application uses Up, Down, Left and Right
arrow keys for navigation of months and years. If you really want to see
the EXE in execution before understanding the code (or copying it)
click here (13.6 KB) to download it off my site.
And the golden rule is: 01-Jan-1900 was Monday and a leap year has of 29
days in February. I have used the default months and years (y and m
variables) as 11 and 1979 respectively, because they are from my
birth-date. If you wish, you may use command line arguments and/or
different months and years too.
Here is the source code:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
enum boolean {false, true};
enum boolean isLeap(int year)
{
if ((year % 4) == 0 && (year % 100 != 0) || (year % 400) == 0)
return true; else
return false; }
getkey()
{
union REGS i,o;
while (!kbhit())
;
i.h.ah = 0;
int86(22, &i, &o);
return(o.h.ah); }
main()
{
int year_arr[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int leap_year_arr[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
char month[12][10] = {"January","February","March","April","May","June",
"July","August","September","October","November","December"};
long days; int y, m; int y1, i, nod; int res, lc, code;
char ch, k;
y = 1979;
m = 11;
do
{
clrscr();
y1 = y;
days = 0;
while ((y - 1) >= 1900)
{
if (isLeap(y - 1) == true)
for (i = 0; i < 12; i++)
days += leap_year_arr[i];
else
for (i = 0; i < 12; i++)
days += year_arr[i];
y--;
}
for (i = 0; i < (m - 1); i++)
{
if (isLeap(y1) == true)
days += leap_year_arr[i];
else
days += year_arr[i];
}
if (isLeap(y1) == true)
nod = leap_year_arr[m - 1];
else
nod = year_arr[m - 1];
res = days % 7;
i = 1;
ch = 218;
printf("%c", ch);
ch = 196;
for (k = 1; k < 37; k++)
printf("%c", ch);
ch = 191;
printf("%c", ch);
ch = 179;
printf("\n%c", ch);
printf(" %10s %d Calendar", month[m-1], y1);
printf("%8c", ch);
ch = 195;
printf("\n%c", ch);
ch = 196;
for (k = 1; k < 37; k++)
printf("%c", ch);
ch = 180;
printf("%c", ch);
ch = 179;
printf("\n%c", ch);
printf(" Mon Tue Wed Thu Fri Sat Sun");
printf("%2c", ch);
ch = 195;
printf("\n%c", ch);
ch = 196;
for (k = 1; k < 37; k++)
printf("%c", ch);
ch = 180;
printf("%c", ch);
ch = 179;
printf("\n%c", ch);
lc = 1;
switch(res)
{
case 1:
printf(" ");
lc += 1;
break;
case 2:
printf(" ");
lc += 2;
break;
case 3:
printf(" ");
lc += 3;
break;
case 4:
printf(" ");
lc += 4;
break;
case 5:
printf(" ");
lc += 5;
break;
case 6:
printf(" ");
lc += 6;
break;
}
while (i <= nod)
{
printf("%5d", i);
lc++;
if (lc == 8)
{
printf("%2c\n", ch);
lc = 1;
}
if (lc == 1)
printf("%c", ch);
i++;
}
for (; lc < 8; lc++)
printf(" ");
printf("%2c", ch);
ch = 192;
printf("\n%c", ch);
ch = 196;
for (k = 1; k < 37; k++)
printf("%c", ch);
ch = 217;
printf("%c", ch);
printf("\n\nCode (c) 2002. Krishna Kumar Khatri.");
printf("\nPress <Esc> to exit...");
code = getkey();
if (code == 72) {
if (y1 == 1900)
y = 9999;
else
y = y1 - 1;
}
else if (code == 80) {
if (y1 == 9999)
y = 1900;
else
y = y1 + 1;
}
else if (code == 75) {
if (m == 1)
m = 12;
else
m -= 1;
y = y1;
}
else if (code == 77) {
if (m == 12)
m = 1;
else
m += 1;
y = y1;
}
else
break;
} while (1);
}
|