编程:倒数计时器

#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秒

转载于:https://www.cnblogs.com/donald1024/p/5452239.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值