首页 > 试题广场 >

重载小于号

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

输入描述:
键盘输入两个整数,分别为小时 和分钟 ,其中


输出描述:
比较输入时间与6小时6分钟的大小,若输入时间较小则输出"yes",否则输出"no"。
示例1

输入

6 5

输出

yes
        bool operator<(const Time& t)
        {
            return hours*60+minutes < t.hours*60+t.minutes;
        }

发表于 2022-02-14 17:40:16 回复(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......
    bool operator<(const Time& t) {
        return hours < t.hours || hours == t.hours && minutes < t.minutes;
    }

};

int main() {
    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(6, 6);

    if (t1 < t2) cout << "yes";
    else cout << "no";
    return 0;
}

编辑于 2024-03-21 09:19:44 回复(0)
bool operator<(const Time& time) {
   return hours < time.hours || minutes <  time.minutes;
}

发表于 2023-04-27 18:31:26 回复(0)
#include<iostream>
using namespace std;
class Time
{
private:
	int hours, minutes;
public:
	Time(int hours, int minutes)
	{
		this->hours = hours;
		this->minutes = minutes;
	}
	bool operator<(const Time& t)
	{
		if (this->hours < t.hours)
			return true;
		else if (this->hours == t.hours && this->minutes < t.minutes)
			return true;
		else
			return false;
	}
};

void test01()
{
	int h, m;
	cin >> h >> m;
	Time t1(h, m), t2(6, 6);
	cout<<(t1 < t2 ? "yes" : "no" )<< endl;
}
int main()
{
	test01();
	return 0;
}

发表于 2023-02-07 21:02:27 回复(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;
    }
    bool operator<(Time &t){
        if(hours < t.hours){
            return true;
        }
        else if(hours == t.hours and minutes < t.minutes){
            return true;
        }
        else{
            return false;
        }
    }
};

int main() {
    int h,m;
    cin >> h >> m;

    Time t1(h,m);
    Time t2(6,6);

    if(t1 < t2){
        cout << "yes";
    }
    else{
        cout << "no";
    }

    return 0;
}
// 64 位输出请用 printf("%lld")
发表于 2022-10-23 19:26:32 回复(0)
#include <iostream>
using namespace std;

class Time {
  public:
    int hours;      // 小时
    int minutes;    // 分钟

    Time(int h, int m) { //构造函数
        hours = h;
        minutes = m;
    }
    int operator<(Time& t) {
        int flag;
        hours < t.hours ? flag = 1 : (hours == t.hours ? (minutes < t.minutes ? flag =
                                          1 : flag = 0) : flag = 0);
        return flag;
    }
    ~Time(){}
};

int main() {
    int h, m;
    cin >> h >>m;
    Time t1(h, m);
    Time t2(6, 6);

    if(t1<t2) cout<< "yes";
    else cout << "no";

    return 0;
}

发表于 2022-09-19 11:10:32 回复(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......
        //重载为类的成员函数
        bool operator <(Time t)
        {
        //两种情况
            if((hours<t.hours)||((hours==t.hours)&&(minutes<t.minutes)))
            {
                return true;
            }
            return false;
        }

};

int main() {
    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(6, 6);
    
    if (t1<t2) cout<<"yes"; else cout<<"no";
    return 0;
}

发表于 2022-07-16 13:56:05 回复(0)
bool operator<(const Time &t){
    bool flag;
    if(this->hours < t.hours)
        flag = true;
    else if(this->hours > t.hours)
        flag = false;
    else{
        if(this->minutes < t.minutes)
            flag = true;
        else
            flag = false;
        }
    return flag;
}

发表于 2022-07-13 21:19:19 回复(0)
返回值类型不一定是与类一致,按需求设置

        bool operator < (Time &t)
        {
            int temp1,temp2;
            temp1=this->hours*60+this->minutes;
            temp2=t.hours*60+t.minutes;
            if(temp1<temp2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

发表于 2022-06-16 16:44:31 回复(0)
自己想的没有一楼的简便,大家可以采取他的想法,代码如下:
#include <iostream>
using namespace std;

class Time {
private:
    int hour,min;
public:
    Time (int x,int y):hour(x),min(y){};
    bool operator < (const Time& t2){
       if (hour == t2.hour){
           return min >= t2.min ? 0 : 1;
       }
        else{
            return hour > t2.hour ? 0 : 1;
        }
    }
};

int main() {
    int h, m;
    cin >> h;
    cin >> m;
    Time t1(h, m);
    Time t2(6, 6);
    if (t1 < t2) cout<<"yes";
    else cout<<"no";
    return 0;
}
发表于 2022-06-04 12:05:03 回复(1)