倒计时器代码

来源:百度知道 编辑:UC知道 时间:2024/05/11 02:37:53
求各位用C语言帮小弟写一个高考的倒计时器
要求如下:
1、根据当前系统的日期来计算距离高考的时间(显示格式为:天数:小时数(24制):分钟数:秒数)
2、当前日期如果过了高考的日期则自动计算距下一年的高考时间显示格式同上

写好了有高分重谢

程序设计思想:
(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 ; /