时间类模拟闹钟,请教,谢谢,问题见程序注释
#include<iostream>using namespace std;
class clock
{
public:
clock(int h,int m,int s);
void setalarm(int h,int m,int s);
void showtime();
void run();
private:
int hours, minutes, seconds;
int ahours, aminutes, aseconds;
};
clock::clock(int h,int m,int s)
{
seconds=(s>59?0:s);
minutes=(m>59?0:m);
hours=(h>23?0:h);
}
void clock::setalarm(int h,int m,int s)
{
ahours=(h>23?0:h);
aminutes=(m>59?0:m);
aseconds=(s>59?0:s);
}
void clock::showtime()
{
cout<<"now:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
void clock::run()
{
seconds=seconds+1;
if(seconds>59)
{
seconds=0;
minutes+=1;
}
if(minutes>59)
{
minutes=0;
hours+=1;
}
if(hours>24)
hours=0;
if(hours==ahours&&minutes==aminutes&&seconds==aseconds)
{
cout<<"!pink...."<<endl;
cout<<"时间到了"<<'\a'<<endl;
}
}
int main()
{
clock c(7,59,57);
c.showtime();
c.setalarm(8,0,0);
for(int i=0;i<3600*24*3+100;i++) //为什么是3600*24*3+100,求解释
c.run();
c.showtime(); 程序输出结果为什么是5:1:37
system("pause");
return 0;
}