计时器的实现

                                                /*此代码的功能实现了计时器的功能*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
struct clock {
    int second;
    int minute;
    int hour;
};/*如果你学过C的话,这并不陌生,{结构},但是,进阶了解过cpp的话,{clock类}*/
typedef struct clock CLOCK;
int main() {
    CLOCK t = { 0,0,0 };
    system("pause");
    int n = 100, i = 0;/*n的值也可以改,代表的是循环次数n-1*/
    while (++i < n) {
        t.second++;
        if (t.second >= 60) {
            t.second = 0;
            t.minute++;
        }
        if (t.minute >= 60) {
            t.minute = 0;
            t.hour++;
        }
        if (t.hour >= 12) {
            t.hour = 0;
        }/*实现计数的功能,minute,second 实现60进制,hour 实现12进制*/
        printf("%2d:%2d:%2d\r",t.hour, t.minute, t.second);
        Sleep(1000);/*让cpu休息下,当然你也可以改变数值,具体换算关系是1000ms=1s*/
    }

    system("pause");
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值