编程:倒数计时器

#include<iostream>
#include<Windows.h>
using namespace std;

class  ti
{
    int h;        //小时
    int m;        //分钟
    int s;        //
public:
    void set(int hour, int min, int sec)        //传递计时器值
    {
        h = hour;
        m = min;
        s = sec;
    };
    void tick()        //计时跳动
    {
        Sleep(1000);        //执行挂起1000毫秒,即等待一秒后继续进行
        s--;
        if (s < 0)
        {
            s = 59;
            m--;
            if (m < 0)
            {
                m = 59;
                h--;
            }
        }
    };
    void show()        //展示剩余时间
    {
        if (h < 10)
        {
            cout << 0;
        }
        cout << h<<":";
        if (m < 10)
            cout << 0;
        cout << m<<":";
        if (s < 10)
            cout << 0;
        cout << s ;
        
    };
    void run()
    {
        while (h || m || s)        //一直运行直到全0
        {
            
            tick();
            system("cls");
            show();
            
        }
        cout << "time out!"<< endl;
    }
};

 
int main()
{
    ti t;
    t.set(0, 2, 0);        //倒数2分钟
    t.run();
}

 等待一秒方法,

(1)Sleep(1000);等待1000毫秒

(2)#include<time.h>

  time_t t=time(NULL);  //获取从1970年1月1日开始算起的秒数

  while(time(NULL)==t);  //直到秒数不同,即过了1秒

posted @ 2016-05-02 13:36  chris_chan1024  阅读(579)  评论(0编辑  收藏  举报