Year Calender in C. Compiler Turbo C

Luckychap 0 Tallied Votes 362 Views Share

A simple graphical year calendar.
Compiler: Turbo C

In main current year is set manually, for more generic way use date function to get current year.

/* Developed by:   Vivek Kumar (vivek_kumar_bt@yahoo.co.in)
 * Description:    A simple Year Calender
 * Last Modified:  Today :D
 */

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<string.h>
#include<stdlib.h>
#include<dos.h>
#define SIZE 112
int IsLeap(int );
int FirstDay(int);
int DrawMonth(int X,int Y,int monthno,int fday);
void DrawCld(int year);
int getkey();
char *MONTH[]={"January","Feburary","March","April","May","June","July","August","September","October","November","December"};
int Monlength[]={31,28,31,30,31,30,31,31,30,31,30,31};
char *DAY[7]={"Monday","Teusday","Wednesday","Thursday","Friday","Saturday","Sunday"};
main()
{
 int year ;
 int gd=DETECT,gm;
 int ch;
 year=2009;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 DrawCld(year);
 while(1)
 {
  ch=getkey();
  fflush(stdin);
  switch(ch)
  {
   case 75:
    year--;
    if(year==0)
     year=1;
    DrawCld(year);
    break;
   case 77:
    year++;
    DrawCld(year);
    break;
   case 1:
    exit(0);
   default :
   sound(900);
   delay(1000);
   nosound();
   // printf("\a");
  }
 }
 closegraph();
 restorecrtmode();
 return(0);
}

int IsLeap(int Y)
{
 if(Y%100==0)
 {
   if(Y%400==0)
    return(1);
   else
    return(0);
 }
 else
 {
  if(Y%4==0)
   return(1);
  else
   return(0);
 }
}

int FirstDay(int Y)
{
 int firstday,leapdays;
 long int normaldays,totaldays;
 normaldays=(Y-1)*365L;
 leapdays=(Y-1)/4-(Y-1)/100+(Y-1)/400;
 totaldays=normaldays+leapdays;
 firstday=totaldays%7;
 return(firstday);
}

int DrawMonth(int X,int Y,int monthno,int fday)
{
 int i,j,isleap;
 char str[3];
 str[1]='\0';
 setfillstyle(1,7);
 bar(X,Y+SIZE/5,X+SIZE,Y+SIZE/5+SIZE);
 setfillstyle(1,4);
 bar(X,Y,X+SIZE,Y+SIZE/5);
 setcolor(15);
 settextstyle(0,0,0);
 settextjustify(CENTER_TEXT,CENTER_TEXT);
 outtextxy(X+SIZE/2,Y+(SIZE/5)/2,MONTH[monthno]);
 setcolor(8);
 rectangle(X,Y+SIZE/5,X+SIZE,Y+SIZE/5+SIZE);

 for(j=1;j<=7;j++)
 {
  setcolor(8);
  line(X,Y+SIZE/5+(SIZE/7)*j,X+SIZE,Y+SIZE/5+(SIZE/7)*j);
  line(X+(SIZE/7)*j,Y+SIZE/5,X+(SIZE/7)*j,Y+SIZE/5+SIZE);
 }
 for(j=0;j<7;j++)
 {
  setfillstyle(1,1);
  floodfill(X+(SIZE/7)*j+(SIZE/7)/2,Y+SIZE/5+(SIZE/7)/2,8);
  if(j==6)
   setcolor(4);
  else
   setcolor(YELLOW);

  str[0]=DAY[j][0];
  outtextxy(X+(SIZE/7)*j+(SIZE/7)/2,Y+SIZE/5+(SIZE/7)/2,str);
 }
 int d=1,flag=0;
 for(i=1;i<=7;i++)
 {
  for(j=0;j<7;j++)
  {
   if(fday==j)
    flag=1;
   if(d>Monlength[monthno])
    return(j);
   if(flag==1)
   {
    itoa(d,str,10);
    settextstyle(2,0,SIZE/25);
    if(j==6)
     setcolor(4);
    else
     setcolor(0);
    outtextxy(X+(SIZE/7)*j+(SIZE/7)/2,Y+SIZE/5+(SIZE/7)*i+(SIZE/7)/2,str);
    d++;
   }
  }
 }
}

void DrawCld(int year)
{
 int leap,firstday,lastday,mon,mul=0;
 int X=10,Y=30,GAP=10;
 char str[10],str1[20]="YEAR-";

 leap=IsLeap(year);
 if(leap==1)
 {
  Monlength[1]=29;
 }
 else
 {
  Monlength[1]=28;
 }
 firstday=FirstDay(year);
 itoa(year,str,10);
 strcat(str1,str);
 setfillstyle(1,2);
 bar(0,0,getmaxx(),getmaxy());
 settextstyle(7,0,1);
 setcolor(1);
 settextjustify(CENTER_TEXT,CENTER_TEXT);
 outtextxy(getmaxx()/2,Y/2,str1);
 for(mon=0;mon<12;mon++)
 {
  if((X+(SIZE+GAP)*mul)>=getmaxx()-(SIZE+GAP))
  {
   Y=Y+SIZE+GAP+SIZE/5;
   X=10;
   mul=0;
  }
  lastday=DrawMonth(X+(SIZE+GAP)*mul,Y,mon,firstday);
  firstday=lastday;
  mul++;
 }
 X=400;
 Y=400;
 setcolor(0);
 outtextxy(X,Y,"Press RIGHT ARROW to see next year");
 outtextxy(X,Y+10,"Press LEFT ARROW to see previous year");
 outtextxy(X,Y+20,"Press ESCAPE to EXIT");
}
int getkey()
{
 union REGS ik,ok;
 ik.h.ah=0;
 int86(0x16,&ik,&ok);
 return(ok.h.ah);
}
santlaha 0 Newbie Poster
printf("nice job");
santlaha 0 Newbie Poster
hello
santlaha 0 Newbie Poster

after a long time .
how are you bro?

santlaha 0 Newbie Poster

df

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.