📄 clock.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 + -