1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <stdio.h>
#include <time.h>
#include <process.h>
#include <stdlib.h>
#include <windows.h>
struct timer_info{
int interval ;
void (*function)(void) ;
} ;
int hour, min, sec ;
HANDLE thread_id ;
void watch()
{
sec++ ;
if (sec > 59){
min++ ;
sec = 0 ;
}
if (min > 59){
hour++ ;
min = 0 ;
}
if (hour > 23)
hour = 0 ;
system("cls") ;
printf("%02d : %02d : %02d\n", hour, min, sec) ;
}
void timer(void *info)
{
struct timer_info *t = (struct timer_info*)info ;
int interval = t -> interval ;
void (*func)(void) = t -> function ;
while(1){
clock_t begin = clock() ;
while (clock() - begin < 1000) ;
func() ;
}
}
void set_timer(int interval, void (*func)(void))
{
struct timer_info info = {interval, func} ;
thread_id = (HANDLE)_beginthread(timer, 0, (void*)&info) ;
}
void init_watch(int &h, int &m, int &s)
{
time_t init_data ;
struct tm *ltime ;
time(&init_data) ;
ltime = localtime(&init_data) ;
h = ltime->tm_hour ;
m = ltime->tm_min ;
s = ltime->tm_sec ;
}
int main()
{
init_watch(hour, min, sec) ;
printf("%02d : %02d : %02d\n", hour, min, sec) ;
set_timer(1000, watch) ;
WaitForSingleObject(thread_id, 1000000000) ;
return 0 ;
}
|