标签:des style color io os ar 数据 sp div
4-2 电子时钟中的运算符重载
输入6个整数,之间用一个空格间隔;分别表示开始时间的时、分、秒和结束时间的时、分、秒的值
从开始时间到结束时间之间所有时间对象的值;每个值占一行,格式为hh:mm:ss
01 01 01 01 01 10
01:01:01 01:01:02 01:01:03 01:01:04 01:01:05 01:01:06 01:01:07 01:01:08 01:01:09 01:01:10
#include <iostream>
using namespace std;
class Time
{
private:
int hour;
int minute;
int second;
public:
Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
Time operator ++ ();
void display();
int cmp(Time &t);
};
Time Time::operator ++ ( )
{
if(++second>= 60)
{
second-= 60;
if(++minute>= 60)
{
minute-=60;
++hour;
}
}
}
void Time::display()
{
if(hour<10)
cout<<"0"<<hour<<":";
else
cout<<hour<<":";
if(minute<10)
cout<<"0"<<minute<<":";
else
cout<<minute<<":";
if(second<10)
cout<<"0"<<second;
else
cout<<second;
cout<<endl;
}
int Time::cmp(Time & t)
{
if(hour>t.hour)
return 0;
else if(hour== t.hour&& minute > t.minute)
return 0;
else if(hour == t.hour && minute == t.minute && second > t.second)
return 0;
else
return 1;
}
int main()
{
int h1,h2,m1,m2,s1,s2;
cin>>h1>>m1>>s1;
cin>>h2>>m2>>s2;
Time t1(h1,m1,s1);
Time t2(h2,m2,s2);
if(!t1.cmp(t2) )
{
cout<<"The begin time is not earlier than the end time!"<<endl;
}
else
{
while(t1.cmp(t2))
{
t1.display();
++t1;
}
}
return 0;
}
标签:des style color io os ar 数据 sp div
原文地址:http://blog.csdn.net/u013486414/article/details/39956903