Digital Clock in C++

Luckychap -2 Tallied Votes 15K Views Share

Here is simple Digital Clock coded in C++ and compiler used is TurboC.

A class DIGIT is designed to draw digits. Follwoing are the details for this class:

Consructor: DIDGT(); for default settings
DIGITE(int s,int c,int st); for user define settings
s -> is size of the digit
c -> color of the digit
st -> fill style of the digit

Methods: void DIGITE::DrawDig(int x,int y,int n)
x,y -> x and y coordinates of the screen
n -> interge value (<10) to be display

void DIGITE::PutDig(int x,int y,int n)
x,y -> x and y coordinates of the screen
n -> interge value (<100) to be display

void DIGITE::MakeBars()
internal function used to draw horizontal and vertical bars for the digits in the memory.

thats it!

/* 
    Developed by: Vivek Kumar
    Email:        vivek_kumar_bt@yahoo.co.in

    A Simple Digital Clock

*/

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
#define DOTCOLOR 14
#define DIGCOLOR 4
class DIGITE
{
 private:
   int X,Y;
   int SIZE;
   int COLOR;
   int HIGHT,WIDTH;
   void *VER,*HOR;
 public:
  int style;
  DIGITE();
  DIGITE(int s,int c,int st);
  void MakeBars();
  void PutDig(int x,int y,int n);
  void DrawDig(int x,int y,int n);
  ~DIGITE();
};
DIGITE::DIGITE()
{
 style=1;
 SIZE=1;
 HIGHT=1;
 WIDTH=12;
 COLOR=15;
 MakeBars();
}
DIGITE::DIGITE(int s,int c,int st)
{
 style=st;
 SIZE=s;
 COLOR=c;
 HIGHT=1;
 WIDTH=6;
 MakeBars();
}
void DIGITE::MakeBars()
{
 int x,y,w,h;
 int area;
 x=10;
 y=getmaxy()/2;
 w=WIDTH*SIZE;
 h=HIGHT*SIZE;
 setfillstyle(style,COLOR);
 bar(x+h,y,x+w-h,y+h);
 setcolor(COLOR);
 line(x,y+h/2,x+h,y);
 line(x,y+h/2,x+h,y+h);
 floodfill(x+h/2,y+h/2,COLOR);
 setcolor(COLOR);
 line(x+w-h,y,x+w+-h+h,y+h/2);
 line(x+w-h,y+h,x+w-h+h,y+h/2);
 floodfill(x+w-h+h/2,y+h/2,COLOR);
 area=imagesize(x,y,x+w,y+h);
 HOR=malloc(area);
 if(HOR==NULL)
 {
  printf("Low Memory(Horizontal)...\nPress Any Key");
  getch();
  exit(0);
 }
 getimage(x,y,x+w,y+h,HOR);
 putimage(x,y,HOR,XOR_PUT);
 x=getmaxx()/2;
 y=10;
 h=SIZE*WIDTH;
 w=HIGHT*SIZE;
 bar(x,y+w,x+w,y+h-w);
 setcolor(COLOR);
 line(x,y+w,x+w/2,y);
 line(x+w,y+w,x+w/2,y);
 floodfill(x+w/2,y+w/2,COLOR);
 setcolor(COLOR);
 line(x,y+h-w,x+w/2,y+h);
 line(x+w,y+h-w,x+w/2,y+h);
 floodfill(x+w/2,y+h-w/2,COLOR);
 area=imagesize(x,y,x+w,y+h);
 VER=malloc(area);
 if(VER==NULL)
 {
  printf("Low Memory(Vertical)...\nPress Any Key");
  getch();
  exit(0);
 }
 getimage(x,y,x+w,y+h,VER);
 putimage(x,y,VER,XOR_PUT);
}
void DIGITE::PutDig(int x,int y,int n)
{
 int num,gap,qu,rem;
 gap=(HIGHT*SIZE);
 num=n;
 if(num>=0&&num<10)
 {
  DrawDig(x,y,0);
  DrawDig(x+WIDTH*SIZE+HIGHT*SIZE+gap,y,n);
 }
 if(num>=10&&num<100)
 {
  qu=num/10;
  rem=num%10;
  DrawDig(x,y,qu);
  DrawDig(x+WIDTH*SIZE+HIGHT*SIZE+gap,y,rem);
 }
}
void DIGITE::DrawDig(int x,int y,int n)
{
 int H[10][3]={1,0,1,
	       0,0,0,
	       1,1,1,
	       1,1,1,
	       0,1,0,
	       1,1,1,
	       1,1,1,
	       1,0,0,
	       1,1,1,
	       1,1,1};
 int V[10][4]={1,1,1,1,
	       0,1,0,1,
	       0,1,1,0,
	       0,1,0,1,
	       1,1,0,1,
	       1,0,0,1,
	       1,0,1,1,
	       0,1,0,1,
	       1,1,1,1,
	       1,1,0,1};
 int HX[3]={x,x,x};
 int HY[3]={y,y+WIDTH*SIZE,y+2*WIDTH*SIZE};
 int VX[4]={x-(HIGHT*SIZE)/2,x+WIDTH*SIZE-(HIGHT*SIZE)/2,x-(HIGHT*SIZE)/2,x+WIDTH*SIZE-(HIGHT*SIZE)/2};
 int VY[4]={y+(HIGHT*SIZE)/2,y+(HIGHT*SIZE)/2,y+WIDTH*SIZE+(HIGHT*SIZE)/2,y+WIDTH*SIZE+(HIGHT*SIZE)/2};
 int i;
 setfillstyle(1,getpixel(x-1,y-1));
 bar(x-(SIZE*HIGHT)/2,y,x+WIDTH*SIZE+(SIZE*HIGHT)/2,y+2*SIZE*WIDTH+HIGHT*SIZE);
 for(i=0;i<3;i++)
 {
  if(H[n][i]==1)
   putimage(HX[i],HY[i],HOR,XOR_PUT);
 }
 for(i=0;i<4;i++)
 {
 if(V[n][i]==1)
  putimage(VX[i],VY[i],VER,XOR_PUT);
 }
}

