C++第四天

代码:

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

运行结果:

相关推荐

  1. C++作业

    2024-01-05 12:04:03       6 阅读
  2. C语言学习

    2024-01-05 12:04:03       10 阅读
  3. c++基础学习(内存分区,引用)

    2024-01-05 12:04:03       21 阅读
  4. 开始学习

    2024-01-05 12:04:03       36 阅读
  5. 实习记录——

    2024-01-05 12:04:03       27 阅读
  6. 练气

    2024-01-05 12:04:03       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-05 12:04:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-05 12:04:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-05 12:04:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-05 12:04:03       20 阅读

热门阅读

  1. 50、Flink的单元测试介绍及示例

    2024-01-05 12:04:03       32 阅读
  2. flutter 项目安卓开启混淆防止渗透

    2024-01-05 12:04:03       37 阅读
  3. LN和BN

    LN和BN

    2024-01-05 12:04:03      30 阅读
  4. cnn lstm结合网络

    2024-01-05 12:04:03       42 阅读
  5. uniapp vue 前端页面半小时选择器

    2024-01-05 12:04:03       32 阅读
  6. 项目管理的细节-平衡

    2024-01-05 12:04:03       35 阅读
  7. 哈希查找【数据结构】

    2024-01-05 12:04:03       34 阅读
  8. Spring中的依赖注入(DI)的几种方法的使用

    2024-01-05 12:04:03       27 阅读
  9. 后台管理系统权限处理

    2024-01-05 12:04:03       46 阅读
  10. k8s三种常用的项目发布方式

    2024-01-05 12:04:03       37 阅读
  11. Ubuntu envs setting

    2024-01-05 12:04:03       33 阅读
  12. C#请求队列后台服务

    2024-01-05 12:04:03       34 阅读
  13. Vue3.4的新变化

    2024-01-05 12:04:03       37 阅读