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

时间限制: 1 Sec  内存限制: 128 MB

提交: 284  解决: 108

题目描述

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

提示

只提交自己定义的函数部分

迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

#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+(int &s)
{
Time t1;
t1.second=s+second;
t1.minute=minute;
t1.hour=hour;
if(t1.second>59)
{
t1.minute+=(t1.second/60);
t1.second%=60;
}
if(t1.minute>59)
{
t1.hour+=(t1.minute/60);
t1.minute%=60;
}
if(t1.hour>23)
t1.hour%=24;
return t1;
}
Time Time::operator+(Time & t)
{
int h,m,s;
s=second+t.second;
m=minute+t.minute;
h=hour+t.hour;
if(s>59)
{
m+=(s/60);
s%=60;
}
if(m>59)
{
h+=(m/60);
m%=60;
}
if(h>23)
h%=24;
Time t1(h,m,s);
return t1;
}
ostream& operator << (ostream& output, Time & c)
{
output<<c.hour<<":"<<c.minute<<":"<<c.second<<endl;
return output;
}
Time operator+(int s,Time & t)
{
Time t1;
t1.second=s+t.second;
t1.minute=t.minute;
t1.hour=t.hour;
if(t1.second>59)
{
t1.minute+=(t1.second/60);
t1.second%=60;
}
if(t1.minute>59)
{
t1.hour+=(t1.minute/60);
t1.minute%=60;
}
if(t1.hour>23)
t1.hour%=24;
return t1;
}
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;
}

YTU 2617: B C++时间类的运算符重载的更多相关文章

  1. 问题 C: B C++时间类的运算符重载

    题目描述 C++时间类的运算符重载 定义一个时间类Time,其数据成员为表示时间的小时(hour).分(minute),秒(second). 重载运算符“+”,使之能用于时间对象的加法运算:重载运算符 ...

  2. sdut 4-1 复数类的运算符重载

    4-1 复数类的运算符重载 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 通过本题目的练习能够掌握成员运算符重载及友元运算符重载 要求定义一个复数类.重 ...

  3. string类中运算符重载实现

    C++中预定义的加.减等运算符的操作对象只能是基本的数据类型.如果要在用户自定义的类型对象上应用同样的运算符,就需要通过运算符重载来重新定义其实现,使它能够用于自定义类型执行特定的操作,所以运算符重载 ...

  4. c++String类的运算符重载---21

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/  一,创建测试程序包 测试代码如下: /* Date: 2017-5-4 * Descripti ...

  5. 问题 B: 矩形类中运算符重载【C++】

    题目描述 定义一个矩形类,数据成员包括左下角和右上角坐标,定义的成员函数包括必要的构造函数.输入坐标的函数,实现矩形加法,以及计算并输出矩形面积的函数.要求使用提示中给出的测试函数并不得改动. 两个矩 ...

  6. C# 类型运算符重载在类继承中的调用测试

    这是一篇晦涩难懂的片面的研究 一,简单的继承层次 class CA { } class CB : CA{ } class CC : CB{ } } void Test(CA oa){//CATest ...

  7. C++中的运算符重载注意事项

    1.C++中的运算符重载的方式有三种: a.类成员函数重载 b.友元函数重载 c.普通函数重载 注意: a.我们主要使用的方式主要是用:类成员函数和友元函数来实现运算符的重载. b.其实用普通函数理论 ...

  8. 《挑战30天C++入门极限》C++运算符重载转换运算符

        C++运算符重载转换运算符 为什么需要转换运算符? 大家知道对于内置类型的数据我们可以通过强制转换符的使用来转换数据,例如(int)2.1f;自定义类也是类型,那么自定义类的对象在很多情况下也 ...

  9. 《挑战30天C++入门极限》C++中利用构造函数与无名对象简化运算符重载函数

        C++中利用构造函数与无名对象简化运算符重载函数 在完整描述思想之前,我们先看一下如下的例子,这个例子中的加运算符重载是以非成员函数的方式出现的: //程序作者:管宁  //站点:www.cn ...

随机推荐

  1. 【CodeForces】【#285】Div.2

    生平第一场Codeforce……纪念一下,虽然跪的跟渣渣似的……啊不就是跪成渣渣了…… A.B暴力过去的……不知道会不会超时……C我犯了个2B错误,让输出总共多少条边,我都求出来边集E了……直接输出E ...

  2. Leetcode#123 Best Time to Buy and Sell Stock III

    原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...

  3. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. BLUR

    ssao的blur遇到个麻烦 花了两三天时间...终于大概知道原因了. 在nvidia的ssao(http://developer.download.nvidia.com/SDK/10.5/direc ...

  5. 在linux中使用phpize安装php扩展模块

    介绍:linux系统中,php安装成功后,在bin目录下会生成一个名叫phpize的可执行脚本,这个脚本的用途是动态安装php扩展模块.使用phpize脚本安装php扩展模块的好处:在安装php时没有 ...

  6. js java正则表达式替换手机号4-7位为星*号

    需求: 一个手机号13152461111,由于安全性,需要替换4-7位字符串为星号,为131****1111,那么有2中玩法,一种是前端隐藏,一种是后台隐藏. 1. 前台隐藏 <!DOCTYPE ...

  7. HDU 1028 Ignatius and the Princess III (递归,dp)

    以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802  Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...

  8. UVA 10635 Prince and Princess

    题意描述:有两个长度分别为p+1和q+1的序列,每个元素中的各个元素互不相同.都是1~n^2之间的整数,求A和B的最长公共子序列.(2<=n<=250,1<=p,q<=n^2) ...

  9. SQL 比较乱

    --DROP TABLE T_UserInfo---------------------------------------------------- --建测试表 CREATE TABLE T_Us ...

  10. mysql limit

    select * from tablename limit 1,4即取出第2条至第5条,4条记录