第九周(运算符重载时间类)

/*

*copyright(c) 2015,烟台大学计算机学院

*All rights reserved。

*文件名称:第九周(运算符重载时间类)

*作者:王忠

*完成日期:2015.5.13

*版本号:v1.0

*

*问题描述:实现Time类中的运算符重载。定义对时间对象的自增和自减一目运算符

	//一目运算符的重载
	CTime operator++(int);//后置++,下一秒
	CTime operator++();//前置++,下一秒,前置与后置返回值不一样
	CTime operator--( int);//后置--,前一秒
	CTime operator--();//前置--,前一秒

*输入描述:

*程序输出:

#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)
    {
        hour=h;
        minute=m;
        second=s;
    }
    void setTime(int h,int m,int s)
    {
        hour=h;
        minute=m;
        second=s;
    }
    void display()
    {
        cout<<hour<<":"<<minute<<":"<<second<<endl;
    }
    //二目的比较运算符重载
    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 operator++(int);//后置++,下一秒
	CTime operator++();//前置++,下一秒,前置与后置返回值不一样
	CTime operator--( int);//后置--,前一秒
	CTime operator--();//前置--,前一秒
};
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)return false;
    return true;
}
bool CTime::operator <= (CTime &t)
{
    if(*this>t)return false;
    return true;
}
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)
{
    hour=hour+t.hour;
    minute=minute+t.minute;
    second=second+t.second;
    if(second>59)
    {
        minute++;
        second=second%60;
    }
    if(minute>59)
    {
        hour++;
        minute=minute%60;
    }
    if(hour>23)
        hour=hour%24;
    CTime t2(hour,minute,second);
    return t2;
}
CTime CTime::operator-(CTime &t)
{
    hour=hour-t.hour;
    minute=minute-t.minute;
    second=second-t.second;
    if(second<0)
    {
        minute--;
        second=second+60;
    }
    if(minute<0)
    {
        hour--;
        minute=minute+60;
    }
    if(hour<0)
        hour=hour+24;
    CTime t2(hour,minute,second);
    return t2;
}
CTime CTime::operator+(int s)//返回s秒后的时间
{
    second=second+s;
    int ss=s/60;
    if(second>59)
    {
        minute+=ss;
        second=second%60;
    }
    int mm=minute/60;
    if(minute>59)
    {
        hour+=mm;
        minute=minute%60;
    }
    if(hour>23)
        hour=hour%24;
    CTime t2(hour,minute,second);
    return t2;
}
CTime CTime::operator-(int s)//返回s秒前的时间
{
    second=second-s;
    if(second<0)
    {
        minute--;
        second=second+60;
    }
    if(minute<0)
    {
        hour--;
        minute=minute+60;
    }
    if(hour<0)
        hour=hour+24;
    CTime t2(hour,minute,second);
    return 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;
}
CTime CTime::operator++(int)
{
    CTime t=*this;
    *this=*this+1;
    return t;
}
CTime CTime::operator++()
{
    *this=*this+1;
    return *this;
}
CTime CTime::operator--(int)
{
    CTime t=*this;
    *this=*this-1;
    return t;
}
CTime CTime::operator--()
{
    *this=*this-1;
    return *this;
}
int main()
{
    CTime t1(8,20,25),t2(11,20,50),t;
    cout<<"t1为:";
    t1.display();
    cout<<"t2为:";
    t2.display();
    t1++;
    t1.display();
    t2--;
    t2.display();
    cout<<"下面比较两个时间大小:\n";
    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;
    cout<<endl;
    //在测试下面的代码时,请采用单步执行的方法跟踪
    t=t1+t2;
    t.display();
    t=t1-t2;
    t=t1+2000;
    t.display();
    t=t1-5000;
    t1+=t2;
    t1-=t2;
    t1+=2000;
    t1-=5000;
    return 0;
}

时间: 2024-08-01 19:41:23

第九周(运算符重载时间类)的相关文章

第八周-运算符重载-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();

第九周(运算符重载分数类)

/* *copyright(c) 2015,烟台大学计算机学院 *All rights reserved. *文件名称:第九周(运算符重载分数类) *作者:王忠 *完成日期:2015.5.13*版本号:v1.0 * *问题描述:定义分数的一目运算+和-,分别代表分数取正和求反,将"按位取反运算符"~重载为分数的求倒数运算. *输入描述: *程序输出: #include <iostream> #include <Cmath> using namespace std

第十二周(日期时间类)

/* *copyright(c) 2015,烟台大学计算机学院 *All rights reserved. *文件名称:第十二周(日期时间类) *作者:王忠 *完成日期:2015.5.27 *版本号:v1.0 * *问题描述:定义一个日期类Date,数据成员包括年.月.日,SetDate(int y,int m,int d)和PrintDate()函数分别用于设置日期和显示日期:再定义一个时间类Time,数据成员包括时.分.秒,SetTime(int h,int m,int s)和PrintTi

第九周(运算符重载复数类)

/* *copyright(c) 2015,烟台大学计算机学院 *All rights reserved. *文件名称:第九周(运算符重载) *作者:王忠 *完成日期:2015.5.13 *版本号:v1.0 * *问题描述:定义Complex类中的<<和>>运算符的重载,实现输入和输出, *输入描述: *程序输出: #include <iostream> using namespace std; class Complex { public: Complex(){rea

第九周项目1-复数类中的运算符重载(续)

在复数类中的运算符重载基础上 (1)再定义一目运算符 -,-c相当于0-c. (2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然. /* * Copyright (c) 2015,烟台大学计算机学院 * All right reserved. * 作者:邵帅 * 文件:Demo.cpp * 完成时间:2015年05月14日 * 版本号:v1.0 */ #include <iostream> using

第九周项目3-分数类中的运算符重载(续)

在分数类中的运算符重载基础上 (1)定义分数的一目运算+和-,分别代表分数取正和求反,将"按位取反运算符"~重载为分数的求倒数运算. (2)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然. /* * Copyright (c) 2015,烟台大学计算机学院 * All right reserved. * 作者:邵帅 * 文件:Demo.cpp * 完成时间:2015年05月14日 * 版本号:v1.0 *

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

<span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">在Time类中的运算符重载基础上</span><br style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height:

第九周项目一-复数类的中的运算符重载(续)

在复数类中的运算符重载基础上 (1)再定义一目运算符 -,-c相当于0-c. (2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然 /* * Copyright (c) 2015,烟台大学计算机学院 * All right reserved. * 作者:赵嵩 * 文件:Demo.cpp * 完成时间:2015年05月16日 * 版本号:v1.0 */ #include <iostream> using

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

在Time类中的运算符重载基础上 (1)定义对时间对象的自增和自减一目运算符 //一目运算符的重载 CTime operator++(int);//后置++,下一秒 CTime operator++();//前置++,下一秒,前置与后置返回值不一样 CTime operator--( int);//后置--,前一秒 CTime operator--();//前置--,前一秒 (2)定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起