⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.cpp

📁 做为SD0609的一员,我扎实的基础是如何打出来的,来看看我那时连猴子选大王都不会的人,是如何成长起来的吧!主要包括我所做的C++,JAVA基础联系.乱是乱了点,不过如果真心想学习,打好基础,看看也不
💻 CPP
字号:
/*****************************************
* alarm clock demo: class and object
* Tarena, Chen Zongquan, 2006/8
*****************************************/
#include <iostream>
using namespace std;
#include <ctime>

class Clock{
    int rh;//realHour;
    int rm;//realMinute;
    int rs;//realSecond;
    int ah;//alarmHour;
    int am;//alarmMinute;
    int as;//alarmSecond;
public:
    Clock();//constructor
    void setTime( int h, int m, int s );//set real time
    void setAlarm( int h, int m, int s );//set alarm time
    void tick();//time step
    void output();//show real time and alarm time
};
Clock::Clock()//initialize all data to 0
:rh(0),rm(0),rs(0)//initial list
{
    ah = am = as = 0;//assign 0
}
void Clock::setTime( int h, int m, int s )
{
    rh = (h>=0&&h<24?h:0);
    rm = (m>=0&&m<60?m:0);
    rs = (s>=0&&s<60?s:0);
}
void Clock::setAlarm( int h, int m, int s )
{
    ah = (h>=0&&h<24?h:0);
    am = (m>=0&&m<60?m:0);
    as = (s>=0&&s<60?s:0);
}
void Clock::output()
{
    cout << "Time:" << rh << ':' << rm << ':' << rs << endl;
    cout << "Alarm:" << ah << ':' << am << ':' << as << endl;
}
void Clock::tick()//real time step
{
    if( ++rs == 60 )//step to 60 seconds?
    {
        rs = 0;//reset second to 0
        if( ++rm == 60 )//step to 60 minutes?
        {
            rm = 0;//reset minute to 0
            if( ++rh == 24 )//step to 24 hours?
                rh = 0;//reset hour to 0
        }
        //show hour and minute
        cout << rh << ':' << rm << endl;
    }
    else
        cout << '.' << flush;
    if( rh==ah && rm==am && rs==as )
    {//is alarm time?
        cout << "Wake Up!\a\a\a\a\a\a\a\a\a" << endl;
    }
}
//my sleep function
void sleep( int n )
{
    time_t t = time(NULL)+n;
    while( t!=time(NULL) );
}


int main()
{
    Clock c;
    c.setAlarm( 7, 10, 0 );
    c.setTime( 7, 9, 50 );
    c.output();
    for( ;; )
    {
        sleep( 1 );
        c.tick();
    }
    cout << endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -