Calendar project in c and c++ Solved

In this project, we design calendar with graphics using C language under Turbo C++ compiler. Calendar designed here has many features like display current time, jumping to a particular date, to a particular month, check day on a particular date etc.
Here we use Turbo C++ compiler because it support both C and C++ and also support graphics which is most important for designing games and other utility application.

Data validation:

We have written many codes for data validation that is most important in calendar design. Code for data validation includes:
1. Data validation for normal and leap years.
2. Number of days in a month for different months for a normal and leap year.
3. Data validation for time.
Most important term in calendar design is leap year. If we not take leap year correctly then we never get correct output.


Condition for leap year: 

For century years:  century year must be divisible by four hundred (400) for a leap year i.e. year 2000 is leap year while 1900 is not a leap year.For non-century years: non-century years must be divisible by 4 for a leap year i.e. 2004 is leap year while 2011, 2014 is not a leap year.

CODE:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
#include<bios.h>

#define mx getmaxx()/2
#define my getmaxy()/2

struct date ori_date;
struct time ori_time;

short dat,month;
int year,y;
short sex=7,sey=7;
short size=190,lx=2*size/sex,ly=2*size/sey;
short count=0;
short int background_colour=BLACK,
box_colour=2,
day_colour=3,
month_and_year_colour=4,
dat_colour=5,
dayhighlight_colour=6,
time_color=RED;

short dim(void);
void tim(void);
short take_info(short *);
void section(void);
void days(void);
void show_mon_year(void);
void show_dats(short fd,short m);
void current_date(void);
void add_info(void);
void info(void);

void main()
{
                int grd=DETECT,grm,erc,temp,fy;
                short fd,m,key=0,check=0,i=0;
                do{
                                closegraph();
                                getdate(&ori_date);
                                initgraph(&grd,&grm,"");
                                erc=graphresult();
                                if(erc!=grOk)
                                {
                                                printf("\nError in graph  %s :",grapherrormsg(erc));
                                                getch();
                                                exit(1);
                                }
                                noexe:
                                if(year<=0)
                                {
                                                dat=1;
                                                month=1;
                                                year=1;
                                                fy=1;
                                }
                                fd=take_info(&m);
                                count++;
                                setcolor(GREEN);
                                rectangle(mx-size-1,my-size-1,mx+size-1,my+size-1);
                                rectangle(mx-size-2,my-size-2,mx+size,my+size);
                                setbkcolor(background_colour);
                                section();
                                days();
                                show_mon_year();
                                show_dats(fd,m);
                                add_info();
                                //getch();
                                ent_right_key:
                                while(1)
                                {
                                                if(i==100)
                                                {
                                                                setfillstyle(SOLID_FILL,background_colour);
                                                                bar(mx-105,2*my-40,mx+120,2*my-3);
                                                                tim();
                                                                i=0;
                                                }
                                                delay(10);
                                                if(bioskey(1))
                                                {
                                                                goto ext;
                                                }
                                                i++;
                                }
                                ext:
                                key=bioskey(0);
                                key=key>>8;
                                if(key==75&&fy==0)
                                {
                                                month--;
                                                if(month<1)
                                                {
                                                                month=12;
                                                                year--;
                                                }
                                                check=dim();
                                                if(dat>check)
                                                                dat=check;
                                                if(year<=0)
                                                {
                                                                dat=1;
                                                                month=1;
                                                                year=1;
                                                }
                                }
                                else if(key==77)
                                {
                                                month++;
                                                if(month>12)
                                                {
                                                                month=1;
                                                                year++;
                                                }
                                                temp=dat;
                                                dat=1;
                                                check=dim();
                                                if(temp>check)
                                                                dat=check;
                                                else
                                                                dat=temp;
                                }
                                else if(key==72)
                                {
                                                year++;
                                                temp=dat;
                                                dat=1;
                                                check=dim();
                                                if(temp>check)
                                                                dat=check;
                                                else
                                                                dat=temp;
                                }
                                else if(key==80&&fy==0)
                                {
                                                year--;
                                                temp=dat;
                                                dat=1;
                                                check=dim();
                                                if(temp>check)
                                                                dat=check;
                                                else
                                                                dat=temp;
                                }
                                else if(key==74&&fy==0)
                                {
                                                dat--;
                                                if(dat<1)
                                                {
                                                                dat=1;
                                                                month--;
                                                                if(month<1)
                                                                {
                                                                                month=12;
                                                                                year--;
                                                                }
                                                                check=dim();
                                                                dat=check;
                                                }
                                }
                                else if(key==78)
                                {
                                                check=dim();
                                                dat++;
                                                if(dat>check)
                                                {
                                                                dat=1;
                                                                month++;
                                                                if(month>12)
                                                                {
                                                                                month=1;
                                                                                year++;
                                                                }
                                                }
                                }
                                else if(key==34)
                                {
                                                count=-100;
                                                closegraph();
                                }
                                else if(key==57)
                                                info();
                                else if(key==46)
                                                current_date();
                                else if(key==1)
                                                break;
                                else
                                                goto ent_right_key;
                                fy=0;
                }
                while(1);

}

