首页 > 其他分享 >第9周项目2-Time类中的运算符重载(续)

第9周项目2-Time类中的运算符重载(续)

时间:2023-08-14 11:07:37浏览次数:45  
标签:return CTime hour t0 运算符 second minute Time 类中


问题描述:

在Time类中的运算符重载基础上
(1)定义对时间对象的自增和自减一目运算符
(2)定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
代码:

#include <iostream>
using namespace std;
class CTime
{
private:
    short int hour;    // 时
    short int minute;  // 分
    short int second;  // 秒
public:
    CTime(int h=0,int m=0,int s=0);
    void setTime(int h,int m,int s);
    void display();
    //二目的比较运算符重载
    bool operator > (CTime &t);
    bool operator < (CTime &t);
    bool operator >= (CTime &t);
    bool operator <= (CTime &t);
    bool operator == (CTime &t);
    bool operator != (CTime &t);
    //二目的加减运算符的重载
    //返回t规定的时、分、秒后的时间
    //例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15
    CTime operator+(CTime &t);
    CTime operator-(CTime &t);//对照+理解
    CTime operator+(int s);//返回s秒后的时间
    CTime operator-(int s);//返回s秒前的时间
    //二目赋值运算符的重载
    void operator+=(CTime &c);
    CTime operator-=(CTime &c);
    CTime operator+=(int s);//返回s秒后的时间
    CTime operator-=(int s);//返回s秒前的时间
    CTime operator++(int);
    CTime operator++();
    CTime operator--(int);
    CTime operator--();
    friend istream& operator>>(istream& input,CTime& t);
    friend ostream& operator<<(ostream& output,CTime& t);
};
istream& operator>>(istream& input,CTime& t)
{
    char a;
    cin>>t.hour>>a>>t.minute>>a>>t.second;
    return input;
}
ostream& operator<<(ostream& output,CTime& t)
{
    cout<<t.hour<<":"<<t.minute<<":"<<t.second;
    return output;
}
CTime CTime::operator++(int)
{
    CTime t(*this);
    *this+=1;
    return t;
}
CTime CTime::operator++()
{
    *this+=1;
    return *this;
}
CTime CTime::operator--(int)
{
    CTime t(*this);
    *this-=1;
    return t;
}
CTime CTime::operator--()
{
    return *this-1;
}
CTime::CTime(int h,int m,int s):hour(h),minute(m),second(s){}
void CTime::setTime(int h,int m,int s)
{
    hour=h;minute=m;second=s;
}
void CTime::display()
{
    cout<<hour<<":"<<minute<<":"<<second<<endl;
}
bool CTime::operator>(CTime &t)
{
    if(hour>t.hour) return true;
    if(hour<t.hour) return false;
    if(minute>t.minute) return true;
    if(minute<t.minute) return false;
    if(second>t.second) return true;
    return false;
}
bool CTime::operator<(CTime &t)
{
    if(hour<t.hour) return true;
    if(hour>t.hour) return false;
    if(minute<t.minute) return true;
    if(minute>t.minute) return false;
    if(second<t.second) return true;
    return false;
}
bool CTime::operator==(CTime &t)
{
    if((*this)>t||(*this)<t) return false;
    else return true;
}
bool CTime::operator>=(CTime &t)
{
    return !((*this)<t);
}
bool CTime::operator<=(CTime &t)
{
    return !((*this)>t);
}
bool CTime::operator!=(CTime &t)
{
    return !((*this)==t);
}
CTime CTime::operator+(CTime &t)
{
    CTime t0;
    t0.hour=hour+t.hour;
    t0.minute=minute+t.minute;
    t0.second=second+t.second;
    if(t0.second>=60)
    {
        t0.second-=60;
        t0.minute++;
    }
    if(t0.minute>=60)
    {
        t0.minute-=60;
        t0.hour++;
    }
    if(t0.hour>=24)
        t0.hour-=24;
    return t0;
}
CTime CTime::operator-(CTime &t)
{
    CTime t0;
    t0.hour=hour-t.hour;
    t0.minute=minute-t.minute;
    t0.second=second-t.second;
    if(t0.second<0)
    {
        t0.second+=60;
        t0.minute--;
    }
    if(t0.minute<0)
    {
        t0.minute+=60;
        t0.hour--;
    }
    if(t0.hour<0)
        t0.hour+=24;
    return t0;
}
CTime CTime::operator+(int ss)
{
    int h,m,s;
    h=ss/3600;
    ss=ss%3600;
    m=ss/60;
    s=ss%60;
    return CTime(h,m,s)+(*this);
}
CTime CTime::operator-(int ss)
{
    int h,m,s;
    h=ss/3600;
    ss=ss%3600;
    m=ss/60;
    s=ss%60;
    CTime t0(h,m,s);
    return (*this)-t0;
}
void CTime::operator+=(CTime &t)
{
    (*this)=(*this)+t;
}
CTime CTime::operator-=(CTime &t)
{
    (*this)=(*this)-t;
     return *this;
}
CTime CTime::operator+=(int ss)
{
    (*this)=(*this)+ss;
    return (*this);
}
CTime CTime::operator-=(int ss)
{
    (*this)=(*this)-ss;
    return (*this);
}
int main()
{
    CTime t1,t2,t;
    cin>>t1>>t2;
    cout<<"t1="<<t1<<'\t'<<"t2="<<t2<<endl;
    t=t1++;
    cout<<t<<'\t'<<t1<<endl;
    t=++t1;
    cout<<t<<'\t'<<t1;
    return 0;
}

 

