倒计时在生活中很常见今天我们就用C++把他实现。
#include <iostream>
using namespace std;
class Time{
int hour;
int min;
int sec;
public:
Time(int a,int b,int c)
{
hour=a;
min=b;
sec=c;
}
void show()
{
cout << '\r';
if(hour<10)
cout << 0;
cout << hour << ':';
if(min<10)
cout << 0;
cout << min << ':';
if(sec<10)
cout << 0;
cout << sec << flush;
}
void tick()
{
long t=time(NULL);
while(time(NULL)==t);
if(--sec<=0)
{
sec=59;
if(--min<=0)
{
min=59;
if(--hour<=0)
hour=23;
}
}
}
void run()
{
for(;;)
{
tick();
show();
}
}
};
int main()
{
int a=0,b=0,c=0;
cout << "请输入时间:" << endl;
cout<< "hour:";
cin >> a;
cout<<"minute:";
cin >> b;
cout<< "second:";
cin >> c;
Time t(a,b,c);
t.run();
}
怎么样是不是很简单