列表

详情


CPP43. 加号运算符重载

描述

有一个时间类(Time),成员变量有:小时(hours)、分钟(minutes),补充 Time 类的代码,重载加号运算符,使得程序能够正确运行。

输入描述

键盘输入两个正整数,分别为小时 h 和分钟 m。要求分钟 m 范围为 0 - 59

输出描述

输出两个 Time 对象(t1 和 t2)相加后的时间结果,通过调用 show() 输出。

示例1

输入:

1
10

输出:

3 30

示例2

输入:

2
50

输出:

5 10

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 320KB, 提交时间: 2022-01-22

#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        Time operator+(Time& a){
            Time sum;
            sum.hours=hours+a.hours+(minutes+a.minutes)/60;
            sum.minutes=(minutes+a.minutes)%60;
            return sum;
        }

};

int main() {

    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(2, 20);

    Time t3 = t1 + t2;
    t3.show();
    
    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 388KB, 提交时间: 2022-04-23

#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        Time operator+(Time& t){
            Time sum;
            sum.hours = hours + t.hours +(minutes+t.minutes)/60;
            sum.minutes = (minutes + t.minutes)%60;
            return sum;
        }

};

int main() {

    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(2, 20);

    Time t3 = t1 + t2;
    t3.show();
    
    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 388KB, 提交时间: 2022-01-21

#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        Time operator+(const Time& t) {
            Time sum;
            sum.hours = hours + t.hours + (minutes + t.minutes) / 60;
            sum.minutes = ( minutes + t.minutes ) % 60;
            return sum;
        }

};

int main() {

    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(2, 20);

    Time t3 = t1 + t2;
    t3.show();
    
    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2021-11-13

#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        Time operator+(const Time& t1)
        {
            Time t;
            t.hours = this->hours+t1.hours;
            t.minutes = this->minutes+t1.minutes;
            if (t.minutes >=60)
            {
                t.hours++;
                t.minutes -= 60;
            }
            return t;
        }

};

int main() {

    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(2, 20);

    Time t3 = t1 + t2;
    t3.show();
    
    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2021-10-30

#include <iostream>
using namespace std;

class Time {

    public:
        int hours;      // 小时
        int minutes;    // 分钟

        Time() {
            hours = 0;
            minutes = 0;
        }

        Time(int h, int m) {
            this->hours = h;
            this->minutes = m;
        }

        void show() {
            cout << hours << " " << minutes << endl;
        }

        // write your code here......
        Time operator+(Time& t){
            Time sum;
            sum.hours = hours + t.hours + (minutes + t.minutes) / 60;  //小时数相加,加上分钟数多余60进1
            sum.minutes = (minutes + t.minutes) % 60; //分钟数要对60取余
            return sum;
        }

};

int main() {

    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(2, 20);

    Time t3 = t1 + t2;
    t3.show();
    
    return 0;
}

上一题