void tim(void)
{
                short col,i=0,hr,mn,sc,fh=0,fm=0,fs=0,hrc,mnc,scr;
                int gap=60;
                char t='a';
                gettime(&ori_time);
                hr=ori_time.ti_hour;
                mn=ori_time.ti_min;
                sc=ori_time.ti_sec;
                hrc=ori_time.ti_hour;
                if(hrc==0)
                {
                                hrc=12;
                                hr=12;
                }
                if(hrc>12)
                {
                                hrc-=12;
                                t='p';
                }
                for(i=0;;i++)
                {
                                if(fh==0)
                                {
                                                if(hr<10)
                                                                hr=0;
                                                else
                                                                hr=1;
                                }
                                if(hr==0&&fh==0)
                                {
                                                hr=i+1;
                                                fh=1;
                                }
                                if(fm==0)
                                                mn=mn/10;
                                if(mn==0&&fm==0)
                                {
                                                mn=i+1;
                                                fm=1;
                                }
                                if(fs==0)
                                                sc=sc/10;
                                if(sc==0&&fs==0)
                                {
                                                sc=i+1;
                                                fs=1;
                                }
                                if(fh==1&&fm==1&&fs==1)
                                                break;
                }
                char ch_hr[5],ch_min[5],ch_sec[5];
                itoa(hrc,ch_hr,10);
                itoa(ori_time.ti_min,ch_min,10);
                itoa(ori_time.ti_sec,ch_sec,10);
                struct textsettingstype gs;
                gettextsettings(&gs);
                col=getcolor();
                setcolor(time_color);
                settextstyle(10,0,3);
                outtextxy(mx-125+(2-hr)*25,2*my-50,ch_hr);
                outtextxy(mx-125+50,2*my-55,":");
                if(ori_time.ti_min==0)
                                outtextxy(mx-125+gap,2*my-50,"00");
                else if(mn==1)
                {
                                outtextxy(mx-125+gap,2*my-50,"0");
                                outtextxy(mx-130+gap+30,2*my-50,ch_min);
                }
                else
                                outtextxy(mx-125+gap,2*my-50,ch_min);
                outtextxy(mx-125+gap+50,2*my-55,":");
                if(ori_time.ti_sec==0)
                                outtextxy(mx-120+2*gap,2*my-50,"00");
                else if(sc==1)
                {
                                outtextxy(mx-120+2*gap,2*my-50,"0");
                                outtextxy(mx-125+2*gap+30,2*my-50,ch_sec);
                }
                else
                                outtextxy(mx-125+2*gap,2*my-50,ch_sec);
                if(t=='a')
                                outtextxy(mx-125+2*gap+30+25,2*my-50,"A.M.");
                if(t=='p')
                                outtextxy(mx-125+2*gap+30+25,2*my-50,"P.M.");
                setcolor(col);
                settextstyle(gs.font,gs.direction,gs.charsize);
}

void current_date(void)
{
                dat=ori_date.da_day;
                month=ori_date.da_mon;
                year=ori_date.da_year;
}

void section(void)
{
                short i,g;
                setcolor(box_colour);
                for(i=0;i<=sey;i++)
                {
                                line(mx-size,my-size+i*ly,mx+size,my-size+i*ly);
                }
                for(i=0;i<=sex;i++)
                {
                                if(i==0||i==sex)
                                                g=0;
                                else
                                                g=ly;
                                line(mx-size+i*lx,my-size+g,mx-size+i*lx,my+size);
                }
}
void days(void)
{
                setcolor(day_colour);
                settextstyle(3,0,2);
                for(short i=1;i<=sex;i++)
                {
                                switch (i)
                                {
                                                case 1: outtextxy(mx-size+i*lx-lx/2-16,my-size+ly+20,"Sun");   break;
                                                case 2: outtextxy(mx-size+i*lx-lx/2-16,my-size+ly+20,"Mon");   break;
                                                case 3: outtextxy(mx-size+i*lx-lx/2-16,my-size+ly+20,"Tue");   break;
                                                case 4: outtextxy(mx-size+i*lx-lx/2-16,my-size+ly+20,"Wed");   break;
                                                case 5: outtextxy(mx-size+i*lx-lx/2-16,my-size+ly+20,"Thur");   break;
                                                case 6: outtextxy(mx-size+i*lx-lx/2-16,my-size+ly+20,"Fri");   break;
                                                case 7: outtextxy(mx-size+i*lx-lx/2-16,my-size+ly+20,"Sat");   break;
                                }
                }
}

