设计24小时制的时间类Time,成员数据包含时(hour)分(minute)秒(second)。 main函数已给定,提交时只需要提交main函数外的代码部分。 #include<iostream> using namespace std; //你提交的代码在这里 int main() { Time t1; cout<<"Show object t1:"<<t1<<endl; int hour,minute,second; cin>>hour>>minute>>second; Time t2(hour,minute,second); cout<<"Show object t2:"<<t2<<endl; cin>>hour>>minute>>second; t1.Set(hour,minute,second); cout<<"Reset and Show object t1:"<<t1<<endl; int n; n=t2-t1; cout<<t2<<"-"<<t1<<"="<<n<<"s"<<endl; cin>>n; cout<<t1<<"+"<<n<<"s="; t2=t1+n; cout<<t2<<endl; cout<<t1<<"-"<<n<<"s="; t1=t1-n; cout<<t1<<endl; return 0; } Input 3行,第1,2行,每行3个整数,分别表示时、分和秒;第3行时一个非负整数。数据保证合法。 Output 按照样例输出格式输出。 输入样例 1 23 55 30 21 34 28 10000 输出样例 1 Show object t1:1:2:3 Show object t2:23:55:30 Reset and Show object t1:21:34:28 23:55:30-21:34:28=8462s 21:34:28+10000s=0:21:8 21:34:28-10000s=18:47:48
时间: 2025-05-24 21:11:57 浏览: 22
### 设计与实现 C++ 时间类 `Time`
为了满足需求,可以设计一个名为 `Time` 的 C++ 类来表示 24 小时制的时间。以下是具体的设计思路:
#### 功能描述
1. **私有成员变量**: 存储小时 (`hour`)、分钟 (`minute`) 和秒 (`second`)。
2. **公有方法**:
- 设置时间和验证时间的有效性。
- 获取当前存储的时间值。
- 支持时间的加法和减法运算。
3. **重载操作符**:
- 输入/输出流操作符 (`<<`, `>>`)。
4. **辅助函数**:
- 验证输入时间是否合法。
---
#### 实现代码
```cpp
#include <iostream>
using namespace std;
class Time {
private:
int hour;
int minute;
int second;
// 辅助函数:判断时间是否有效
bool isValid(int h, int m, int s) const {
return (h >= 0 && h < 24) && (m >= 0 && m < 60) && (s >= 0 && s < 60);
}
public:
// 构造函数,默认时间为 00:00:00
Time(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s) {
if (!isValid(hour, minute, second)) {
throw invalid_argument("Invalid time provided.");
}
}
// 设置时间
void setTime(int h, int m, int s) {
if (isValid(h, m, s)) {
hour = h;
minute = m;
second = s;
} else {
throw invalid_argument("Invalid time provided.");
}
}
// 获取时间
void getTime(int &h, int &m, int &s) const {
h = hour;
m = minute;
s = second;
}
// 加法运算
void add(Time other) {
int totalSeconds = toTotalSeconds() + other.toTotalSeconds();
fromTotalSeconds(totalSeconds);
}
// 减法运算
void subtract(Time other) {
int totalSeconds = toTotalSeconds() - other.toTotalSeconds();
if (totalSeconds < 0) {
throw underflow_error("Resulting time cannot be negative.");
}
fromTotalSeconds(totalSeconds);
}
// 转换为总秒数
int toTotalSeconds() const {
return hour * 3600 + minute * 60 + second;
}
// 从总秒数转换回时间
void fromTotalSeconds(int totalSeconds) {
hour = totalSeconds / 3600 % 24;
minute = totalSeconds / 60 % 60;
second = totalSeconds % 60;
}
// 重载 << 输出操作符
friend ostream& operator<<(ostream &os, const Time &t) {
os << t.hour << ":" << t.minute << ":" << t.second;
return os;
}
// 重载 >> 输入操作符
friend istream& operator>>(istream &is, Time &t) {
is >> t.hour >> t.minute >> t.second;
if (!t.isValid(t.hour, t.minute, t.second)) {
throw invalid_argument("Invalid time input.");
}
return is;
}
};
// 主函数测试
int main() {
try {
Time t1(12, 30, 45); // 初始化时间对象
cout << "Initial Time: " << t1 << endl;
Time t2;
cout << "Enter another time (hh:mm:ss): ";
cin >> t2; // 用户输入时间
cout << "You entered: " << t2 << endl;
// 执行加法
t1.add(t2);
cout << "After adding: " << t1 << endl;
// 执行减法
t1.subtract(t2);
cout << "After subtracting: " << t1 << endl;
} catch (const exception &e) {
cerr << e.what() << endl;
}
return 0;
}
```
---
#### 关键点解析
1. **有效性检查**
使用 `isValid()` 方法确保时间始终处于合理范围内[^1]。
2. **时间计算逻辑**
- 将时间统一转化为秒数进行加减运算,便于处理溢出情况。
- 运算完成后通过 `fromTotalSeconds()` 方法还原为标准时间格式[^2]。
3. **异常处理**
对非法输入抛出异常,增强程序健壮性[^3]。
4. **操作符重载**
提供友好的输入输出接口,简化交互过程。
---
###
阅读全文
相关推荐


















