这是倒计时完整程序,语句意思 见里面的说明。(抱歉,我习惯用英文)。
while (k < xxxx) 改变 数值xxxx,可以控制程序 运行时间。
#include
#include
#include
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}
void main(){
time_t rawtime;
struct tm * target_time;
int d,h,m,s;
int k=0;
long double dif,r;
time_t now,t_end; // in sec
/* get current timeinfo and modify it to the user's choice */
time ( &rawtime );
target_time = localtime ( &rawtime ); // time_t to tm
// time struc and to time_t
target_time->tm_year = 2015 - 1900;
target_time->tm_mon= 8 - 1; // month - 1
target_time->tm_mday = 1 ; // day
target_time->tm_hour = 0 ; // hour
target_time->tm_min = 0 ;
target_time->tm_sec = 0 ;
t_end = mktime (target_time);
// printf("%s ",ctime(&t_end)); // print and check
while (k < 120)
{
now = time (NULL);
// printf("%s ",ctime(&now)); // print and check
dif = difftime (t_end,now); // double , time_t time_t
// printf( "%lf\n",dif);
d = (int) (dif / 86400.0);
r = dif - d * 86400.0;
h = (int) (r / 3600.0);
r = r - h * 3600.0;
m = (int) (r / 60.0);
r = r - m * 60.0;
s = (int) (r);
printf("%d--days %d--hours %d--min %d--sec\n",d,h,m,s);
(void) wait ( 2 ); // every 2 seconds print
k = k + 1;
};
}