short dim(void)
{
                short mon;
      if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
                mon=31;
      else if(month==4||month==6||month==9||month==11)
                mon=30;
      else if(y==0)
                mon=29;
      else
                mon=28;
      return mon;
}

short take_info(short *mon)
{
      short i=0,k=0,l=0,m=0,datc=0,temp;
      long d;
      start:
      clrscr();
      clearviewport();
      if(count==0)
                      dat=ori_date.da_day;
      else if(count==-100)
      {
                      printf("\nEnter the day, month year.\n");
                      scanf("%d",&dat);
      }
      else{}
      if((dat>31)||(dat<=0))
      {
                   printf("\nInvalid dat.\nEnter date again.\n");
                  delay(2000);
                  goto start;
      }
      if(count==0)
                      month=ori_date.da_mon;
      else if(count==-100)
                   scanf("%d",&month);
      else{}
      if((month>12)||(month<=0))
      {
                    printf("\nInvalid month.\nEnter month again:\n");
                  delay(2000);
                  goto start;
      }
      if(count==0)
                      year=ori_date.da_year;
      else if(count==-100)
      {
                                scanf("%d",&year);
                 /*          if(year>=31500)
                                {
                                                printf("\nYear limit exceeded.Enter year again.");
                                                delay(2000);
                                                goto start;
                                }   */
      }
      if(year<=0)
      {
                   printf("\nInvalid dat.\nEnter date again.\n");
                  delay(2000);
                  goto start;
      }
      else{}
      datc=1;
      d=datc;
      k=(year-1);
      l=(year-1)/4;
      if(year%100==0)
                y=(year/100)%4;
      else
                y=year%4;
      for(i=1;i<=month;i++)
      {
                                                   if(i==2)
                                                                d+=31;
                                                   else if(i==3)
                                                   {
                                                                if(y==0)
                                                                                d+=29;
                                                                else
                                                                                d+=28;
                                                   }
                                                   else if(i==4)
                                                                d+=31;
                                                   else if(i==5)
                                                                d+=30;
                                                   else if(i==6)
                                                                d+=31;
                                                   else if(i==7)
                                                                d+=30;
                                                   else if(i==8)
                                                                d+=31;
                                                   else if(i==9)
                                                                   d+=31;
                                                   else if(i==10)
                                                                d+=30;
                                                   else if(i==11)
                                                                d+=31;
                                                   else if(i==12)
                                                                d+=30;
      }
      //printf("\nd=%d   k=%d   l=%d   year=%d  month=%d   day=%d",d,k,l,year,month,dat);
      if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
                *mon=31;
      else if(month==4||month==6||month==9||month==11)
                *mon=30;
      else if(y==0)
                *mon=29;
      else
                *mon=28;
      if(dat>*mon)
      {
                                printf("\nEnter date again.");
                                delay(2000);
                                clrscr();
                                goto start;
      }
      if(year<=2000)
      {
                                for(temp=2000;temp>=year;temp-=100)
                                {
                                                if(temp%400==0)
                                                                continue;
                                                m++;
                                                m%=7;
                                }
      }
      else
      {
                                for(temp=2000;temp<=year;temp+=100)
                                {
                                                if(temp%400==0)
                                                                continue;
                                                m++;
                                                m%=7;
                                }
      }
      d+=m;
    //  printf("\n\n m: %d\n\n",*mon);
      d+=k+l;
      if(d>7)
      d=d%7;
   /*   printf("\n\n");
      switch(d)
      {
                       case 1: printf("\tSunday");
                       break;
                       case 2: printf("\tMonday");
                       break;
                       case 3: printf("\tTuesday");
                       break;
                       case 4: printf("\tWednesday");
                       break;
                       case 5: printf("\tThursday");
                       break;
                       case 6: printf("\tFriday");
                       break;
                       case 0: printf("\tSaturday");
      }
      getche();
      printf("\n\nd:\n\n",d);    */
      clearviewport();
      if(d==0)
                                return 7;
      else
                                return d;
}

