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. Oracle创建表时涉及的参数解析

    1.oracle pctfree和pctused详解   http://www.cnblogs.com/linjiqin/archive/2012/01/16/2323320.html http:// ...

  2. SqlServer Split函数

    Create FUNCTION [dbo].[SplitToTable] ( @SplitString nvarchar(max), @Separator nvarchar(10)=' ' ) RET ...

  3. log4j.properties配置文件

    #配置根 Logger,其语法为:log4j.rootLogger = level,appenderName1,appenderName2,... #优先级:ALL < DEBUG < I ...

  4. offsetParent、offsetTop、offsetLeft、offsetWidth、offsetHeight

    w3c规范,请戳这里:http://www.w3.org/TR/cssom-view/#dom-htmlelement-offsetparent 一.offsetParent 英文解读: part o ...

  5. RAID磁盘阵列学习笔记

    RAID是“Redundant Array of Independent Disk”的缩写,中文意思是独立冗余磁盘阵列.简单地解释,就是将N台硬盘通过RAID Controller(分Hardware ...

  6. 《EnterLib PIAB深入剖析》系列博文汇总_转

    转: http://www.cnblogs.com/artech/archive/2008/08/08/1263418.html

  7. BZOJ 1071组队

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1071 题目很好,居然写了很久,题解找了真多: 主要两种做法: O(n^2lgn),通过优先 ...

  8. js中的null VS undefined

    var a;------>undefined. JS变量的默认值.注意点在于判断变量的值为null.这是错误的.比如 if( a === null ) { // TODO; }; 实际上是und ...

  9. JavaScript事件委托的技术原理

    如今的JavaScript技术界里最火热的一项技术应该是‘事件委托(event delegation)’了.使用事件委托技术能让你避免对特定的每个节点添加事件监听器:相反,事件监听器是被添加到它们的父 ...

  10. css系列-间隔与间距实例

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...