运行结果:

第9周项目2-Time类中的运算符重载(续)_运算符重载


标签:return,CTime,hour,t0,运算符,second,minute,Time,类中
From: https://blog.51cto.com/u_16225007/7074020

相关文章

  • 第9周项目1-复数类中的运算符重载(续)
    问题描述:在复数类中的运算符重载基础上(1)再定义一目运算符-,-c相当于0-c。(2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。代码:#include<iostream>usingnamespacestd;classComplex{public:Complex(){real=......
  • AppleScript成功实现FaceTime语音,FaceTime视频,FaceTime数据筛选,检测手机号是否开通
    FaceTime是苹果公司iOS和macOS(以前称MacOSX或OSX)内置的一款视频通话软件,通过Wi-Fi或者蜂窝数据接入互联网,在两个装有FaceTime的设备之间实现视频通话。其要求通话双方均具有装有FaceTime的苹果设备,苹果ID以及可接入互联网的3G/4G/5G或者Wi-Fi网络。 一、Windows电脑上部署苹......
  • 二、运算符
    !>顺便在这里提一句,在编程下你的输入法是要英文的,所有符号也要是英文符号,例如中文符号是。而英文符号是.当然编程就和数学一样,存在有算数运算符。运算符号描述示例+两数相加a+b-两数相减a-b*两数相乘a*b/两数相除a/b%取模-返回除......
  • System.currentTimeMillis()与时区无关
    摘要:System.currentTimeMillis()获取的时间戳与时区无关。综述  System.currentTimeMillis()经常被用来获取当前时间戳,单位是毫秒,可以用来计算当前年月日或者星期几等,可以方便地与Date进行转换,可以计算某个方法的耗时:longcurTime=System.currentTimeMillis();doSth();Sy......
  • System.currentTimeMillis()高并发性能优化
    摘要:System.currentTimeMillis()性能问题的研究、测试与优化。  性能优化使用的测试环境:jdk版本jdk8  操作系统:macOS版本:13.2.1芯片:AppleM1CPU核数:8核  System.currentTimeMillis()是Java极其常用的API,广泛地用来获取时间戳或统计代码执行耗时等,在我们的......
  • php运算符
    加、减、乘、除、负数$a=10;$b=1;echo$a+$b."</br>";echo$a-$b."</br>";echo$a*$b."</br>";echo$a/$b."</br>";echo-$a;输出:1191010-10逻辑运算(与、或、非、异或)var_dump(1and1);var_dump(true&am......
  • 第二章 运算符和数学函数
    第二章运算符和数学函数2.1数学运算符:创建序列(两头都会包含)>x<-2:4>x[1]234+加>1+1[1]2-减>2-1[1]1*乘>1*2[1]2/浮点数除法>3/2[1]1.5%/%整数除法>3%/%2[1]1%%余数>3%%2[1]1^或**求幂>2^2[1]4>2**2[1]4......
  • 报错RuntimeError: Working outside of request context的解决办法
    在程序编写过程中,我写了一个装饰器defauth(func):definner(*args,**kwargs):ifsession.get('username'):res=func(*args,**kwargs)#真正的执行视图函数,在执行视图函数之前判断是否登录returnreselse:#重......
  • onnx_runtime 推理
    onnx_runtime推理importnumpyasnpimportonnximportonnxruntimeasrt#createinputdatainput_data=np.ones((1,3,299,299),dtype=np.float32)#createruntimesessionsess=rt.InferenceSession("inception_v3.onnx")#getoutputnameinput......
  • 创建元组的三种方式、字典中的setdefault和get妙用、类中的重载方法__add__()
    创建元组的三种方式#print(tuple([input(),input()]))#print((input(),input()))t=input(),input()print(t)#可以将列表转换成tuple,也可以直接()创建tuple,或者将多个变量赋值给一个值会自动转换成tuple字典中的setdefault和get妙用setdefault类似get方法w=input()......