C++第四天

代码:

cpp 复制代码
#include <iostream>
using namespace std;

// 定义时间类
class timee
{
private:
    int h; // 时
    int m; // 分
    int s; // 秒

public:
    // 无参构造
    timee();
    // 有参构造
    timee(int h, int m, int s);

    // 两个时间对象相加,表示时 分 秒各自相加,超过60 则向上进位
    timee operator+(timee &t);

    // 两个时间对象相减,表示时 分 秒各自相减,不够向上借位
    timee operator-(timee &t);

    // + - int n:表示秒增加n
    timee operator+(int n);
    timee operator-(int n);
    // 扩展下标
    int &operator[](int i);

    void show();
};

timee::timee() : h(0), m(0), s(0) {}

timee::timee(int h, int m, int s)
{
    this->h = h;
    this->m = m;
    this->s = s;
}

timee timee::operator+(timee &t)
{
    timee temp;

    temp.s = (s + t.s) % 60;
    temp.m = (m + t.m + (s + t.s) / 60) % 60;
    temp.h = (h + t.h + (m + t.m + (s + t.s) / 60) / 60);

    return temp;
}

timee timee::operator-(timee &t)
{
    timee temp;

    temp.s = s - t.s;
    if (temp.s < 0)
    {
        temp.m--;
        temp.s += 60;
    }
    temp.m = temp.m + m - t.m;
    if (temp.m < 0)
    {
        temp.h--;
        temp.m += 60;
    }
    temp.h = temp.h + h - t.h;
    if (temp.h < 0)
    {
        if (temp.s > 0)
        {
            temp.s = 60 - temp.s;
            temp.m++;
        }
        if (temp.m > 0)
        {
            temp.m = 60 - temp.m;
            temp.h++;
        }
    }

    return temp;
}

timee timee::operator+(int n)
{
    timee temp = *this;
    temp.s += n;
    temp.m += temp.s / 60;
    temp.h += temp.m / 60;
    temp.s %= 60;
    temp.m %= 60;
    return temp;
}

timee timee::operator-(int n)
{
    timee temp = *this;
    temp.s -= n;
    temp.m -= (temp.s) / -60;
    temp.h -= (temp.m) / -60;
    temp.m %= -60;
    temp.s %= -60;
    if (temp.s < 0)
    {
        temp.m--;
        temp.s += 60;
    }
    if (temp.m < 0)
    {
        temp.h--;
        temp.m += 60;
    }
    return temp;
}

int &timee::operator[](int i)
{
    switch (i)
    {
    case 0:
        return s;

    case 1:
        return m;

    case 2:
        return h;

    default:
        cout << "超出范围" << endl;
        return s;
    }
}

void timee::show()
{
    cout << h << "小时" << m << "分" << s << "秒" << endl;
}

int main(int argc, const char *argv[])
{
    timee tm;
    timee tm1(2, 20, 30);
    timee tm2(5, 0, 0);
    tm = tm1 + tm2;
    tm.show();
    tm = tm1 - tm2;
    tm.show();
    tm = tm1 - 80;
    tm.show();
    tm = tm2 + 90;
    tm.show();
    
    cout << "s = " << tm1[0] << " m = " << tm1[1] << " h = " << tm1[2] << endl;
    return 0;
}

运行结果:

相关推荐
且听风吟ayan3 分钟前
leetcode day20 滑动窗口209+904
算法·leetcode·c#
m0_675988233 分钟前
Leetcode350:两个数组的交集 II
算法·leetcode·数组·哈希表·python3
_Itachi__4 分钟前
LeetCode 热题 100 160. 相交链表
算法·leetcode·链表
a小胡哦6 分钟前
Windows、Mac、Linux,到底该怎么选?
linux·windows·macos·操作系统
m0_675988237 分钟前
Leetcode1206:设计跳表
算法·leetcode·跳表·python3
冠位观测者8 分钟前
【Leetcode 每日一题 - 扩展】1512. 好数对的数目
数据结构·算法·leetcode
Joyner20189 分钟前
python-leetcode-路径总和 III
算法·leetcode·职场和发展
南宫生9 分钟前
力扣每日一题【算法学习day.133】
java·学习·算法·leetcode
_Itachi__10 分钟前
LeetCode 热题 100 560. 和为 K 的子数组
数据结构·算法·leetcode
夏末秋也凉10 分钟前
力扣-贪心-55 跳跃游戏
算法·leetcode·游戏