第八周项目二:Time类中的运算符重载

本文介绍了一个名为Time的类,该类实现了多种运算符的重载,包括比较运算符和算术运算符等,通过这些运算符可以方便地进行时间的比较和计算。

问题及代码:

/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称: Time类中的运算符重载 .cpp
*作    者:白云飞
*完成日期:2015年4月25日
*版 本 号:v1.0
*
*问题描述:实现Time类中的运算符重载。
*程序输入:无
*程序输出:时间
*/
#include<iostream>
using namespace std;
class CTime
{
private:
    unsigned short int hour;    // 时
    unsigned short int minute;  // 分
    unsigned 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秒前的时间
    //二目赋值运算符的重载
    CTime operator+=(CTime &c);
    CTime operator-=(CTime &c);
    CTime operator+=(int s);//返回s秒后的时间
    CTime operator-=(int s);//返回s秒前的时间
};
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)
{
    return !(*this<t);
}
bool CTime::operator <= (CTime &t)
{
    return !(*this>t);
}
bool CTime::operator == (CTime &t)
{
    if (*this < t || *this > t) return false;
    return true;

}
bool CTime::operator != (CTime &t)
{
    if (*this == t) return false;
    return true;

}
CTime CTime::operator+(CTime &t)
{
  int h,m,s;
    s=second+t.second;
    m=minute+t.minute;
    h=hour+t.hour;
    if (s>59)
    {
        s-=60;
        m++;
    }
    if (m>59)
    {
        m-=60;
        h++;
    }
    if (h>23) h-=24;
    CTime t0(h,m,s);
    return t0;
}
CTime CTime::operator-(CTime &t)//对照+理解
{
    int s2,m2,h2;
    s2=second-t.second;
    m2=minute-t.minute;
    h2=hour-t.hour;
    if(s2<0)
    {
        s2+=60;
        m2--;
    }
    if(m2<0)
    {
        m2+=60;
        h2--;
    }
    if(h2<0)
    {
        h2+=24;
    }
    CTime t0(h2,m2,s2);
    return t0;
}
CTime CTime::operator+(int s)//返回s秒后的时间
{
    int s1,m1,h1;
    s1=s%60;
    m1=(s/60)%60;
    h1=s/3600;
    CTime t1(s1,m1,h1);
    return *this+t1;
}
CTime CTime::operator-(int s)//返回s秒前的时间
{
    int s2,m2,h2;
    s2=s%60;
    m2=(s/60)%60;
    h2=s/3600;
    CTime t2(h2,m2,s2);
    return *this-t2;
}
//二目赋值运算符的重载
CTime CTime::operator+=(CTime &c)
{
    *this=*this+c;
    return *this;
}
CTime CTime::operator-=(CTime &c)
{
    *this=*this-c;
    return *this;
}
CTime CTime::operator+=(int s)//返回s秒后的时间
{
    *this=*this+s;
    return *this;
}
CTime CTime::operator-=(int s)//返回s秒前的时间
{
    *this=*this-s;
    return *this;
}
int main()
{
    CTime t1(8,20,25),t2(11,20,50),t;
    cout<<"t1:";
    t1.display();
    cout<<"t2:";
    t2.display();
    if(t1>t2) cout<<"t1>t2"<<endl;
    if(t1<t2) cout<<"t1<t2"<<endl;
    if(t1>=t2) cout<<"t1>=t2"<<endl;
    if(t1<=t2) cout<<"t1<=t2"<<endl;
    if(t1==t2) cout<<"t1=t2"<<endl;
    if(t1!=t2) cout<<"t1!=t2"<<endl;
    t=t1+t2;
    t.display();
    t=t1-t2;
    t.display();
    t=t1+100;
    t.display();
    t=t1-100;
    t.display();
    t1+=t2;
    t1.display();
    t1-=t2;
    t1.display();
    t1+=100;
    t1.display();
    t1-=100;
    t1.display();
    return 0;
}


运行结果:


学习心得:

代码中运算符重载函数有bool型,成员函数,参数有int型,还有类的成员。


基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究”展开,提出了一种结合数据驱动方法与Koopman算子理论的递归神经网络(RNN)模型线性化方法,旨在提升纳米定位系统的预测控制精度与动态响应能力。研究通过构建数据驱动的线性化模型,克服了传统非线性系统建模复杂、计算开销大的问题,并在Matlab平台上实现了完整的算法仿真与验证,展示了该方法在高精度定位控制中的有效性与实用性。; 适合人群:具备一定自动化、控制理论或机器学习背景的科研人员与工程技术人员,尤其是从事精密定位、智能控制、非线性系统建模与预测控制相关领域的研究生与研究人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能预测控制;②为复杂非线性系统的数据驱动建模与线性化提供新思路;③结合深度学习与经典控制理论,推动智能控制算法的实际落地。; 阅读建议:建议读者结合Matlab代码实现部分,深入理解Koopman算子与RNN结合的建模范式,重点关注数据预处理、模型训练与控制系统集成等关键环节,并可通过替换实际系统数据进行迁移验证,以掌握该方法的核心思想与工程应用技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值