首页 > 试题广场 >

加号运算符重载

[编程题]加号运算符重载
  • 热度指数:8949 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
有一个时间类(Time),成员变量有:小时(hours)、分钟(minutes),补充 Time 类的代码,重载加号运算符,使得程序能够正确运行。

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


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

输入

1
10

输出

3 30
示例2

输入

2
50

输出

5 10
#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 res;            
            res.minutes = this -> minutes + t.minutes;
            res.hours = this -> hours + t.hours + res.minutes/60;
            res.minutes %= 60;
            return res;
        }

};

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

发表于 2022-01-15 11:30:25 回复(2)
考虑到可能存在输入分钟会有>=60的情况所以要在程序里面化简。
hours=hours+minutes/60;
minutes%=60;
发表于 2021-11-24 17:46:49 回复(0)
#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){
            int temp = t.minutes + minutes;
            int h = temp / 60;
            this->hours += t.hours + h;
            this->minutes = temp % 60;
            return *this;
        }

};

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

发表于 2024-04-28 15:59:41 回复(0)
#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 answer(this->hours + t.hours, this->minutes + t.minutes);
        while (answer.minutes >= 60) {
            answer.minutes -= 60;
            answer.hours++;
        }
        answer.hours %= 24;
        return answer;
    }

};

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

编辑于 2024-03-21 09:17:47 回复(0)
#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 t2)
        {
            hours  = hours + t2.hours;
            minutes = minutes + t2.minutes;
            if (minutes > 59) {
                minutes = minutes - 60;
                hours = hours + 1;
            
            }
            return  *this;
        }  

};

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

发表于 2023-10-05 16:42:09 回复(0)
#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)
        {
            int minutes1=this->minutes + t.minutes;
            int hours1=this->hours+t.hours;
            while(minutes1>=60)
            {
                minutes1=minutes1-60;
                hours1++;
            }
            return Time(hours1,minutes1);
        }

};

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

发表于 2023-05-21 20:12:07 回复(0)
#include<iostream>
using namespace std;
#include <string>
class Time
{
private:
	int hours, minutes;
public:
	Time()
	{
		this->hours = 0;
		this->minutes = 0;
	}
	Time(int hours, int minutes)
	{
		this->hours = hours;
		this->minutes = minutes;
	}
	Time operator+ (Time& t)
	{
		Time temp;
		if (this->minutes+t.minutes >= 60)
		{
			temp.minutes = this->minutes + t.minutes - 60;
			temp.hours = this->hours + t.hours + 1;
			return temp;
		}
		else
		{
			temp.minutes = this->minutes + t.minutes;
			temp.hours = this->hours + t.hours;
			return temp;
		}
	}
	void showTime()
	{
		cout << this->hours << " " << this->minutes << endl;
	}

};
void test01()
{
	int h, m;
	cin >> h >> m;
	Time t1(h, m);
	Time t2(2, 20);
	Time t3 = t1 + t2;
	t3.showTime();
}
int main()
{
	test01();
	return 0;
}

发表于 2023-02-06 22:02:48 回复(0)
#include <iostream>
using namespace std;

class Time {
    int hours;
    int minutes;
  public:
    Time(int hours, int minutes): hours(hours), minutes(minutes) {};
    const Time operator+(const Time& t) {
        int tminutes = this->minutes + t.minutes;
        int thours = this->hours + t.hours;
        if (tminutes >= 60) {
            thours++;
            tminutes -= 60;
        }
        return Time(thours, tminutes);
    }
    friend void prHM(const Time& t) {
        cout << t.hours << " " << t.minutes;
    }
};

int main() {
    int hours;
    int minutes;
    cin >> hours >> minutes;
    Time t1(hours, minutes);
    /*还得猜测给定的是2小时20分*/
    Time t2(2, 20);
    prHM(t1 + t2);
}
// 64 位输出请用 printf("%lld")

发表于 2022-12-28 21:04:13 回复(0)
#include <iostream>
using namespace std;

class Time{
    private:
        int hours;
        int minutes;
    public:
        void show();
        Time();
        void set_Time(int h, int m);   
        void set_Time(Time &t1, Time &t2);
};

Time::Time(){                                       //构造函数,参数初始化固定
    this->hours = 2;
    this->minutes = 20;
}

void Time::set_Time(int h , int m){                 //设置参数
    this->hours = h;
    this->minutes = m;
}
void Time::set_Time(Time &t1, Time &t2){            //重载,设置参数
    this->hours = t1.hours + t2.hours;
    this->minutes = t1.minutes + t2.minutes;
    if(this->minutes >= 60){
        this->hours ++;
        this->minutes -= 60;
    }
}

void Time::show(){                                  //打印
    cout << this->hours << " " << this->minutes << endl;
}

int main() {
    int h, m;
    Time t1, t2, t3;
    cin >> h >> m;
    t2.set_Time(h, m);
    t3.set_Time(t1, t2);
    t3.show();
}

发表于 2022-11-10 11:25:54 回复(0)
#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;
    }
    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;
    
}
// 64 位输出请用 printf("%lld")
发表于 2022-10-23 19:33:43 回复(0)
Time operator+(const Time &t){
    Time time;
    time.minutes = (this->minutes+t.minutes)%60;
    time.hours = (this->hours+t.hours+(this->minutes+t.minutes)/60);
    return time;
}

发表于 2022-07-13 21:08:03 回复(0)
注意:
p3=p1+p2的全写形式为p3=p1.operate+(p2)
一定要有返回值


        Time operator + (Time &t)
        {
            Time res;
            res.hours=this->hours+t.hours;
            res.minutes=this->minutes+t.minutes;
            if(res.minutes>=60)
            {
                res.hours=res.hours+1;
                res.minutes=res.minutes-60;
            }
            return res;
        }

发表于 2022-06-16 16:35:54 回复(0)
关于运算符重载,我在评论区留下一篇我觉得比较好的文章,希望有用,代码如下:
#include <iostream>
using namespace std;

class Time{
private:
    int hour,min;
public:
    Time(int x,int y):hour(x),min(y){};
    void get(){
        cout << hour << " " << min;
    }
    Time operator + (const Time& b);
};
Time Time:: operator + (const Time& b){
    min = min + b.min;
    if (min >= 60)
    {
        hour++;
        min = min - 60;
    }
    return Time(hour + b.hour,min);
}
int main(){
    int x,y;
    cin >> x >> y;
    Time a(x,y);
    Time b(2,20);
    Time c = a + b;
    c.get();
    return 0;
}

发表于 2022-06-04 11:49:53 回复(1)
别忘了进位!

发表于 2022-01-28 09:22:51 回复(0)
#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)
        {
            t.hours+=hours;
            t.minutes+=minutes;
            while(t.minutes >= 60)
            {
                t.minutes-=60;
                t.hours+=1;
            }
           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;
}
发表于 2021-12-22 23:35:21 回复(0)
发表于 2021-11-27 23:42:41 回复(0)

问题信息

难度:
16条回答 688浏览

热门推荐

通过挑战的用户

查看代码