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

运行结果:

相关推荐
醍醐三叶40 分钟前
C++类与对象--2 对象的初始化和清理
开发语言·c++
phoenix@Capricornus1 小时前
反向传播算法——矩阵形式递推公式——ReLU传递函数
算法·机器学习·矩阵
Magnum Lehar2 小时前
3d游戏引擎EngineTest的系统实现3
java·开发语言·游戏引擎
Inverse1622 小时前
C语言_动态内存管理
c语言·数据结构·算法
数据与人工智能律师2 小时前
虚拟主播肖像权保护,数字时代的法律博弈
大数据·网络·人工智能·算法·区块链
Mcworld8572 小时前
java集合
java·开发语言·windows
成功人chen某2 小时前
配置VScodePython环境Python was not found;
开发语言·python
QQ2740287562 小时前
Soundness Gitpod 部署教程
linux·运维·服务器·前端·chrome·web3
wuqingshun3141592 小时前
蓝桥杯 16. 外卖店优先级
c++·算法·职场和发展·蓝桥杯·深度优先
qwfys2002 小时前
How to configure Linux mint desktop
linux·desktop·configure·mint