Write a program to display Analog Clock in c language | CTechnotips

 // C++ Program for display Clock.

#include <iostream.h>
#include <graphics.h>
#include <time.h>
#include <math.h>
#include <dos.h>    // for kbhit()
#include <stdlib.h>    // for exit()     // Set the path where your graphics files are located here
#include<conio.h>

// Set the path where your graphics files are located here
#define BGI_PATH "c:\\tc\\bgi"

// Constants
const double PI = 3.14159265;
const double DEGS_PER_SEC = 6.0; // Angle by which seconds hand moves in a sec
const double DEGS_PER_MIN = 6.0;
const double DEGS_PER_HOUR = 30.0;
const double SEC_PER_MIN = 60.0;
const double MIN_PER_HOUR = 60.0;

int maxx, maxy;     // Maximum x and y coordinates for current graph mode

void Initialize( void );    // Init graphics system
void ShowFrame( void );        // Show clock frame

// Convert degrees ro radians
inline double ToRadians( double theta )
{
    return (theta*PI)/180.0;
}

    // A hand class
class HAND
{
    int length, color;
    double angle,
        prev_angle;        // Holds prev position, needed to erase hand
 public:
    HAND( int len, int clr ) { length = len; color = clr; prev_angle = angle = 0;}    // Constructor
    void SetAngle( double theta ) { angle = theta; }
    void Copy(){ prev_angle = angle ;}
    int Change(){ return ( angle != prev_angle ); }
    void Display( int x, int y );
    void Erase( int x, int y );
};

    // Display hand with origin x,y
void HAND::Display( int x, int y  )
{
    int x1, y1;

    x1 = x + length*sin( angle );
    y1 = y - length*cos( angle );    // y increases from top to bottom

    setcolor( color );
    setfillstyle( EMPTY_FILL, color );
    line( x,y,x1,y1 );
}

    // Erase previously drawn hand
void HAND::Erase( int x, int y )
{

    int x1, y1;
    x1 = x + length*sin( prev_angle );
    y1 = y - length*cos( prev_angle );    // y increases from top to bottom

    setcolor( BLACK );        // Backgnd color
    line( x,y,x1,y1 );
}


int main()
{
    struct tm* ptr_curtime;    // Pointer to current time
    time_t curtime;            // Holds cuurent time

    HAND hour( 55, RED ), minute( 85, GREEN ), second( 70, BLUE );
    int h_angle, m_angle, s_angle;
    int center_x, center_y;        // Clock center

    Initialize();
    ShowFrame();

    center_x = (maxx+1)/2;
    center_y = (maxy+1)/2;

    while( !kbhit())
    {
        hour.Copy();    // Store previous values
        minute.Copy();
        second.Copy();

            // Get current time
        curtime = time(&curtime);
        ptr_curtime = localtime(&curtime);

            // Calculate the angles of hands
        h_angle = ( ptr_curtime->tm_hour%12 * DEGS_PER_HOUR ) + ( ptr_curtime->tm_min * ( DEGS_PER_HOUR / MIN_PER_HOUR ));    // Ignore change in seconds
        m_angle = ( ptr_curtime->tm_min * DEGS_PER_MIN ) + ( ptr_curtime->tm_sec *  ( DEGS_PER_MIN / SEC_PER_MIN )) ;    // Minute hand's position also changes with secs
        s_angle = ptr_curtime->tm_sec * DEGS_PER_SEC;


        hour.SetAngle( ToRadians(h_angle));
        minute.SetAngle( ToRadians( m_angle));
        second.SetAngle( ToRadians( s_angle));

            // Display all hands if seconds position has changed
        if( second.Change())
        {
            second.Erase( center_x, center_y );
            second.Display( center_x, center_y );

            minute.Erase( center_x, center_y );
            minute.Display( center_x, center_y );

            hour.Erase( center_x, center_y );
            hour.Display( center_x, center_y );

            gotoxy(30,25);
            cout<<ctime(&curtime);
        }

    } // End of while( !kbhit())

    closegraph();
    restorecrtmode();
    return 0;
} // End of main

    // Initialize graphics system
void Initialize( void )
{
    int GraphDriver    = DETECT, GraphMode;
    int ErrorCode;

    initgraph( &GraphDriver, &GraphMode, BGI_PATH );
    ErrorCode = graphresult();
    if( ErrorCode != grOk )
    {
        cout<<"\nGraphics system error: "<<grapherrormsg( ErrorCode );
        exit( 1 );
    }
    maxx = getmaxx();
    maxy =  getmaxy();
}

    // Shows the clock frame
