[C++] 纯文本查看 复制代码
#include <iostream>
#include <cstdlib>
#include <ctime> //在C++中使用C库中的函数
using namespace std;
class timer {
int seconds;
public:
//以字符串形式指定初始化时间的秒数
timer(const char *t) { seconds = atoi(t); }
//以整数形式指定初始化时间的秒数
timer(int t) { seconds = t; }
//以分和秒的形式指定初始化时间
timer(int min, int sec) { seconds = min*60 + sec; }
void run();
};
void timer::run()
{
clock_t t1;
t1 = clock();
while((clock()/CLOCKS_PER_SEC - t1/CLOCKS_PER_SEC) < seconds);
cout << "\a"; //响铃
}
int main()
{
timer a(10), b("20"), c(1, 10);
a.run(); //倒数10秒
b.run(); //倒数20秒
c.run(); //倒数1分零10秒
return 0;
}