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

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

Time Limit: 1000MS  Memory Limit: 65536KB
Problem Description
通过本题目的练习可以运算符重载的方法;
设计一个时间类Time,私有数据成员有hour(时)、minute(分)、second(秒);
公有成员函数有:setHour(int)设置数据成员hour的值,非法的输入默认为12;setMinue(int)设置数据成员minute的值,非法输入默认为0;setSecond(int)设置数据成员second的值,非法输入默认为0;setTime(int,int,int)设置时、分、秒三个数据成员的值;三个成员函数int getHour(); int getMinute(); int getSecond();分别用于获取时间对象的属性值。
定义两个构造函数Time(); 和 Time(int,int,int);
定义一个成员函数void displayTime(); 用于显示时间,注意格式为 hh:mm:ss,位数不够用0填充;
定义一个时钟增加1的成员函数 void tick(); 把second的值加1,并注意是否到60;
定义一个成员或友元函数 bool operator= =(….); 判断两个时间对象的值是否相等;
定义一个成员或友元函数bool operator>(…..); 判断第一个时间对象的值是否大于第二个时间对象的值。
 
 
在主函数main()中指定开始时间和结束时间,并调用相应成员函数,显示从开始时间到结束时间之间所有时间对象的值,其格式见示例输出。
Input

输入6个整数,之间用一个空格间隔;分别表示开始时间的时、分、秒和结束时间的时、分、秒的值

Output

从开始时间到结束时间之间所有时间对象的值;每个值占一行,格式为hh:mm:ss

Example Input
01 01 01 01 01 10
Example Output
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
Hint
输入
11 10 12 10 12 56
输出
The begin time is not earlier than the end time!

Author


#include<bits/stdc++.h>
using namespace std;

class Time
{
private:
    int hour, minute, second;
public:
    Time(int a, int b, int c)
    {
        hour = a;
        minute = b;
        second = c;
    }
    void show()
    {
        printf("%02d:%02d:%02d\n", hour, minute, second);
    }
    Time operator++()
    {
        if(++second >= 60)
        {
            second -= 60;
            if(++minute += 60)
            {
                minute -= 60;
                if(++hour >= 24)
                    hour -= 24;
            }
        }
        return *this;
    }
    bool operator > (const Time &right)
    {
        if(hour > right.hour)
            return true;
        else if(hour == right.hour && minute > right.minute)
            return true;
        else if(hour==right.hour&&minute==right.minute&&second>right.second)
            return true;
        return false;
    }
};
int main()
{
    int h1, m1, s1, h2, m2, s2;
    cin>>h1>>m1>>s1>>h2>>m2>>s2;
    Time p(h1, m1, s1), q(h2, m2, s2);
    if(!(p > q))
    {
        while(!(p > q))
        {
            p.show();
            ++p;
        }
    }
    else
        cout<<"The begin time is not earlier than the end time!"<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基本的 C++ 电子时钟实现,其重载了 + 和 - 运算符: ```c++ #include <iostream> using namespace std; class Clock { private: int hour, minute, second; public: Clock(int h = 0, int m = 0, int s = 0) { hour = h; minute = m; second = s; } void display() { cout << hour << ":" << minute << ":" << second << endl; } Clock operator+(const Clock& c) const { int h = hour + c.hour; int m = minute + c.minute; int s = second + c.second; if (s >= 60) { s -= 60; m++; } if (m >= 60) { m -= 60; h++; } if (h >= 24) { h -= 24; } return Clock(h, m, s); } Clock operator-(const Clock& c) const { int h = hour - c.hour; int m = minute - c.minute; int s = second - c.second; if (s < 0) { s += 60; m--; } if (m < 0) { m += 60; h--; } if (h < 0) { h += 24; } return Clock(h, m, s); } }; int main() { Clock c1(10, 30, 45), c2(12, 50, 15); cout << "c1 = "; c1.display(); cout << "c2 = "; c2.display(); Clock c3 = c1 + c2; cout << "c3 = "; c3.display(); Clock c4 = c2 - c1; cout << "c4 = "; c4.display(); return 0; } ``` 在上面的代码,Clock 类具有小时、分钟和秒属性,并且有一个默认构造函数。display() 函数用于显示当前时间。operator+() 和 operator-() 分别重载了 + 和 - 运算符,以使 Clock 对象可以相加和相减。 在主函数,我们创建了两个 Clock 对象 c1 和 c2,并显示了它们的值。然后我们通过重载的 + 运算符将它们相加,将结果存储在 c3 ,并将其显示出来。接下来,我们通过重载的 - 运算符将 c2 从 c1 减去,将结果存储在 c4 ,并将其显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值