void ShowFrame( void )
{
      setcolor( WHITE );
      int x1,y1;
      circle( (maxx+1)/2, ( maxy+1)/2, 100 );
      circle( (maxx+1)/2, ( maxy+1)/2, 105 );

      char str[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"};



      for( int angle = 0,k=0; angle < 360; angle+=30,k++ )
      {
        x1 = (maxx+1)/2 + 90*cos(ToRadians(angle));
        y1 = (maxy+1)/2 - 90*sin(ToRadians(angle));
        outtextxy(x1,y1,str[k]);
        gotoxy(25,25);
      }
}


----------------------------------------------------------
                        OR
----------------------------------------------------------

// C coding for Display Clock.

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

void main()
{
    struct time t;
    void drawclock(void);
    void intro(void);
    void sound(void);
    int gdriver=DETECT,gmode;
    initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
    int i,j,k,s,m,h;
    intro();
    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
    drawclock();
    gettime(&t);

    if(t.ti_sec>15)
    s=360-((t.ti_sec-15)*6);
    else
    s=90-(t.ti_sec*6);

    if(t.ti_min>15)
    m=360-((t.ti_min-15)*6);
    else
    m=90-(t.ti_min*6);

    if( (t.ti_hour==0)||(t.ti_hour==1)||(t.ti_hour==2)||(t.ti_hour==3))
    h=90-(t.ti_hour*30)-((t.ti_min/12)*6);
    else if((t.ti_hour>=4)&&(t.ti_hour<=12)) h=360-((t.ti_hour-3)*30)-((t.ti_min/12)*6); else if((t.ti_hour>=13)&&(t.ti_hour<=23))
    h=360-((t.ti_hour-15)*30)-((t.ti_min/12)*6);
    else// if(t.ti_hour==12)
    h=90-((t.ti_hour-12)*30)-((t.ti_min/12)*6);

    for(k=0;k<=12;k++)
    {
        if(h==0)
        h=360;
        delay(1000);
        drawclock();
        for(j=0;j<=60;j++)
        {
            if(m==0)
            m=360;
            setcolor(4);
            sector(320,150,m,m+1,75,75);
            m=m-6;
            delay(1000);
            drawclock();
            for(i=0;i<=60;i++)
            {
                setcolor(4);
                sector(320,150,h,h+3,60,60);
                setcolor(4);
                sector(320,150,m,m+1,75,75);
                if(s==0)
                s=360;
                setcolor(1);
                sector(320,150,s,s+1,80,80);
                sound();
                s=s-6;
                delay(1000);
                drawclock();
                if(kbhit())
                exit(0);
            }
            setcolor(6);
            sector(320,150,h,h+3,60,60);
        }
    }
    getch();
}


void drawclock()
{
    cleardevice();
    setbkcolor(15);
    setcolor(1);
    circle(320,150,80);
    setcolor(5);
    circle(320,150,95);
    circle(320,150,1);
    outtextxy(314,58,"12");
    outtextxy(268,70,"11");
    outtextxy(235,105,"10");
    outtextxy(233,147,"9");
    outtextxy(241,190,"8");
    outtextxy(273,225,"7");
    outtextxy(318,238,"6");
    outtextxy(362,225,"5");
    outtextxy(392,190,"4");
    outtextxy(403,147,"3");
    outtextxy(393,105,"2");
    outtextxy(361,70,"1");
    outtextxy(302,200,"TILAK");
}

void intro()
{
int i;
int u=installuserfont("TSCR.CHR");
settextstyle(u,0,7);
outtextxy(135,100,"KAUSHAL's");
outtextxy(220,200,"CLOCK");
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 1);
outtextxy(380,400,"Loading..");

for(i=0;i<510;i++)
{
setcolor(15);
rectangle(50,100,50+i,110);
delay(5);
}
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 1);
outtextxy(380,400,"Loading....");

for( i=0;i<510;i++)
{
setcolor(4);
rectangle(50,100,50+i,110);
delay(5);
}

settextstyle(TRIPLEX_FONT, HORIZ_DIR, 1);
outtextxy(380,400,"Loading......");

for(i=0;i<510;i++)
{
setcolor(15);
rectangle(50,200,50+i,210);
delay(5);
}
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 1);
outtextxy(380,400,"Loading........");

setfillstyle(SOLID_FILL,1);
fillellipse(320,205,6,6);
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 1);
outtextxy(380,400,"Loading..........");

for(i=0;i<510;i++)
{
setcolor(15);
rectangle(50,300,50+i,310);
delay(5);
}
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 1);
outtextxy(380,400,"Loading............");

for( i=0;i<510;i++)
{
setcolor(2);
rectangle(50,300,50+i,310);
delay(5);
}
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 1);
outtextxy(380,400,"Loading..............");
delay(1000);
}
void sound()
{
sound(2000);
delay(100);
nosound();
}

0 comments:

Post a Comment