这个的本质其实就是个简单的计时器
/*
功能:改写闹钟类
日期:2013-10-19
*/
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class Time
{
public:
void getNow(time_t t,tm *lt)const;
public:
int m_aYear;
int m_aMonth;
int m_aDay;
int m_aHour;
int m_aMinute;
int m_aSecond;
time_t t;
tm *lt;
string m_aName;
};
class Clock:public Time
{
public:
void getClock(time_t t,tm *lt);
void setName(string name);
void setDay(int aYear,int aMonth,int aDay);
void setTime(int aHour,int aMinute,int aSecond);
};
void Time::getNow(time_t t,tm *lt)const
{
cout<<"现在时间:"<<lt->tm_year+1900<<"-"<<lt->tm_mon+1<<"-"<<lt->tm_mday<<" "<<lt->tm_hour<<":"<<lt->tm_min<<":"<<lt->tm_sec<<endl;
}
void Clock::setName(string name)
{
m_aName = name;
}
void Clock::setDay(int aYear,int aMonth,int aDay)
{
m_aYear = aYear;
m_aMonth = aMonth;
m_aDay = aDay;
}
void Clock::setTime(int aHour,int aMinute,int aSecond)
{
m_aHour = aHour;
m_aMinute = aMinute;
m_aSecond = aSecond;
}
void Clock::getClock(time_t t,tm *lt)
{
cout<<"闹铃启动..."<<endl;
while(1)
{
if (m_aYear == lt->tm_year+1900&&m_aMonth ==lt->tm_mon+1&&m_aDay == lt->tm_mday&&m_aHour ==lt->tm_hour&&m_aMinute==lt->tm_min&&m_aSecond==lt->tm_sec)
{
cout<<m_aName<<"开始行动啦!"<<endl;
break;
}
t = time(NULL);
lt = localtime(&t);
}
}
int main(void)
{
Time timea;
Clock clock;
string name;
time_t t;
tm *lt;
int year,month,day,hour,minute,second;
t = time(NULL);
lt = localtime(&t);
cout<<"欢迎使用电子闹钟,请输入您的名字:"<<endl;
cin>>name;
clock.setName(name);
timea.getNow(t, lt);
cout<<"请输入您的闹铃日期:(年、月、天使用回车隔开):"<<endl;
cin>>year>>month>>day;
clock.setDay(year,month,day);
cout<<"请输入您的闹铃时间:(时、分、秒使用回车隔开):"<<endl;
cin>>hour>>minute>>second;
clock.setTime(hour,minute,second);
clock.getClock(t,lt);
system("pause");
return 0;
}