#include <iostream>
#include <ctime> //describe clock() function, clock_t type
int main()
{
using namespace std;
cout<<"Enter the delay time, in seconds: ";
float secs;
cin>>secs;
clock_t delay= secs * CLOCKS_PER_SEC//convert to clock ticks *在此处是乘号
cout<<"starting\a\n";
clock_t start=clock();
while (clock()-start<delay ) //wait until time elaspses
; //note the semicolon
cout<<"done \a\n";
return 0;
}
头文件ctime(time.h)里面定义了一个符号常量——CLOCKS_PER_SEC,该常量等于每秒钟包含的系统时间单位数,因此,将系统时间除以这个值,可以得到秒数,或者将秒数乘以CLOCKS_PER_SEC可以得到一系统时间单位为单位的时间。
其次,ctime将clock_t作为clock()返回类型的别名(typedef创建别名在后面的文章内会写到),这意味着可以将变量生命为clock_t类型,编译器可以把它转为long、unsigned int或者其他适合系统的类型。