首页 > 代码库 > SDUTOJ 2711 4-2 电子时钟中的运算符重载

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

#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;
}



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