B C++时间类的运算符重载

B C++时间类的运算符重载

时间: 1ms        内存:128M

描述:

C++时间类的运算符重载
定义一个时间类Time,其数据成员为表示时间的小时(hour)、分(minute),秒(second)。
重载运算符“+”,使之能用于时间对象的加法运算;重载运算符“<<”,使之能用于时间对象的输出操作。
(1)参加运算的两个操作数可以都是时间类对象,也可以其中有一个是整数(表示秒),顺序任意。
例如,t1+t2,i+t1,t1+i均合法(设i为整数表示秒数, t1,t2为时间类Time对象)。
(2)输出时间对象的方式为 小时:分:秒,例如 14:23:55、00:00:00、23:59:59等。

编写程序,分别求两个时间对象之和、整数和时间对象之和,时间对象和整数之和,并且输出。

请在下面的程序段基础上完成设计:
#include <iostream>
using namespace std;

class Time
{
public:
Time():hour(0),minute(0),second(0) {}
Time(int h,int m,int s) {
hour=(h>=24||h<0)?0:h;
minute=(m>=60||m<0)?0:m;
second=(s>=60||s<0)?0:s;
}
Time operator+(Time &);
Time operator+(int &);
friend Time operator+(int,Time &);
friend ostream& operator << (ostream& output, Time & c);
private:
int hour;
int minute;
int second;
};

//将程序需要的其他成份写在下面,只提交begin到end部分的代码
//******************** begin ********************


//********************* end ********************

