C program code to display system time as clock
The following is a source code to display the system time. If you are interested in this program, then it is easy to produce its executable file. You simply copy this code and save it on your computer with extension '.c'.For example the file name as 'clock.c'. After this you open this file in 'turboc' and run it. surely you will get the result.
The code:
/*********** vipin programs **********************************
* Program to display a virtual clock and a digital display *
* by vipin.p.nair *
* s1s2 ec cek *
* 31-3-07 *
*************************************************************/
// including header files
#include"graphics.h"
#include "dos.h"
#include"math.h"
// declaration of functions
void graphics_initialize();
void clockpic();
void needletime();
void clock_refresh();
void about();
void digital_display();
int timesec(char);
// define half of maximam x and y
#define maxx getmaxx()/2
#define maxy getmaxy()/2
// main function
void main()
{
graphics_initialize();
setbkcolor(WHITE);
clockpic();
about();
digital_display();
needletime();
closegraph();
}
//*******************************************************
// definition of functions
// initializing graphics
void graphics_initialize()
{
// auto detection mode
int driver=DETECT,mode=0,errorcode;
// initializing graphics driver
initgraph(&driver,&mode,"c:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
printf("graphics driver not initialized");
}
// drawing picture of clock
void clockpic()
{
setcolor(BROWN);
setfillstyle(7,YELLOW);
fillellipse(maxx,maxy,200,200);
circle(maxx,maxy,200);
circle(maxx,maxy,201);
setcolor(GREEN);
setcolor(GREEN);
settextstyle(1,0,6);
outtextxy(maxx-20,38,"12");
outtextxy(maxx-11,385,"6");
outtextxy(maxx+160,maxy-28,"3");
outtextxy(135,maxy-28,"9");
outtextxy(460,120,"2");
outtextxy(145,120,"10");
outtextxy(155,300,"8");
outtextxy(400,60,"1");
outtextxy(205,60,"11");
outtextxy(220,360,"7");
outtextxy(395,362,"5");
outtextxy(460,300,"4");
setcolor(LIGHTGRAY);
settextstyle(4,0,4);
outtextxy(290,120,"Clock");
outtextxy(290,120,"Clock");
}
// function for defining the motion of needle
void needletime()
{
{
double x_sec,y_sec,
x_hr,y_hr,
x_min,y_min, prv_sec,
sec=timesec('s'),
min=timesec('m')+sec/60,
//sec*6 degree/360
hr =timesec('h')+min/12;
//min*30 degree/360
while(!kbhit())
{
digital_display();
setcolor(BROWN);
x_sec=maxx+145*cos(1.57-sec*0.017444);
setcolor(BROWN);
x_sec=maxx+145*cos(1.57-sec*0.017444);
//0.01744 = 3.14/180 (degree to radian)
y_sec=maxy-145*sin(1.57-sec*.017444); // 1.57 Radian = 90 degree
x_min=maxx+110*cos(1.57-min*0.017444);
x_min=maxx+110*cos(1.57-min*0.017444);
y_min=maxy-110*sin(1.57-min*.017444);
x_hr=maxx+70*cos(1.57-hr*0.017444);
x_hr=maxx+70*cos(1.57-hr*0.017444);
y_hr=maxy-70*sin(1.57-hr*.017444);
line(319,239,x_hr,y_hr);
line(319,239,x_hr,y_hr);
line(319,239,x_min,y_min);
line(319,239,x_sec,y_sec);
circle(maxx,maxy,1);
circle(maxx,maxy,1);
circle(maxx,maxy,2);
circle(maxx,maxy,3);
circle(maxx,maxy,4);
prv_sec=sec;
sec=timesec('s');
if(prv_sec!=sec)
if(prv_sec!=sec)
clock_refresh();
min=timesec('m')+sec/60;
min=timesec('m')+sec/60;
hr=timesec('h')+min/12;
}
}
}
// refreshing a part of clock after each second
// refreshing a part of clock after each second
void clock_refresh()
{
setcolor(WHITE);
setfillstyle(7,YELLOW);
fillellipse(maxx,maxy,150,150);
settextstyle(1,0,3);
setcolor(BLUE);
outtextxy(maxx-25,100,"Clock");
}
// proving the degree of each needle
int timesec(char tm)
{
int hr;
struct time t;
gettime(&t);
if(tm=='s')
return (t.ti_sec*6);
if(tm=='m') return (t.ti_min*6);
if(tm=='h')
{
hr=t.ti_hour;
if(hr>12)
return (30*(hr-12));
return (30*hr);
} return 0; }
// about programmer
void about()
{
char cnme[]={ 118,105,112,105,110,32 ,112,
114,111,103,114,97 ,109,115,0 },
nme[]= { 66 ,121,32 ,118,105,112,105,110,
46 ,112,46 ,110,97 ,105,114,0 },
sdy[]= { 115,49 ,115,50 ,32 ,67 ,69 ,75 ,44 ,
50 ,48 ,48 ,55 ,0 },
mil[]= { 118,105,112,105,110,112,110,64,103,
109,97 ,105,108,46 ,99 ,111,109,0 };
setcolor(DARKGRAY);
settextstyle(0,1,1);
outtextxy(10,300,cnme);
outtextxy(20,300,nme);
outtextxy(30,300,sdy);
outtextxy(40,300,mil);
}
// providing digital display of clock
void digital_display()
{
int sec=timesec('s')/6,
min=timesec('m')/6,
hr =timesec('h')/30;
char s[3],m[3],h[3];
s[1]=sec%10+48;
// adding 48 to make it the ASCII value, since char
s[0]=sec/10+48;
m[2]=h[2]=s[2]=0;
m[1]=min%10+48;
m[0]=min/10+48;
h[1]=hr%10+48;
h[0]=hr/10+48;
setcolor(RED);
setcolor(RED);
settextstyle(0,0,1);
outtextxy(maxx-40,maxy+50,h);
outtextxy(maxx-25,maxy+50,":");
outtextxy(maxx-15,maxy+50,m);
outtextxy(maxx,maxy+50,":");
outtextxy(maxx+10,maxy+50,s);
}