永发信息网

用C++写一个时钟怎么写

答案:1  悬赏:30  手机版
解决时间 2021-03-31 21:32
用C++写一个时钟怎么写
最佳答案
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 

class Date;
class Time {
public:
    bool OVFlag;

    Time(unsigned short const h_, unsigned short const m_, unsigned short const s_) :
        OVFlag(false), h(h_), m(m_), s(s_) {
        if (h_ >= 24 || m_ >= 60 || s_ >= 60) throw;// 检查时间是否合理
    }
    void tick(Date &d);

private:
    unsigned short h, m, s;// 时分秒

    template
    friend std::basic_ostream& operator<<(std::basic_ostream& ostr, Time const &t) {
        using std::setw;
        using std::setfill;
        return ostr
            << setw(2) << setfill('0') << t.h << ":"
            << setw(2) << setfill('0') << t.m << ":"
            << setw(2) << setfill('0') << t.s;
    }
};

class Date {
public:
    Date(unsigned short const y_, unsigned short const m_, unsigned short const d_) :
        y(y_), m(m_), d(d_) {
        if (1 > m_ || m_ > 12 || 1 > d_ || d_ > days(y_, m_)) throw;// 检查日期是否合理
    }
    void pass(Time &t);

private:
    unsigned short y, m, d;// 年月日

    static bool leap(unsigned short y);
    static unsigned short days(unsigned short y, unsigned short m);

    template
    friend std::basic_ostream& operator<<(std::basic_ostream& ostr, Date const &d) {
        using std::setw;
        return ostr << d.y << "/" << d.m << "/" << d.d;
    }
};

void Time::tick(Date &d) {
    assert(OVFlag == false && h < 24 && m < 60 && s < 60);
    if (++s == 60) {
        // 过了一分钟
        s = 0;
        if (++m == 60) {
            // 过了一小时
            m = 0;
            if (++h == 24) {
                // 过了一天
                h = 0;
                OVFlag = true;
                d.pass(*this);
            }
        }
    }
}

bool Date::leap(unsigned short y) {
    return (y % 4 == 0) || ((y % 100 != 0) && (y % 400 == 0));
}

unsigned short Date::days(unsigned short y, unsigned short m) {
    switch (m) {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        return 31;
    case 4: case 6: case 9: case 11:
        return 30;
    case 2:
        return leap(y) + 28;
    default:
        throw;// 不在正确的范围
    }
}

void Date::pass(Time &t) {
    assert(t.OVFlag == true);
    t.OVFlag = false;
    if (d++ == days(y, m)) {// 过了一个月
        d = 1;
        if (m++ == 12) {// 过了一年
            m = 1;
            ++y;
        }
    }
}

int main() {
    // 获取当前时间
    time_t current_time;
    time(¤t_time);
    tm *local_time = localtime(¤t_time);

    // 设置初始时间
    Date d(1900 + local_time->tm_year, 1 + local_time->tm_mon, local_time->tm_mday);
    Time t(local_time->tm_hour, local_time->tm_min, local_time->tm_sec);

    // 开始模拟
    clock_t last_clock = clock(), duration = 0;
    while (true) {
        clock_t const current_clock = clock();
        duration += current_clock - last_clock;// 时间间隔
        last_clock = current_clock;// 记下上次的时间
        for (int i = 0; i != duration / CLOCKS_PER_SEC; ++i) {// 每经过1秒,调用1次tick
            t.tick(d);
            std::cout << d << " " << t << "
";
        }
        duration %= CLOCKS_PER_SEC;
    }

    return 0;
}


使用公共属性OVFlag的弊端:打破封装,将实现细节暴露给外面,容易出错,不利于定位错误,调试和维护。
好处:访问起来很方便。

追问
这个怎么改阿
追答是复制过去的吗?最好贴出完整的代码!不行试试这个


我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
注册会计师和注册资产评估师,哪个证的含金量
以my most unforgettable day为题的英语作文
男生发“……”是什么意思?
搞笑献爱心段子
#店长#店员不小心卖给45岁左右女士一盒毓婷,
滦县旅行社详细点的有几家
256除以六的商是几位数,商的最高位是几位数,
蛋糕1300克是多大
珠宝电商的经营模式有几种,具体该怎么玩?
长春八中属于哪个区
电脑上网看电视都没声音怎么回事
何谓离别? 一句话说出你的见解
计算;180度-(48度39分40秒+67度41分35秒)
纪宝如的为爱私奔
西安哪些学校有开夜大?
推荐资讯
李超饭店怎么去啊,有知道地址的么
甲公司购买原材料,对方代垫的包装费,如甲公
女朋友的爷爷过世我要跪前来吊唁的客人吗
供热计量表跟水流量有关吗
怎么学打架
寇致公司注册地是哪里
单选题比较下列句子,上下文衔接较好的一项是
求小说白洁传
对联:家传美德,世继嘉风的含义
3.54x0.4÷3.54x0.4的简便计算
主板 技嘉B15M-D3H和华硕B15M-PLUS哪个好 请
ofo共享单车怎么免费用
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?