Friday, October 30, 2009

Chap9[F]e Creating a dynamic calendar

Develop a program that receives the month and year from the keyboard as integers and prints the calendar in the following format.



Note that according to the Gregorian calendar 01/01/1900 was Monday. With this as the base the calendar should be generated.
Modify the above program suitably so that once the calendar for a particular month and year has been displayed on the screen, then using arrow keys the user must be able to change the calendar in the following manner: Up arrow key : Next year, same month Down arrow key : Previous year, same month Right arrow key : Same year, next month Left arrow key : Same year, previous month If the escape key is hit then the procedure should stop. Hint: Use the getkey( ) function discussed in Chapter 8, problem number [L](c).




#include "dos.h"

void main()
{
int f=0,year,month,mod,i,j,k,m;
char z[1];

clrscr();
while(1)
{
if(f==0)
{ gotoxy(20,2);
printf("\nKey in the year");
scanf("%d",&year);
gotoxy(20,4);
printf("\nKey in the month(digits)");
scanf("%d",&month);
clrscr();
if(year<1900||year>3500)
{
printf("\nCalendar out of range");
continue;
}
if(month>12||month<1)
{
printf("\nInvalid month");
continue;
}
creategrid();
header(year,month);
f=1;
}


if(year<1900)
break;

mod=dayofmonth(year,month);

if(mod==0)
mod=7;

j=daysinmonth(year,month);


for(i=1,k=mod;i<=j;i++,k++)
{
gotoarr(k);
printf("%d",i);
}


m=getkey();

if(m==72)
year=year+1;

if(m==80)
year=year-1;

if(m==75)
month=month-1;

if(m==77)
month=month+1;

if(month==13)
{
month=1;
year=year+1;
}

if(month==0)
{
month=12;
year=year-1;
}

if(year<1900)
break;

clrscr();
creategrid();
header(year,month);


}
}

creategrid()
{
int i;

for(i=17;i<=59;i++)
{
if(i==17)
{

gotoxy(i,1);
printf("%c",218);
gotoxy(i,22);
printf("%c",192);
}
else if(i==59)
{
gotoxy(i,1);
printf("%c",191);
gotoxy(i,22);
printf("%c",217);
}
else
{
gotoxy(i,1);
printf("%c",196);
gotoxy(i,22);
printf("%c",196);
}
}

for(i=20;i<=56;i++)
{
if(i==20)
{
gotoxy(i,4);
printf("%c",218);
gotoxy(i,21);
printf("%c",192);
}
else if(i==56)
{
gotoxy(i,4);
printf("%c",191);
gotoxy(i,21);
printf("%c",217);
}
else
{
gotoxy(i,4);
printf("%c",196);
gotoxy(i,21);
printf("%c",196);
}
}

for(i=2;i<=21;i++)
{
gotoxy(17,i);
printf("%c",179);
gotoxy(59,i);
printf("%c",179);
}

for(i=5;i<=20;i++)
{
gotoxy(20,i);
printf("%c",179);
gotoxy(56,i);
printf("%c",179);
}

gotoxy(22,7);
printf("Mon");

gotoxy(27,7);
printf("Tue");

gotoxy(32,7);
printf("Wed");

gotoxy(37,7);
printf("Thu");

gotoxy(42,7);
printf("Fri");

gotoxy(47,7);
printf("Sat");

gotoxy(52,7);
printf("Sun");

gotoxy(21,23);
printf("%c=Next year",30);

gotoxy(39,23);
printf("%c=Previous year",31);

gotoxy(21,24);
printf("%c=Next month",16);

gotoxy(39,24);
printf("%c=Previous month",17);

}

getkey()
{
union REGS i,o;
while(!kbhit())
;
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}

header(int year,int month)
{
int len;
char *m;

switch(month)
{
case 1:
m="January";
break;

case 2:
m="February";
break;

case 3:
m="March";
break;

case 4:
m="April";
break;

case 5:
m="May";
break;

case 6:
m="June";
break;

case 7:
m="July";
break;

case 8:
m="August";
break;

case 9:
m="September";
break;

case 10:
m="October";
break;

case 11:
m="November";
break;

case 12:
m="December";
break;
}

len=strlen(m)+5;
len=len/2;
gotoxy(38-len,5);
printf("%s %d",m,year);
}

dayofmonth(int year,int month)
{
int leap,mod,check=0,i,diff;
float days;
long days1;

if((year%4==0&&year%100!=0)||year%400==0)

check=1;


year=year-1;
diff=year-1900;
leap=(year-1900)/4-(year-1900)/100+((year/400)-4);
days=((diff-leap)*365.0)+(leap*366.0)+365+1;


for(i=1;i<=12;i++)
{
if(i==month+1)
break;

else if(i==1)
continue;

else if(i==3)
{
if(check==0)
days=days+28;

else if(check==1)
days=days+29;
}

else if(i<9)
{
if(i%2==0)
days=days+31;

else
days=days+30;
}

else if(i==9)
days=days+31;

else if(i>9)
{
if(i%2==0)
days=days+30;

else
days=days+31;
}
}

days1=days;

mod=days1%7;
return(mod);
}

daysinmonth(int year,int month)
{
int days;
if(((year%4==0&&year%100!=0)||year%400==0)&&month==2)
days=29;

else if(month==2)
days=28;

else if(month<8)
{
if(month%2==0)
days=30;
else
days=31;
}

else if(month==8)
days=31;

else if(month>8)
{
if(month%2==0)
days=31;
else
days=30;
}

return(days);
}

gotoarr(int i)
{
int row,col,x,y;

row=((i-1)/7)+1;

if(i<=7)
i=i+7;

col=i%7;

if(col==0)
col=7;

y=7+(2*row);
x=17+(5*col);
gotoxy(x,y);
}

No comments:

Post a Comment