编译器:C++ (g++)
请设计时间类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;
}
Ans:
class TIME {
private:
int hours;
int minutes;
int seconds;
public:
// 构造函数
TIME() : hours(0), minutes(0), seconds(0) {}
// 重载输入运算符
friend istream& operator>>(istream& input, TIME& time) {
char colon;
input >> time.hours >> colon >> time.minutes >> colon >> time.seconds;
return input;
}
// 重载输出运算符
friend ostream& operator<<(ostream& output, const TIME& time) {
output << setfill('0') << setw(2) << time.hours << ":"
<< setfill('0') << setw(2) << time.minutes << ":"
<< setfill('0') << setw(2) << time.seconds;
return output;
}
// 重载大于运算符
friend bool operator>(const TIME& left, const TIME& right) {
if (left.hours > right.hours) {
return true;
} else if (left.hours == right.hours) {
if (left.minutes > right.minutes) {
return true;
} else if (left.minutes == right.minutes) {
return left.seconds > right.seconds;
}
}
return false;
}
};
409



被折叠的 条评论
为什么被折叠?



