您的位置:首页 > 其它

SDUTOJ 2711 4-2 电子时钟中的运算符重载

2014-10-14 18:33 183 查看

#include<iostream>
#include<stdio.h>
using namespace std;
class Time
{
private:
int h, m, s;
public:
Time();
Time(int,int,int);
void display();
void tick();
Time operator ++();
bool operator >(Time);
};
bool Time::operator >(Time a)
{
if(h*3600+m*60+s>a.h*3600+a.m*60+a.s)
return true;
else
return false;
}
Time Time::operator ++()
{
if(++s>=60)
{
s=0;
if(m++>=60)
{
m=0;
h++;
}
}
return *this;
}
void Time::display()
{
printf("%02d:%02d:%02d\n",h,m,s);
}
Time::Time()
{
h=12;
m=0;
s=0;
}
Time::Time(int a,int b,int c)
{
if(b>=60||b<0)b=0;
if(c>=60||c<0)c=0;
h=a;
m=b;
s=c;
}
int main()
{
int a[6];
for(int i=0;i<6;i++)
cin>>a[i];
Time t1(a[0],a[1],a[2]),t2(a[3],a[4],a[5]);
if(t1>t2)
cout<<"The begin time is not earlier than the end time!"<<endl;
else
{
while(t2>t1)
{
t1.display();
++t1;
}
t1.display();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  运算符重载