鱼C论坛

 找回密码
 立即注册
查看: 551|回复: 1

[已解决]求助

[复制链接]
发表于 2023-12-21 18:28:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
编写一个程序,定义一个时间类Time,包含三个属性: hour, minute 和 second

要求通过运算符重载实现如下功能:
时间输入输出(>>、<<);

时间增加减少若干(+=、-=),例:Time& operator+=(const Time&);Time& operator-=(const Time&);

时间前、后自增加/减少1秒(++、--),前自增例:Time& operator++(); 后自增例:Time operator++(int);

【输入形式】

输入固定为两个Time实例(time1,time2),每个实例占一行;

Time实例输入格式为:hour minute second。

【输出形式】

Time实例输出格式为:hour:minute:second;

每个输出实例占一行。

依次输出以下表达式的值
time1 += (time2++)

time1 -= time2

++time2

time2 += (time1--)

--time1

time2 -= time1

【样例输入】

21 10 35
10 15 25

这题怎么写
最佳答案
2023-12-21 18:35:35
<<)
时间相等判断(==)
时间比较大小(>)
时间加法(+)
时间减法(-)

可以这样实现:
#include <iostream>using namespace std;class Time {public:    int hour;    int minute;    int second;    // 重载输入运算符    friend istream& operator>>(istream& input, Time& t) {        input >> t.hour >> t.minute >> t.second;        return input;    }    // 重载输出运算符    friend ostream& operator<<(ostream& output, const Time& t) {        output << t.hour << ":" << t.minute << ":" << t.second;        return output;    }    // 重载相等运算符    bool operator==(const Time& t) const {        return (hour == t.hour && minute == t.minute && second == t.second);    }    // 重载大于运算符    bool operator>(const Time& t) const {        if (hour > t.hour) {            return true;        } else if (hour == t.hour) {            if (minute > t.minute) {                return true;            } else if (minute == t.minute) {                if (second > t.second) {                    return true;                }            }        }        return false;    }    // 重载加法运算符    Time operator+(const Time& t) const {        Time result;        result.hour = hour + t.hour;        result.minute = minute + t.minute;        result.second = second + t.second;                if (result.second >= 60) {            result.minute += result.second / 60;            result.second %= 60;        }        if (result.minute >= 60) {            result.hour += result.minute / 60;            result.minute %= 60;        }        if (result.hour >= 24) {            result.hour %= 24;        }                return result;    }    // 重载减法运算符    Time operator-(const Time& t) const {        int thisSeconds = hour * 3600 + minute * 60 + second;        int tSeconds = t.hour * 3600 + t.minute * 60 + t.second;        int diffSeconds = thisSeconds - tSeconds;                Time result;        result.hour = diffSeconds / 3600;        result.minute = (diffSeconds % 3600) / 60;        result.second = diffSeconds % 60;                return result;    }};int main() {    Time t1, t2;    cout << "请输入第一个时间(hh:mm:ss): ";    cin >> t1;    cout << "请输入第二个时间(hh:mm:ss): ";    cin >> t2;    cout << "第一个时间为: " << t1 << endl;    cout << "第二个时间为: " << t2 << endl;    if (t1 == t2) {        cout << "两个时间相等" << endl;    } else {        cout << "两个时间不相等" << endl;    }    if (t1 > t2) {        cout << "第一个时间大于第二个时间" << endl;    } else {        cout << "第一个时间小于等于第二个时间" << endl;    }    Time sum = t1 + t2;    cout << "两个时间之和为: " << sum << endl;    Time diff = t1 - t2;    cout << "两个时间的差为: " << diff << endl;    return 0;}
希望对你有所帮助!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-21 18:35:35 | 显示全部楼层    本楼为最佳答案   
<<)
时间相等判断(==)
时间比较大小(>)
时间加法(+)
时间减法(-)

可以这样实现:
#include <iostream>using namespace std;class Time {public:    int hour;    int minute;    int second;    // 重载输入运算符    friend istream& operator>>(istream& input, Time& t) {        input >> t.hour >> t.minute >> t.second;        return input;    }    // 重载输出运算符    friend ostream& operator<<(ostream& output, const Time& t) {        output << t.hour << ":" << t.minute << ":" << t.second;        return output;    }    // 重载相等运算符    bool operator==(const Time& t) const {        return (hour == t.hour && minute == t.minute && second == t.second);    }    // 重载大于运算符    bool operator>(const Time& t) const {        if (hour > t.hour) {            return true;        } else if (hour == t.hour) {            if (minute > t.minute) {                return true;            } else if (minute == t.minute) {                if (second > t.second) {                    return true;                }            }        }        return false;    }    // 重载加法运算符    Time operator+(const Time& t) const {        Time result;        result.hour = hour + t.hour;        result.minute = minute + t.minute;        result.second = second + t.second;                if (result.second >= 60) {            result.minute += result.second / 60;            result.second %= 60;        }        if (result.minute >= 60) {            result.hour += result.minute / 60;            result.minute %= 60;        }        if (result.hour >= 24) {            result.hour %= 24;        }                return result;    }    // 重载减法运算符    Time operator-(const Time& t) const {        int thisSeconds = hour * 3600 + minute * 60 + second;        int tSeconds = t.hour * 3600 + t.minute * 60 + t.second;        int diffSeconds = thisSeconds - tSeconds;                Time result;        result.hour = diffSeconds / 3600;        result.minute = (diffSeconds % 3600) / 60;        result.second = diffSeconds % 60;                return result;    }};int main() {    Time t1, t2;    cout << "请输入第一个时间(hh:mm:ss): ";    cin >> t1;    cout << "请输入第二个时间(hh:mm:ss): ";    cin >> t2;    cout << "第一个时间为: " << t1 << endl;    cout << "第二个时间为: " << t2 << endl;    if (t1 == t2) {        cout << "两个时间相等" << endl;    } else {        cout << "两个时间不相等" << endl;    }    if (t1 > t2) {        cout << "第一个时间大于第二个时间" << endl;    } else {        cout << "第一个时间小于等于第二个时间" << endl;    }    Time sum = t1 + t2;    cout << "两个时间之和为: " << sum << endl;    Time diff = t1 - t2;    cout << "两个时间的差为: " << diff << endl;    return 0;}
希望对你有所帮助!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 12:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表