第1个回答 推荐于2016-06-09
程序设计思想:
(1)输入目标时间,高考的年,月,日,时,分,秒
下面例子中简写成直接赋值。
(2)转换成 struct tm
(3)再转换成 time_t
(4) 获当前时间 now = time (NULL);
(5)用difftime 计算时间差,送返 long double 秒
(6)把秒转换成 日,时,分,秒
(7)循环 (下面例子中简写成 打印120次,每隔2秒左右打一次)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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 struc and to time_t
target_time->tm_year = 2008 - 1900; //year - 1900
target_time->tm_mon= 7 - 1; // month - 1
target_time->tm_mday = 15 ; // day
target_time->tm_hour = 13 ; // hour
target_time->tm_min = 1 ;
target_time->tm_sec = 1 ;
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;
};
}本回答被提问者采纳