【C++ | PTA】时间相加

文章描述了一个使用C++编写的Time类,该类包含小时、分钟和秒的私有数据成员,并通过重载+运算符实现了两个时间对象的相加。相加过程中考虑了时间范围的限制,确保小时不超过24小时,分钟不超过59分钟,秒不超过59秒。在main函数中,给出了一个示例,展示了如何创建时间对象并进行相加操作。

文章目录

题目要求

设计一个时间类,用来保存时、分、秒等私有数据成员,通过重载操作符+实现2个时间的相加。
要求:
(1)小时的时间范围限制在大于等于0;(2)分的时间范围为0-59分;(3)秒的时间范围为0-59秒。

#include <iostream>

using namespace std;

class Time {

private:

 int hours,minutes, seconds;

public:

 Time(int h=0, int m=0, int s=0);

 Time operator + (Time &);

 void DispTime();

};


/* 请在这里填写答案 */


int main() {

 Time tm1(8,75,50),tm2(0,6,16), tm3;

 tm3=tm1+tm2;

 tm3.DispTime();

 return 0;

}

在这里给出相应的输出。例如:
9h:22m:6s


代码

Time::Time(int h, int m, int s):hours(h),minutes(m),seconds(s){}
Time Time::operator+(Time& t) {
    int s = (t.seconds + this->seconds) % 60;
    int m = ((t.seconds + this->seconds) / 60 + t.minutes + this->minutes) % 60;
    int h = ((t.seconds + this->seconds) / 60 + t.minutes + this->minutes) / 60 + t.hours + this->hours;
    t.hours = h;
    t.minutes = m;
    t.seconds = s;
    return t;
}
void Time::DispTime() {
    cout << this->hours << "h:" << this->minutes << "m:" << this->seconds << "s";
}
### PTA 7-2 时间 C++ 实现解决方案 在C++中实现时间,通常需要定义一个来表示时间,并提供相应的构造函数、成员函数和运算符重载功能。以下是一个可能的实现方案,基于常见的需求,例如时间的加减操作、格式化输出等。 #### 设计 时间设计可以包括以下部分: 1. **私有成员变量**:用于存储小时、分钟和秒。 2. **公有成员函数**:包括构造函数、设置时间的函数、获取时间的函数、时间的加减操作以及格式化输出。 3. **运算符重载**:支持时间的加法和减法操作。 以下是完整的C++代码实现: ```cpp #include <iostream> #include <iomanip> using namespace std; class Time { private: int hour; int minute; int second; // 辅助函数:规范化时间 void normalize() { if (second >= 60) { minute += second / 60; second %= 60; } if (minute >= 60) { hour += minute / 60; minute %= 60; } if (hour >= 24) { hour %= 24; } } public: // 构造函数 Time(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s) { normalize(); } // 设置时间 void setTime(int h, int m, int s) { hour = h; minute = m; second = s; normalize(); } // 获取时间 void getTime(int &h, int &m, int &s) const { h = hour; m = minute; s = second; } // 格式化输出 void printTime() const { cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second; } // 运算符重载:时间相加 Time operator+(const Time &other) const { int totalSeconds = this->hour * 3600 + this->minute * 60 + this->second + other.hour * 3600 + other.minute * 60 + other.second; return Time(totalSeconds / 3600, (totalSeconds % 3600) / 60, totalSeconds % 60); } // 运算符重载:时间相减 Time operator-(const Time &other) const { int totalSeconds = this->hour * 3600 + this->minute * 60 + this->second - (other.hour * 3600 + other.minute * 60 + other.second); if (totalSeconds < 0) { totalSeconds += 24 * 3600; // 确保结果为正值(同一天内) } return Time(totalSeconds / 3600, (totalSeconds % 3600) / 60, totalSeconds % 60); } }; int main() { int h1, m1, s1, h2, m2, s2; cin >> h1 >> m1 >> s1 >> h2 >> m2 >> s2; Time t1(h1, m1, s1); Time t2(h2, m2, s2); Time sum = t1 + t2; Time diff = t1 - t2; cout << "Sum: "; sum.printTime(); cout << endl; cout << "Difference: "; diff.printTime(); cout << endl; return 0; } ``` #### 解释 1. **`normalize` 函数**:确保时间的小时、分钟和秒都在合理范围内[^1]。 2. **构造函数与 `setTime` 方法**:初始化或修改时间值,并调用 `normalize` 确保合法性。 3. **`printTime` 方法**:以固定宽度格式化输出时间,便于阅读。 4. **运算符重载**:通过将时间转换为总秒数进行加减操作,简化计算逻辑[^2]。 #### 注意事项 - 如果输入的时间超出合法范围(如分钟大于59),需通过 `normalize` 函数调整。 - 在时间相减时,若结果为负值,则假设时间为同一天内,因此加上24小时的总秒数[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋说

感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值