Electronic clock program made of C

Source: Internet
Author: User






/* ----------------------------- Source code of the electronic clock ---------------------------------*/

/* ------------------------------ Preprocessing ----------------------------------------*/
# Include <graphics. h>
# Include <math. h>
# Include <dos. h>
# Include <stdio. h>
/* ----------------------------- Macro definition -----------------------------------------*/
# Define PI 3.1415926
# Define Up 0x4800
# Define Down 0x5000
# Define Tab 0xf09
# Define Esc 0x11b
/* ----------------------------- Define the global variable -----------------------------------*/
Double Hour, Minute, Second;
Int Radius = 130, Center_x = 300, Center_y = 265;
Double Hour_x, Hour_y, Minute_x, Minute_y, Second_x, Second_y;
Struct time Time [1];
/* ----------------------------- Function declaration -------------------------------------*/
Void ColockHandle ();
Int DigitalHour (double Hour );
Int DigitalMinute (double Minute );
Int DigitalSecond (double Second );
Void DigitalClock (int x, int y, int clock );
Int Up_Key (int Count );
Int Down_Key (int Count );
Void ClearCursor (int Count );
Void DrawCursor (int Count );
/* ---------------------------- Main function ------------------------------------------*/
Main ()
{
/* -------------- ----------------------*/
Int Driver, Mode;
Int I, j;
Driver = DETECT;
Initgraph (& Driver, & Mode ,"");
Setcolor (RED );
Line (5,120, 0 );
Line (595,120, 0 );
Line (5,120,595,120 );
Setfillstyle (SOLID_FILL, RED );
Floodfill (300,100, RED );
Setcolor (BLUE );
Line (65,120,535,120 );
Line (65,120, 65,470 );
Line (535,120,535,470 );
Line (65,470,535,470 );
Line (125,120,125,470 );
Line (475,120,475,470 );
Line (125,410,475,410 );
Setfillstyle (SOLID_FILL, BLUE );
Floodfill (80,130, BLUE );
Setfillstyle (SOLID_FILL, BLUE );
Floodfill (500,130, BLUE );
Setfillstyle (SOLID_FILL, BLACK );
Floodfill (300,450, BLUE );
/* ------------- Paint time interval -----------------*/
For (I = 275; I <= 305; I = I + 30)
{
For (j = 434; j <= 442; j = j + 8)
{
Setcolor (WHITE );
Setlinestyle (0, 0, 3 );
Circle (I, j, 1 );
}
}
/* ------------ Draw the cursor ----------------------*/
Setcolor (WHITE );
Line (270,430,270,445 );
/* ------------- Draw the table axis ---------------------*/
Setlinestyle (0, 0, 3 );
Circle (Center_x, Center_y, 1 );
/* ------------- Draw table box ---------------------*/
Setlinestyle (0, 0, 1 );
Circle (Center_x, Center_y, Radius + 3 );
/* -------------- Draw the sharding scale ----------------*/
For (I = 0, Minute = 0; I <= 59; I ++, Minute ++)
{
Minute_x = Center_x + Radius * sin (Minute/30 * PI );
Minute_y = Center_y-Radius * cos (Minute/30 * PI );
Setlinestyle (0, 0, 1 );
Circle (Minute_x, Minute_y, 1 );
}
/* -------------- Draw the hourly scale ----------------*/
Setcolor (RED );
For (I = 0, Hour = 0, Minute = 0; I <= 11; I ++, Hour ++)
{
Hour_x = Center_x + Radius * sin (Hour * 60 + Minute)/360 * PI );
Hour_y = Center_y-Radius * cos (Hour * 60 + Minute)/360 * PI );
Setlinestyle (0, 0, 3 );
Circle (Hour_x, Hour_y, 1 );


}
/* -------------- Add text --------------------*/
Setcolor (GREEN );
Outtextxy (283,50, "CLOCK ");
Outtextxy (320,110, "Designed by Zhang Cheng Jin ");
Outtextxy( 80,130, "Help ");
Outtextxy( 70,150, "Up :");
Outtextxy (78,168, "Time ++ ");
Outtextxy( 70,216, "Down :");
Outtextxy (78,234, "Time --");
Outtextxy (70,286, "Tab :");
Outtextxy (78,304, "Move ");
Outtextxy (70,356, "Esc :");
Outtextxy (78,374, "Exit ");
Outtextxy (500,190, "No ");
Outtextxy( 490,215, "time ");
Outtextxy (490,240, "like ");
Outtextxy( 495,265, "");
Outtextxy (480,290, "present ");
Outtextxy (500,315 ,"! ");
/* ---------- Call the function -----------------------*/
ColockHandle ();
/* ----------- Close the image ----------------------*/
Closegraph ();
/* ----------- Return 0, end of the program ---------------*/
Return 0;
}


