/*
??????????????????????????????????????????????????????????????????????????
????
? CALENDAR
??????????????????????????????????????????????????????????????????????????
????*/
# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <process.h>
static char *month[] = {
"",
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
};
static char *weekday[]={
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
};
main()
{
int mm, yy;
int key;
clrscr();
printf(
"
???????????????????????????????????????????????????");
printf( "
?
?");
printf( "
? Calender ?");
printf( "
? ?");
printf(
"
???????????????????????????????????????????????????
");
printf("
****Press any key****");
getch();
clrscr();
printf("
???????????????????????????????????????????????????");
printf("
?Calender from 01-01-01
to infinity.... ?");
printf("
?Enter In this format: MM YYYY
?");
printf("
?Writen in C language.. ?");
printf("
?Design by: K.E. Joseph ?");
printf("
???????????????????????????????????????????????????
");
l1:
printf("
Please Enter month & year :");
scanf("%d %d", &mm, &yy);
if(mm < 1 || yy<1)
{
printf("
Enter Correct data..
");
goto l1;
}
while(1)
{
clrscr();
display_calendar(mm,
yy);
puts("For
navigation, use the arrow keys.");
puts("To
enter another date, press the Insert key.");
puts("For
RESET press INSERT key");
puts("Press
ESC to exit.");
printf("
???????????????????????????????????????????????????");
printf("
? Design by
:K.Ebenezer Joseph ?");
printf("
???????????????????????????????????????????????????
");
key =
getkey();
switch(key)
{
case
72://up arrow key
yy++;
break;
case
80://down arrow key
yy--;
break;
case
75://left arrow key
if(mm==1)
{
yy--;
mm=12;
}
else
mm--;
break;
case
77://right arrow key
mm++;
if(mm>12)
{
yy++;
mm%=12;
}
if(mm==0)
mm=12;
break;
case
1://escape key
exit(0);
case
82://Insert key
return
main();
}
}
}
display_calendar(int mm, int yy)
{
char i,
days;
char
first_day;
char
lines=1;
first_day=find_day(1,
mm, yy);
printf("
???????????????????????????????????????????????????");
printf("
? %s-%d
?", month[mm],yy);
printf("
???????????????????????????????????????????????????
");
for(i=0;
i<7;i++)
printf("%s ", weekday[i]);
puts("");
for(i=0;
i<first_day;i++)
printf(" ");
//Find
total no. of days in the month
if
(mm!=2)
switch(mm)
{
case
4:
case
6:
case
9:
case
11:
days=30;
break;
default:
days=31;
}
else
isleapyear(yy)?(days=29):(days=28);
for(i=1;i<=days;i++)
{
printf("%d ", i);
if((i+first_day)%7==0)
{
puts("");
lines++;
}
}
puts("
");
switch(lines)
{
case
4:
puts("");
case
5:
puts("");
}
}
find_day(int dd, int mm, int yy)
{
int
odddays, leapyear;
leapyear=isleapyear(yy);
odddays=odddayyear(yy)+odddaymonth(mm,leapyear)+odddayday(dd);
odddays
%= 7;
return
odddays;
}
/*Every 4th year is a leap year, however centuries are not
leap years
except those divisible by 400
for example:
1990 is NOT a leap year
2000 is a leap year
2004 is a leap year
*/
isleapyear(int yy)
{
if(yy%4==0
&& (yy%100!=0 || yy%400==0))
return
1;
else
return
0;
}
//Calculates odd days upto first day of year
odddayyear(int yy)
{
int
odddays=0;
yy-=1;
if(yy>=400)
yy%=400;
if(yy>=100)
{
odddays=yy/100;
odddays
*= 5;
yy%=100;
}
odddays
+= yy;
odddays
+= yy/4;
return
odddays;
}
odddaymonth(int mm, char leapyear)
{
switch(mm)
{
case
1:
return
0;
case
2:
return
3;
case
3:
case
11:
return
leapyear?4:3;
case
4:
case
7:
return
leapyear?0:6;
case
5:
return
leapyear?2:1;
case
6:
return
leapyear?5:4;
case
8:
return
leapyear?3:2;
case
9:
case
12:
return
leapyear?6:5;
case
10:
return
leapyear?1:0;
}
}
odddayday(int dd)
{
return
dd%7;
}
//Returns scan code of the key that has been hit
#include "dos.h"
getkey()
{
union
REGS i, o;
while
(!kbhit())
;
i.h.ah=0;
int86(22,
&i, &o);
return
(o.h.ah);
}
/*******************H I N T S *********************
Every 4th year is a leap year,
however centuries are not leap years except those divisible
by 400
for example:
1990 is NOT a leap year
2000 is a leap year
2004 is a leap year
for example:
-------------
1 ordinary year = 365 days = (52 weeks + 1 day) = 1 odd day
that's
why if this year 23th August is on Tuesday,
it will be on Wednesday next year
1 leap year = 366
days = (52 weeks + 2 days) = 2 odd days
100 years
= 76 ordinary years +
24 leap years
= [(76*52) weeks+76 days] + [(24*52)
weeks+48 days]
= 5200 weeks + 124 days
= 5217 weeks + 5 days
= 5 odd days
200 years = 10
odd days = 3 odd days
300 years = 15
odd days = 1 odd days
400 years = 20
+ 1 odd days = 0 days
800 years = 0
odd days
Sunday is the 0th odd day, Monday the 1st odd day and so
on*/
No comments:
Post a Comment