函数体的规模要小,尽量控制在 50 行代码之内。
1 #include <iostream> 2 #include <conio.h> 3 #include <time.h> 4 5 using namespace std; 6 //定义时间延迟函数 7 void Dtime(double dt) { 8 time_t current_time; 9 time_t start_time; 10 11 //得到开始时间 12 time(&start_time); 13 //延迟处理 14 do 15 { 16 time(¤t_time); 17 } 18 while (difftime(current_time,start_time)<dt); 19 } 20 21 22 int main(int argc, char** argv) { 23 //声明变量 24 int i; 25 time_t current_time; 26 char *timep; 27 //循环10次,每隔2秒显示一次时间 28 for(i=0;i<10;i++) { 29 time(¤t_time); 30 timep=ctime(¤t_time); 31 cputs(timep); 32 Dtime(2); 33 } 34 }