/* --------------------------- Clock animation processing function ---------------------------------*/
Void ColockHandle ()
{
Int Key = 0, Count;
Setcolor (WHITE );
/* ---------------- Obtain the system time -------------*/
Gettime (Time );
Hour = Time [0]. ti_hour;
Minute = Time [0]. ti_min;
/* -------------- Draw the sub-needle -----------------*/
Minute_x = Center_x + 100 * sin (Minute/30 * PI );
Minute_y = Center_y-100 * cos (Minute/30 * PI );
Line (Center_x, Center_y, Minute_x, Minute_y );
/* ---------------- Draw the hour hand -----------------*/
Hour_x = Center_x + 80 * sin (Hour * 60 + Minute)/360 * PI );
Hour_y = Center_y-80 * cos (Hour * 60 + Minute)/360 * PI );
Line (Center_x, Center_y, Hour_x, Hour_y );
/* -------------- Draw a digital clock -------------*/
DigitalClock (255,435, DigitalHour (Hour ));
DigitalClock (285,435, DigitalMinute (Minute ));


Setwritemode (1 );
For (Count = 2; Key! = Esc ;)
{
/* ---------------- Draw seconds -----------------*/
Setcolor (RED );
Hour = Time [0]. ti_hour;
Minute = Time [0]. ti_min;
Second = Time [0]. ti_sec;


Second_x = Center_x + 120 * sin (Second/30 * PI );
Second_y = Center_y-120 * cos (Second/30 * PI );
Line (Center_x, Center_y, Second_x, Second_y );
/*-----------------------------------------*/
While (Hour = Time [0]. ti_hour & Minute = Time [0]. ti_min & Second = Time [0]. ti_sec)
{
Gettime (Time );
If (bioskey (1 )! = 0)
{
Key = bioskey (0 );
Count = KeyHandle (Key, Count );
If (Count = 5)
Count = 1;
}
}
/* ---------- Seconds for drawing the digital clock ---------------*/
Setcolor (WHITE );
DigitalClock (315,435, DigitalSecond (Second) + 1 );
/* ---------- Clear the image for the previous second -----------*/
Setcolor (RED );
Second_x = Center_x + 120 * sin (Second/30 * PI );
Second_y = Center_y-120 * cos (Second/30 * PI );
Line (Center_x, Center_y, Second_x, Second_y );


Setcolor (WHITE );
/* --------- Processing of sharding changes ----------------*/
If (Minute! = Time [0]. ti_min)
{
Minute_x = Center_x + 100 * sin (Minute/30 * PI );
Minute_y = Center_y-100 * cos (Minute/30 * PI );
Line (Center_x, Center_y, Minute_x, Minute_y );


Minute = Time [0]. ti_min;
DigitalClock (285,435, DigitalMinute (Minute ));
Minute_x = Center_x + 100 * sin (Minute/30 * PI );
Minute_y = Center_y-100 * cos (Minute/30 * PI );
Line (Center_x, Center_y, Minute_x, Minute_y );


}
/* ---------- Processing of hour-hand changes www.2cto.com ---------------*/
If (Hour! = Time [0]. ti_hour)
{
Hour_x = Center_x + 80 * sin (Hour * 60 + Minute)/360 * PI );
Hour_y = Center_y-80 * cos (Hour * 60 + Minute)/360 * PI );
Line (Center_x, Center_y, Hour_x, Hour_y );


