Tuesday, June 6, 2017

C Program to create a clock

Graphics in C – Program to create a clock


In C computer language, you can create any program. Only your imagination is the limit. Here is a program in C graphics, which creates a clock. Please type this program in your editor.

#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int i=0, j=0, k=0, l=0;
int s, m, h, c, r;
void dial(int x, int y);
void minutehand(int a, int b, int c);
void secondhand(int a, int b, int c);
void hourhand(int a, int b, int c);
void main()
{
      int gd=DETECT, gm;
      int maxx, maxy;
      char str[50];
      int ch1, ch2, ch3, ch4, ch5, ch6;
      int hour, min, sec;
      /* time_t is a variable type */
      time_t t;
      /* time() gets time of day */
      time(&t);
      /* ctime() converts date and
      time to a string */
      strcpy(str, ctime(&t));
      initgraph(&gd, &gm, "c:\\tc\\bgi");
      /* for converting a charactor to
         an integer */
      ch1=str[11]-'0';
      ch2=str[12]-'0';
      ch3=str[14]-'0';
      ch4=str[15]-'0';
      ch5=str[17]-'0';
      ch6=str[18]-'0';
      hour=ch1*10+ch2;
      min=ch3*10+ch4;
      sec=ch5*10+ch6;

      if(hour>=12)
      {
            hour=hour-12;
      }
      s=sec;
      m=min;
      h=hour;
      c=m/12;
      r=m%12;
      maxx=getmaxx();
      maxy=getmaxy();
      while(!kbhit())
      {
            cleardevice();
            dial(maxx, maxy);
            hourhand(maxx, maxy, hour);
            minutehand(maxx, maxy, min);
            secondhand(maxx, maxy, sec);
            delay(1000);
            if(s+i==60)
            {
                  j++;
                  s=0;
            }

            if(i>60)
            {
                    i=i%60;
            }

            l=j;

            if(r+l==12)
            {
                    r=0;
                    k++;
             }

            if(l>12)
            {
                   l=l%12;
            }

            if(k==5)
            {
                    k=0;
            }
      }
      closegraph();
      restorecrtmode();
}

void dial(int x, int y)
{
      setcolor(WHITE);
      setfillstyle(SOLID_FILL, WHITE);
      circle(x/2, y/2, 120);
      floodfill(x/2, y/2, WHITE);
      circle(x/2, y/2, 140);
      setfillstyle(SOLID_FILL, DARKGRAY);
      floodfill(x/2, y/2-130, WHITE);
      setcolor(RED);
      settextstyle(0, 0, 2);
      outtextxy(x/2-13, y/2-110, "12");
      outtextxy(x/2-6, y/2+96, "6");
      outtextxy(x/2-108, y/2-7, "9");
      outtextxy(x/2+96, y/2-7, "3");
      circle(x/2, y/2, 10);
      setfillstyle(SOLID_FILL, RED);
      floodfill(x/2, y/2, RED);
      setcolor(BLACK);
      circle(x/2, y/2-115, 4);
      setfillstyle(SOLID_FILL, BLACK);
      floodfill(x/2, y/2-115, BLACK);
      circle(x/2, y/2+115, 4);
      floodfill(x/2, y/2+115, BLACK);
      circle(x/2-114, y/2, 4);
      floodfill(x/2-114, y/2, BLACK);
      circle(x/2+114, y/2, 4);
      floodfill(x/2+114, y/2, BLACK);
      circle(x/3+45, y/3-15, 4);
      floodfill(x/3+45, y/3-15, BLACK);
      circle(x/3+5, y/3+30, 4);
      floodfill(x/3+5, y/3+30, BLACK);
      circle(x/3+45, 2*y/3+15, 4);
      floodfill(x/3+45, 2*y/3+15, BLACK);
      circle(x/3+5, 2*y/3-30, 4);
      floodfill(x/3+5, 2*y/3-30, BLACK);
      circle(2*x/3-45, y/3-15, 4);
      floodfill(2*x/3-45, y/3-15, BLACK);
      circle(2*x/3-5, y/3+30, 4);
      floodfill(2*x/3-5, y/3+30, BLACK);
      circle(2*x/3-5, 2*y/3-30, 4);
      floodfill(2*x/3-5, 2*y/3-30, BLACK);
      circle(2*x/3-45, 2*y/3+15, 4);
      floodfill(2*x/3-45, 2*y/3+15, BLACK);
}

void secondhand(int a, int b, int sec)
{
      int x1=a/2, y1=b/2;
      int x2, y2;
      /* 2*3.14/60=0.1047 */
      x2=x1+(int)(90 * cos((i-15+sec)*0.1047));
      y2=y1+(int)(90 * sin((i-15+sec)*0.1047));
      setcolor(BLACK);
      setlinestyle(0, 0, 1);
      line(x1, y1, x2, y2);
      i++;
}

void minutehand(int a, int b, int min)
{
      int x1, x2, y1, y2;
      x1=a/2;
      y1=b/2;
      setcolor(BLACK);
      setlinestyle(0, 0, 3);
      x2=x1+(int)(90*cos((min+j-15)*0.1047));
      y2=y1+(int)(90*sin((min+j-15)*0.1047));
      line(x1, y1, x2, y2);
}

void hourhand(int a, int b, int hour)
{
      int x1, y1, x2, y2;
      x1=a/2;
      y1=b/2;
      setcolor(BLACK);
      setlinestyle(0, 0, 3);
      x2=x1+(int)(70*cos(((hour*5)+k+c-15)*0.1047));
      y2=y1+(int)(70*sin(((hour*5)+k+c-15)*0.1047));
      line(x1, y1, x2, y2);
}