void show_mon_year(void)
{
                short x_cor=mx-size+30,y_cor=my-size+10;
                char ch_year[8];
                short j=0;
                for(short i=year;;)
                {
                                i=i/10;
                                j++;
                                if(i==0)
                                {
                                                break;
                                }
                }
                itoa (year,ch_year,10);
                setfillstyle(SOLID_FILL,box_colour);
                floodfill(x_cor+3,y_cor+3,box_colour);
                setcolor(month_and_year_colour);
                settextstyle(3,0,4);
                switch(month)
                {
                                case 1 : outtextxy(x_cor,y_cor,"JANUARY,"); break;
                                case 2 : outtextxy(x_cor,y_cor,"FEBRUARY,"); break;
                                case 3 : outtextxy(x_cor,y_cor,"MARCH,"); break;
                                case 4 : outtextxy(x_cor,y_cor,"APRIL,"); break;
                                case 5 : outtextxy(x_cor,y_cor,"MAY,"); break;
                                case 6 : outtextxy(x_cor,y_cor,"JUNE,"); break;
                                case 7 : outtextxy(x_cor,y_cor,"JULY,"); break;
                                case 8 : outtextxy(x_cor,y_cor,"AUGUST,"); break;
                                case 9 : outtextxy(x_cor,y_cor,"SEPTEMBER,"); break;
                                case 10: outtextxy(x_cor,y_cor,"OCTOBER,"); break;
                                case 11: outtextxy(x_cor,y_cor,"NOVEMBER,"); break;
                                case 12: outtextxy(x_cor,y_cor,"DECEMBER,"); break;
                }
                if(j<=4)
                {
                                for(i=0;i<4-j;i++)
                                                outtextxy(x_cor+200+20*i+i,y_cor,"0");
                                outtextxy(x_cor+200+i*20,y_cor,ch_year);
                }
                else
                                outtextxy(x_cor+200,y_cor,ch_year);
}
void show_dats(short fd,short mon)
{
                short k=0,l=0,m=0,get;
                char change[3];
                setcolor(dat_colour);
                settextstyle(1,0,2);
                remain:
                for(short i=my-size+2*ly;i<my+size-10;i+=ly)
                {
                                for(short j=mx-size;j<mx+size-10;j+=lx)
                                {
                                                k++;
                                                if(k==fd)
                                                                l=1;
                                                if(l==1)
                                                {
                                                                m++;
                                                                itoa(m,change,10);
                                                                if(m==dat)
                                                                {
                                                                                get=getcolor();
                                                                                setcolor(dayhighlight_colour);
                                                                                rectangle(j+10,i+10,j+50,i+50);
                                                                                setfillstyle(SOLID_FILL,dayhighlight_colour);
                                                                                floodfill(j+15,i+15,dayhighlight_colour);
                                                                                setcolor(get);
                                                                }
                                                                outtextxy(j+20,i+20,change);
                                                                if(m>=mon)
                                                                                goto out;
                                                }
                                }
                }
                goto remain;
                out:
}

void add_info(void)
{
                int pclr;
                pclr=getcolor();
                settextstyle(6,3,3);
                setcolor(LIGHTMAGENTA);
                outtextxy(2*mx-100,50,"Press spacebar for additional information.");
                setcolor(pclr);
}

void info(void)
{
                int bk,width=3,gap=3,x=0,i,nxt_line=30,x_diff=8,y=0,font=6,font_size=3;
                clearviewport();
                bk=getbkcolor();
                setbkcolor(bk);
                setcolor(RED);
                x+=gap;
                for(i=x;i<=width+x;i++)
                                rectangle(i,i,2*mx-i,2*my-i);
                x=x+width+gap;
                setcolor(WHITE);
                for(i=x;i<=width+x;i++)
                                rectangle(i,i,2*mx-i,2*my-i);
                x=x+width+gap;
                setcolor(GREEN);
                for(i=x;i<=width+x;i++)
                                rectangle(i,i,2*mx-i,2*my-i);
                y+=nxt_line;
                x+=x_diff;
                setcolor(GREEN);
                settextstyle(font,0,font_size);
                outtextxy(x,y,          "1.Press '+' key to increase the date by 1.");
                outtextxy(x,y+=nxt_line,"2.Press '-' key to decrease the date by 1.");
                outtextxy(x,y+=nxt_line,"3.Press 'left arrow' key to decrease the month by 1.");
                outtextxy(x,y+=nxt_line,"4.Press 'right arrow' key to increase the month by 1.");
                outtextxy(x,y+=nxt_line,"5.Press 'lower arrow' key to decrease the year by 1.");
                outtextxy(x,y+=nxt_line,"6.Press 'upper arrow' key to increase the year by 1.");
                outtextxy(x,y+=nxt_line,"7.Press 'g' key to go to new date .");
                outtextxy(x,y+=nxt_line,"8.Press 'c' key to go to the current date .");
   //         outtextxy(x,y+=nxt_line,"9.Press 'o' key to change the window outline colour.");
                outtextxy(x,y+=nxt_line,"9.Press ESC to exit from the program .");
                getch();
                setbkcolor(bk);
                clearviewport();
}




Run this program using TURBO C++ compiler. You can also manipulate code for different looks and output and to add or delete some features.

No comments:

Powered by Blogger.