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

运行结果:

相关推荐
果子⌂6 分钟前
容器技术入门之Docker环境部署
linux·运维·docker
vvilkim14 分钟前
Nuxt.js 全面测试指南:从单元测试到E2E测试
开发语言·javascript·ecmascript
Tipriest_41 分钟前
Python异常类型介绍
开发语言·python·异常
Python×CATIA工业智造1 小时前
详细页智能解析算法:洞悉海量页面数据的核心技术
爬虫·算法·pycharm
深度学习04071 小时前
【Linux服务器】-安装ftp与sftp服务
linux·运维·服务器
Swift社区1 小时前
Swift 解 LeetCode 321:拼接两个数组中的最大数,贪心 + 合并全解析
开发语言·leetcode·swift
ruan1145141 小时前
Java Lambda 类型推断详解:filter() 方法与 Predicate<? super T>
java·开发语言·spring·stream
广东数字化转型1 小时前
java jar 启动应用程序
开发语言·python
阿幸软件杂货间1 小时前
阿幸课堂随机点名
android·开发语言·javascript
threelab2 小时前
three案例 Three.js波纹效果演示
开发语言·javascript·ecmascript