int main()
{
//测试Time类对象加Time类对象
int hour,minute,second;
cin>>hour>>minute>>second;
Time t1(hour,minute,second);
cin>>hour>>minute>>second;
Time t2(hour,minute,second);
Time t3=t1+t2;
cout<<"t1+t2=";
cout<<t3;

//测试时间对象加整数
int i;
cin>>hour>>minute>>second;
cin>>i;
t3=Time(hour,minute,second)+i;
cout<<"t1+i=";
cout<<t3;

//测试整数加时间对象
cin>>i;
cin>>hour>>minute>>second;
t1=Time(hour,minute,second);
t3=i+t1;
cout<<"i+t1=";
cout<<t3;

return 0;

输入:

一个时间类的小时 分 秒,另一个时间类的小时 分 秒(注意要符合实际)
一个时间类的小时 分 秒,一个整数(表示秒数)
一个整数(表示秒数),一个时间类的小时 分 秒

输出:

两个时间之和、时间和整数之和,整数和时间之和。

示例输入:

1 2 3 4 5 6
0 0 0 200
59 14 59 1

示例输出:

t1+t2=5:7:9
t1+i=0:3:20
i+t1=15:0:0

提示:

参考答案(内存最优[1268]):


#include <iostream>
using namespace std;

class Time
{
public:
 Time():hour(0),minute(0),second(0) {}
 Time(int  h,int  m,int s) {
    hour=(h>=24||h<0)?0:h;
    minute=(m>=60||m<0)?0:m;
    second=(s>=60||s<0)?0:s;
}
Time operator+(Time &);
Time operator+(int &);
friend Time operator+(int,Time &);
friend ostream& operator << (ostream& output,  Time & c);
private:
  int  hour;
  int  minute;
  int  second;
};Time Time::operator+(Time & t)
{
  int hours=hour+t.hour;
  int minutes=minute+t.minute;
  int seconds=second+t.second;
  if(seconds>=60)
  {
	  minutes+=seconds/60;
	  seconds=seconds%60;
  }
  if(minutes>=60)
  {
      hours+=minutes/60;
	  minutes=minutes%60;
  }
  if(hours>=24)
  {
     hours=hours%24;
  }
  return Time(hours,minutes,seconds);
}

Time Time::operator+(int & i)
{ int hours=hour;
  int minutes=minute;
  int seconds=second+i;
  if(seconds>=60)
  {
	  minutes+=seconds/60;
	  seconds=seconds%60;
  }
  if(minutes>=60)
  {
      hours+=minutes/60;
	  minutes=minutes%60;
  }
  if(hours>=24)
  {
     hours=hours%24;
  }
  return Time(hours,minutes,seconds);
}

Time operator+(int i,Time & t)
{
  int hours=t.hour;
  int minutes=t.minute;
  int seconds=t.second+i;
  if(seconds>=60)
  {
	  minutes+=seconds/60;
	  seconds=seconds%60;
  }
  if(minutes>=60)
  {
      hours+=minutes/60;
	  minutes=minutes%60;
  }
  if(hours>=24)
  {
     hours=hours%24;
  }
  return Time(hours,minutes,seconds);
}

ostream& operator << (ostream& output,  Time & c)
{ output<<c.hour<<":"<<c.minute<<":"<<c.second<<endl;
  return output;
}
int main()
{
//测试Time类对象加Time类对象
int  hour,minute,second;
cin>>hour>>minute>>second;
Time t1(hour,minute,second);
cin>>hour>>minute>>second;
Time t2(hour,minute,second);
Time t3=t1+t2;
cout<<"t1+t2=";
cout<<t3;

//测试时间对象加整数
int i;
cin>>hour>>minute>>second;
cin>>i;
t3=Time(hour,minute,second)+i;
cout<<"t1+i=";
cout<<t3;

//测试整数加时间对象
cin>>i;
cin>>hour>>minute>>second;
t1=Time(hour,minute,second);
t3=i+t1;
cout<<"i+t1=";
cout<<t3;

return 0;
}

参考答案(时间最优[0]):


#include <iostream>
using namespace std;

class Time
{
public:
 Time():hour(0),minute(0),second(0) {}
 Time(int  h,int  m,int s) {
    hour=(h>=24||h<0)?0:h;
    minute=(m>=60||m<0)?0:m;
    second=(s>=60||s<0)?0:s;
}
Time operator+(Time &);
Time operator+(int &);
friend Time operator+(int,Time &);
friend ostream& operator << (ostream& output,  Time & c);
private:
  int  hour;
  int  minute;
  int  second;
};Time Time::operator+(Time & t)
{
  int hours=hour+t.hour;
  int minutes=minute+t.minute;
  int seconds=second+t.second;
  if(seconds>=60)
  {
	  minutes+=seconds/60;
	  seconds=seconds%60;
  }
  if(minutes>=60)
  {
      hours+=minutes/60;
	  minutes=minutes%60;
  }
  if(hours>=24)
  {
     hours=hours%24;
  }
  return Time(hours,minutes,seconds);
}

Time Time::operator+(int & i)
{ int hours=hour;
  int minutes=minute;
  int seconds=second+i;
  if(seconds>=60)
  {
	  minutes+=seconds/60;
	  seconds=seconds%60;
  }
  if(minutes>=60)
  {
      hours+=minutes/60;
	  minutes=minutes%60;
  }
  if(hours>=24)
  {
     hours=hours%24;
  }
  return Time(hours,minutes,seconds);
}

Time operator+(int i,Time & t)
{
  int hours=t.hour;
  int minutes=t.minute;
  int seconds=t.second+i;
  if(seconds>=60)
  {
	  minutes+=seconds/60;
	  seconds=seconds%60;
  }
  if(minutes>=60)
  {
      hours+=minutes/60;
	  minutes=minutes%60;
  }
  if(hours>=24)
  {
     hours=hours%24;
  }
  return Time(hours,minutes,seconds);
}

ostream& operator << (ostream& output,  Time & c)
{ output<<c.hour<<":"<<c.minute<<":"<<c.second<<endl;
  return output;
}
int main()
{
//测试Time类对象加Time类对象
int  hour,minute,second;
cin>>hour>>minute>>second;
Time t1(hour,minute,second);
cin>>hour>>minute>>second;
Time t2(hour,minute,second);
Time t3=t1+t2;
cout<<"t1+t2=";
cout<<t3;

//测试时间对象加整数
int i;
cin>>hour>>minute>>second;
cin>>i;
t3=Time(hour,minute,second)+i;
cout<<"t1+i=";
cout<<t3;

//测试整数加时间对象
cin>>i;
cin>>hour>>minute>>second;
t1=Time(hour,minute,second);
t3=i+t1;
cout<<"i+t1=";
cout<<t3;

return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注