DIGITE::~DIGITE()
{
 delete VER;
 delete HOR;
}

void drawdot();
int color=DOTCOLOR;

void main()
{
 int gd=DETECT,gm,i;
 initgraph(&gd,&gm,"");
 struct time t;
 int th=t.ti_hour;
 int tm=t.ti_min;
 int ts=t.ti_sec;
 int tms=t.ti_hund;
 //setbkcolor(15);
 gettime(&t);
 DIGITE h(8,DIGCOLOR,1);
 DIGITE m(8,DIGCOLOR,1);
 DIGITE s(8,DIGCOLOR,1);
 DIGITE ms(2,DIGCOLOR,1);
 int y=200;
 int flag=1;
 h.PutDig(20,y,t.ti_hour);
 m.PutDig(220,y,t.ti_min);
 s.PutDig(380,y,t.ti_sec);
 ms.PutDig(500,y,t.ti_hund);

 while(!kbhit())
 {
  gettime(&t);
  if(th!=t.ti_hour)
  {
   th=t.ti_hour;
   h.PutDig(20,y,t.ti_hour);
  }
  if(tm!=t.ti_min)
  {
   tm=t.ti_min;
   m.PutDig(220,y,t.ti_min);
  }
  if(ts!=t.ti_sec)
  {
   ts=t.ti_sec;
   s.PutDig(380,y,t.ti_sec);

    drawdot();
    color=color-DOTCOLOR;
    if(color<0)
     color=DOTCOLOR;
  }
  if(tms!=t.ti_hund)
  {
    tms=t.ti_hund;
    ms.PutDig(500,y,t.ti_hund);
  }
 }
 getch();
}

void drawdot()
{
 int x,y,gap,size,c;
 c=color;
 gap=10;
 size=15;
 x=170;
 y=200;
 setfillstyle(1,color);
 bar(x+gap,y+gap,x+gap+size,y+gap+size);
 bar(x+gap,y+8*gap,x+gap+size,y+8*gap+size);
 //color=15;
 delay(0);
}
irum.nageen.3 -6 Newbie Poster

check out this one:

#include<iostream>
#include<Windows.h>
using namespace std;

struct time{

    int hr,min,sec;
};
int main()
{
    time a;
    a.hr = 0;
    a.min = 0;
    a.sec = 0;

    for(int i = 0; i<24; i++)
    {
        if(a.hr == 23)
        {
            a.hr = 0;
        }

        for(int j = 0; j<60; j++)
        {
            if(a.min == 59)
            {
                a.min = 0;
            }

            for(int k = 0; k<60; k++)
            {
                if(a.sec == 59)
                {
                    a.sec = 0;
                }

                cout<<a.hr<<" : "<<a.min<<" : "<<a.sec<<endl;
                a.sec++;
                Sleep(1000);
                system("Cls");
            }       
        a.min++;

    }

        a.hr++; 
    }

}
irum.nageen.3 -6 Newbie Poster
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.