Hour = Time [0]. ti_hour;
DigitalClock (255,435, DigitalHour (Hour ));
Hour_x = Center_x + 80 * sin (Hour * 60 + Minute)/360 * PI );
Hour_y = Center_y-80 * cos (Hour * 60 + Minute)/360 * PI );
Line (Center_x, Center_y, Hour_x, Hour_y );
}
}
}
/* ---------------------- Digital clock module ----------------------------------------*/
Int DigitalHour (double Hour)
{
Int h;
For (h = 0; h <24; h ++)
{
If (h = Hour)
{
Return h;
}
}
}


Int DigitalMinute (double Minute)
{
Int m;
For (m = 0; m <60; m ++)
{
If (m = Minute)
{
Return m;
}
}
}


Int DigitalSecond (double Second)
{
Int s;
For (s = 0; s <60; s ++)
{
If (s = Second)
{
Return s;
}
}
}


Void DigitalClock (int x, int y, int clock)
{


Char buffer [10];
Setfillstyle (0, 2 );
Bar (x, y, x + 14,449 );
If (clock = 60)
Clock = 0;
Sprintf (buffer, "% d", clock );
Outtextxy (x, y, buffer );
}
/* ---------------------- Key processing function ------------------------------------------*/
Int KeyHandle (int Key, int Count)
{
Switch (Key)
{
Case Up: Up_Key (Count-1 );
Break;
Case Down: Down_Key (Count-1 );
Break;
Case Tab: ClearCursor (Count );
DrawCursor (Count );
Count ++;
Break;
}
Return Count;
}
/* -------------------- Up direction key function ------------------------------------------*/
Int Up_Key (int Count)
{
If (Count = 1)
{
Time [0]. ti_hour --;
If (Time [0]. ti_hour = 24)
Time [0]. ti_hour = 0;
Settime (Time );
}


If (Count = 2)
{
Time [0]. ti_min --;
If (Time [0]. ti_min = 60)
Time [0]. ti_min = 0;
Settime (Time );
}


If (Count = 3)
{
Time [0]. ti_sec --;
If (Time [0]. ti_sec = 60)
Time [0]. ti_sec = 0;
Settime (Time );
}
}
/* -------------------- Downward direction key function ------------------------------------------*/
Int Down_Key (int Count)
{
If (Count = 1)
{
Time [0]. ti_hour ++;
If (Time [0]. ti_hour = 24)
Time [0]. ti_hour = 0;
Settime (Time );
}


If (Count = 2)
{
Time [0]. ti_min ++;
If (Time [0]. ti_min = 60)
Time [0]. ti_min = 0;
Settime (Time );
}


If (Count = 3)
{
Time [0]. ti_sec ++;
If (Time [0]. ti_sec = 60)
Time [0]. ti_sec = 0;
Settime (Time );
}
}
/* --------------------- Cursor processing function -------------------------------------------*/
Void ClearCursor (int Count)
{Setcolor (WHITE );
Switch (Count)
{
Case 2: line (1, 270,430,270,445 );
Break;
Case 3: line (1, 300,430,300,445 );
Break;
Case 1: line (1, 330,430,330,445 );
Break;
}
}


Void DrawCursor (int Count)
{
Switch (Count)
{
Case 1: line (1, 270,430,270,445 );
Break;
Case 2: line (1, 300,430,300,445 );
Break;
Case 3: line (1, 330,430,330,445 );
Break;
}
}
/*----------------------------------------------------------------------------*/

 

From wyzhangchengjin123

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.