#include
#include
# include "pthread. H"
#include
#include
#include
#include
using namespace std;
The class Clock
{
Private:
Struct tm mytime.//structure of the object is used to object storage time
Struct tm * p;
Public:
Int ret.
Explicit Clock (void);
The static void gettime (Clock * s);
Tm settime ();
Int setalarm ();
The static void * timing (void * arg c);
};
Explicit Clock: : Clock (void)
{
Pthread_t hd.//thread data
Time_t temp.//time structure object
Time (& amp; Temp);//get the system time
P=localtime (& amp; Temp);//data structure is changed time
//in the Clock class members
Mytime. Tm_year=p - & gt; Tm_year;
Mytime. Tm_mon=p - & gt; Tm_mon;
Mytime. Tm_mday=p - & gt; Tm_mday;
Mytime. Tm_hour=p - & gt; Tm_hour;
Mytime. Tm_min=p - & gt; Tm_min;
Mytime. Tm_sec=p - & gt; Tm_sec;
//open new threads for their own time run
If ((ret=pthread_create (& amp; Hd, NULL, timing, this (void *)))!=0)
{
cout<" Pthread create erorr!"
}
cout<" Time running "& lt;
The static void Clock: : gettime (Clock * s)
{
Time_t temp.
Time (& amp; Temp);
S - & gt; P=localtime (& amp; Temp);
S - & gt; Mytime. Tm_year=s - & gt; P - & gt; Tm_year;
S - & gt; Mytime. Tm_mon=s - & gt; P - & gt; Tm_mon;
S - & gt; Mytime. Tm_mday=s - & gt; P - & gt; Tm_mday;
S - & gt; Mytime. Tm_hour=s - & gt; P - & gt; Tm_hour;
S - & gt; Mytime. Tm_min=s - & gt; P - & gt; Tm_min;
S - & gt; Mytime. Tm_sec=s - & gt; P - & gt; Tm_sec;
}
//show time function
Char * Clock: : showtime ()
{
The static char buf [100].
Sprintf (buf, "% d years - on - % d % d % d: % d: % d", mytime. Tm_year + 1900, mytime. Tm_mon + 1, mytime. Tm_mday, mytime. Tm_hour, mytime. Tm_min, mytime. Tm_sec);
cout
}
//custom setting time function
Tm Clock: : settime ()
{
cout<" Please enter the year: ";
Cin> Mytime. Tm_year;
Mytime. Tm_year=mytime. Tm_year - 1900;
cout<" Please enter a month: ";
Cin> Mytime. Tm_mon;
Mytime. Tm_mon=mytime. Tm_mon - 1;
cout<" Please enter the date: ";
Cin> Mytime. Tm_mday;
cout<" Please enter the whole: ";
Cin> Mytime. Tm_hour;
cout<" Please enter a minute: ";
Cin> Mytime. Tm_min;
cout<" Please enter the second: ";
Cin> Mytime. Tm_sec;
Return mytime.
}
//simple imitation of the alarm clock
Int Clock: : setalarm ()
{
Int hour, min, SEC;
cout<" Please enter the alarm clock the whole: ";
Cin> Hour;
cout<" Please input clock minutes: ";
Cin> Min.
cout<" Please enter the second alarm clock: ";
Cin> The SEC;
While (1)
{
If (mytime. Tm_hour==hour)
{
If (min==mytime. Tm_min)
{
If (SEC==mytime. Tm_sec)
{
cout<" Master, time is up, the alarm clock rang, quick get up! \ n "& lt;
}
}
}
}
}
//thread execution function
The static void * Clock: : timing (void * arg c)
{
While (1)
{
Sleep (1);
(* (Clock * arg c)). Mytime. Tm_sec + +;
If ((* (Clock * arg c)). Mytime. 60 tm_sec %==0)
{
(* (Clock * arg c)). Mytime. Tm_sec=0;
(* (Clock * arg c)). Mytime. Tm_min + +;
If ((* (Clock * arg c)). Mytime. 60 tm_min %==0)
{
(* (Clock * arg c)). Mytime. Tm_min=0;
(* (Clock * arg c)). Mytime. Tm_hour + +;
If ((* (Clock * arg c)). Mytime. 24 tm_hour %==0)
{
(* (Clock * arg c)). Mytime. Tm_hour=0;
Gettime ((Clock *) arg c);
}
}
}
}
return NULL;
}
//main function content your call
Int main (void)
{
Clock obj.//object creation
Obj. Showtime ();//display time
Obj. Setalarm ();//set the alarm clock time
Obj. Showtime ();//show
Obj. Settime ();//set time
Obj. Showtime ();
While (1)
{
Sleep (1);
Obj. Showtime ();
}
return 0;
}