c++请设计时间类TIME,实现时间的输入、输出和比较。 #include <iostream> #include <iomanip> using namespace std; /* 你提交的代码将被嵌在这里 */ int main() { TIME a, b; cin >> a >> b; if (a > b) { cout << "Yes\n"; } else { cout << "No\n"; } return 0; } 输入样例 9:0:1 8:59:58 输出样例 Yes 设计要求: 设计构造函数,达到以下效果。 测试代码1 TIME a; cout << a << endl; 输出样例1 00:00:00 测试代码2 TIME a(8); cout << a << endl; 输出样例2 08:00:00 测试代码3 TIME a(8, 30); cout << a << endl; 输出样例3 08:30:00 测试代码4 TIME a(8, 30, 45); cout << a << endl; 输出样例4 08:30:45 重载输出运算符函数,达到以下效果。 测试代码1 TIME a(9, 15) cout << a << endl; 输出样例1 09:15:00 测试代码2 TIME a(8, 50), b(9, 15, 47); cout << a << ' ' << b << endl; 输出样例2 08:50 09:15:47 重载输入运算符函数,达到以下效果。 测试代码1 TIME a; cin >> a; cout << a << endl; 输出样例1 8:3:5 08:03:05 测试代码2 TIME a, b; cin >> a >> b; cout << a << ' ' << b << endl; 输出样例2 0:0:1 23:0:0 00:00:01 23:00:00 重载大于运算符,达到以下效果。 测试代码1 TIME a(9, 0, 1), b(8, 59, 58); if (a > b) { cout << "Yes\n"; } else { cout << "No\n"; } 输出样例1 Yes 测试代码2 TIME a(3, 4, 5), b(2), c, d(1, 59); if (a > b && c > d) { cout << "Yes\n"; } else { cout << "No\n"; }
时间: 2024-02-20 08:58:26 浏览: 20
代码如下:
```c++
#include <iostream>
#include <iomanip>
using namespace std;
class TIME {
private:
int hour;
int minute;
int second;
public:
TIME() {
hour = 0;
minute = 0;
second = 0;
}
TIME(int h) {
hour = h;
minute = 0;
second = 0;
}
TIME(int h, int m) {
hour = h;
minute = m;
second = 0;
}
TIME(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
friend ostream& operator<<(ostream& out, const TIME& t) {
out << setfill('0') << setw(2) << t.hour << ':'
<< setw(2) << t.minute << ':' << setw(2) << t.second;
return out;
}
friend istream& operator>>(istream& in, TIME& t) {
char c;
in >> t.hour >> c >> t.minute >> c >> t.second;
return in;
}
friend bool operator>(const TIME& a, const TIME& b) {
if (a.hour > b.hour) return true;
if (a.hour < b.hour) return false;
if (a.minute > b.minute) return true;
if (a.minute < b.minute) return false;
if (a.second > b.second) return true;
if (a.second < b.second) return false;
return false;
}
};
int main() {
TIME a, b;
cin >> a >> b;
if (a > b) {
cout << "Yes\n";
} else {
cout << "No\n";
}
return 0;
}
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)