应该是很简单的但不知道为什么就是过不了。。。救救孩子!
6-2 时间的比较*
分数 15
作者 李祥
单位 湖北经济学院
请设计时间类TIME,实现时间的输入、输出和比较。
#include
#include
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";
}
输出样例2
No
回答:简单写了一下,我还是喜欢Java,C++不熟悉;这里面还需要你对数字进行处理一下,然后对输入的数据进行判断,防止出现时间不符合现实的情况
# include <iostream>
using namespace std;
/* 你提交的代码将被嵌在这里 */
class TIME
{
int second;
int minute;
int hour;
public:
TIME()
{
this->second = 0;
this->minute = 0;
this->hour = 0;
}
TIME(int hour)
{
this->hour = hour;
}
TIME(int hour, int minute)
{
this->hour = hour;
this->minute = minute;
}
TIME(int hour, int minute, int second)
{
this->hour = hour;
this->minute = minute;
this->second = second;
}
bool operator > (TIME time)
{
if(this->hour > time.hour)
{
return true;
}
else if(this->hour == time.hour && this->minute > time.minute)
{
return true;
}
else if(this->hour == time.hour && this->minute == time.minute && this->second > time.second)
{
return true;
}
return false;
}
// 这里需要对是否补 0进行判断
friend ostream& operator << (ostream& os, TIME& time)
{
os<<time.hour<<":"<<time.minute<<":"<<time.second;
return os;
}
};
int main()
{
// TIME a(9, 15);
// cout << a << endl;
// TIME a(8, 50), b(9, 15, 47);
// cout << a << ' ' << b << endl;
TIME a(9, 0, 1), b(8, 59, 58);
if (a > b)
{
cout << "Yes\n";
}
else
{
cout << "No\n";
}
return 0;
}