美文网首页
小练习:时钟

小练习:时钟

作者: ie大博 | 来源:发表于2016-11-08 17:10 被阅读0次
    #include<iostream>
    #include<unistd.h>//sleep(1)的头文件
    #include<stdlib.h>
    #include<stdio.h>
    using namespace std;
    class alexlock
    {
        int m_hour;
        int m_min;
        int m_sec;
    public:
        void set(int h, int m, int s)
        {
            m_hour=h;
            m_min=m;
            m_sec=s;
        }
        void show()
        {
            cout<<m_hour<<":";
            cout<<m_min<<":";
            cout<<m_sec<<":"<<endl;//这个地方不能少回车,因为要从缓存流显示出来,
    没有回车就会等到缓存流满了才显示。
        }
        void tick()
        {
            sleep(1);
            m_sec++;
            if(m_sec==60)
            {
                m_min++;
                m_sec=0;
                if(m_min==60)
                {
                    m_hour++;
                    m_min=0;
                    if(m_hour==24)
                    {
                        m_hour=0;
                    }
                }
            }
        }
        void run()
        {
            while(1)
            {
                
            //  system("clear");
                show();
                tick();
            }
        }
    };
    int main()
    {
        alexlock t;
        t.set(23,59,55);
        t.run();
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:小练习:时钟

          本文链接:https://www.haomeiwen.com/subject/isoauttx.html