鱼C论坛

 找回密码
 立即注册
查看: 371|回复: 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
<<)
时间相等判断(==)
时间比较大小(>)
时间加法(+)
时间减法(-)

可以这样实现:
  1. #include <iostream>
  2. using namespace std;
  3. class Time {
  4. public:
  5.     int hour;
  6.     int minute;
  7.     int second;
  8.     // 重载输入运算符
  9.     friend istream& operator>>(istream& input, Time& t) {
  10.         input >> t.hour >> t.minute >> t.second;
  11.         return input;
  12.     }
  13.     // 重载输出运算符
  14.     friend ostream& operator<<(ostream& output, const Time& t) {
  15.         output << t.hour << ":" << t.minute << ":" << t.second;
  16.         return output;
  17.     }
  18.     // 重载相等运算符
  19.     bool operator==(const Time& t) const {
  20.         return (hour == t.hour && minute == t.minute && second == t.second);
  21.     }
  22.     // 重载大于运算符
  23.     bool operator>(const Time& t) const {
  24.         if (hour > t.hour) {
  25.             return true;
  26.         } else if (hour == t.hour) {
  27.             if (minute > t.minute) {
  28.                 return true;
  29.             } else if (minute == t.minute) {
  30.                 if (second > t.second) {
  31.                     return true;
  32.                 }
  33.             }
  34.         }
  35.         return false;
  36.     }
  37.     // 重载加法运算符
  38.     Time operator+(const Time& t) const {
  39.         Time result;
  40.         result.hour = hour + t.hour;
  41.         result.minute = minute + t.minute;
  42.         result.second = second + t.second;
  43.         
  44.         if (result.second >= 60) {
  45.             result.minute += result.second / 60;
  46.             result.second %= 60;
  47.         }
  48.         if (result.minute >= 60) {
  49.             result.hour += result.minute / 60;
  50.             result.minute %= 60;
  51.         }
  52.         if (result.hour >= 24) {
  53.             result.hour %= 24;
  54.         }
  55.         
  56.         return result;
  57.     }
  58.     // 重载减法运算符
  59.     Time operator-(const Time& t) const {
  60.         int thisSeconds = hour * 3600 + minute * 60 + second;
  61.         int tSeconds = t.hour * 3600 + t.minute * 60 + t.second;
  62.         int diffSeconds = thisSeconds - tSeconds;
  63.         
  64.         Time result;
  65.         result.hour = diffSeconds / 3600;
  66.         result.minute = (diffSeconds % 3600) / 60;
  67.         result.second = diffSeconds % 60;
  68.         
  69.         return result;
  70.     }
  71. };
  72. int main() {
  73.     Time t1, t2;
  74.     cout << "请输入第一个时间(hh:mm:ss): ";
  75.     cin >> t1;
  76.     cout << "请输入第二个时间(hh:mm:ss): ";
  77.     cin >> t2;
  78.     cout << "第一个时间为: " << t1 << endl;
  79.     cout << "第二个时间为: " << t2 << endl;
  80.     if (t1 == t2) {
  81.         cout << "两个时间相等" << endl;
  82.     } else {
  83.         cout << "两个时间不相等" << endl;
  84.     }
  85.     if (t1 > t2) {
  86.         cout << "第一个时间大于第二个时间" << endl;
  87.     } else {
  88.         cout << "第一个时间小于等于第二个时间" << endl;
  89.     }
  90.     Time sum = t1 + t2;
  91.     cout << "两个时间之和为: " << sum << endl;
  92.     Time diff = t1 - t2;
  93.     cout << "两个时间的差为: " << diff << endl;
  94.     return 0;
  95. }
复制代码

希望对你有所帮助!如果还有其他问题,请随时提问。

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

使用道具 举报

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

可以这样实现:
  1. #include <iostream>
  2. using namespace std;
  3. class Time {
  4. public:
  5.     int hour;
  6.     int minute;
  7.     int second;
  8.     // 重载输入运算符
  9.     friend istream& operator>>(istream& input, Time& t) {
  10.         input >> t.hour >> t.minute >> t.second;
  11.         return input;
  12.     }
  13.     // 重载输出运算符
  14.     friend ostream& operator<<(ostream& output, const Time& t) {
  15.         output << t.hour << ":" << t.minute << ":" << t.second;
  16.         return output;
  17.     }
  18.     // 重载相等运算符
  19.     bool operator==(const Time& t) const {
  20.         return (hour == t.hour && minute == t.minute && second == t.second);
  21.     }
  22.     // 重载大于运算符
  23.     bool operator>(const Time& t) const {
  24.         if (hour > t.hour) {
  25.             return true;
  26.         } else if (hour == t.hour) {
  27.             if (minute > t.minute) {
  28.                 return true;
  29.             } else if (minute == t.minute) {
  30.                 if (second > t.second) {
  31.                     return true;
  32.                 }
  33.             }
  34.         }
  35.         return false;
  36.     }
  37.     // 重载加法运算符
  38.     Time operator+(const Time& t) const {
  39.         Time result;
  40.         result.hour = hour + t.hour;
  41.         result.minute = minute + t.minute;
  42.         result.second = second + t.second;
  43.         
  44.         if (result.second >= 60) {
  45.             result.minute += result.second / 60;
  46.             result.second %= 60;
  47.         }
  48.         if (result.minute >= 60) {
  49.             result.hour += result.minute / 60;
  50.             result.minute %= 60;
  51.         }
  52.         if (result.hour >= 24) {
  53.             result.hour %= 24;
  54.         }
  55.         
  56.         return result;
  57.     }
  58.     // 重载减法运算符
  59.     Time operator-(const Time& t) const {
  60.         int thisSeconds = hour * 3600 + minute * 60 + second;
  61.         int tSeconds = t.hour * 3600 + t.minute * 60 + t.second;
  62.         int diffSeconds = thisSeconds - tSeconds;
  63.         
  64.         Time result;
  65.         result.hour = diffSeconds / 3600;
  66.         result.minute = (diffSeconds % 3600) / 60;
  67.         result.second = diffSeconds % 60;
  68.         
  69.         return result;
  70.     }
  71. };
  72. int main() {
  73.     Time t1, t2;
  74.     cout << "请输入第一个时间(hh:mm:ss): ";
  75.     cin >> t1;
  76.     cout << "请输入第二个时间(hh:mm:ss): ";
  77.     cin >> t2;
  78.     cout << "第一个时间为: " << t1 << endl;
  79.     cout << "第二个时间为: " << t2 << endl;
  80.     if (t1 == t2) {
  81.         cout << "两个时间相等" << endl;
  82.     } else {
  83.         cout << "两个时间不相等" << endl;
  84.     }
  85.     if (t1 > t2) {
  86.         cout << "第一个时间大于第二个时间" << endl;
  87.     } else {
  88.         cout << "第一个时间小于等于第二个时间" << endl;
  89.     }
  90.     Time sum = t1 + t2;
  91.     cout << "两个时间之和为: " << sum << endl;
  92.     Time diff = t1 - t2;
  93.     cout << "两个时间的差为: " << diff << endl;
  94.     return 0;
  95. }
复制代码

希望对你有所帮助!如果还有其他问题,请随时提问。

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-11 16:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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