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

本文展示了一个使用C++实现的时间类,包括初始化、显示、递增和比较时间的功能。
#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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值