第八周(运算符重载)Time类

/*

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

*All rights reserved。

*文件名称:第八周(运算符重载)

*作者:王忠

*完成日期:2015.4.28

*版本号: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)
    {
        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秒前的时间
};
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;
}
int main()
{
    CTime t1(8,20,25),t2(11,20,50),t;
    cout<<"t1为:";
    t1.display();
    cout<<"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;
}

太弱了,测试函数写不出来,测试不全。在CTime CTime::operator+(CTime &t)这里时总是输出一个随机数,怎么看怎么写的对,就是不输出正确答案,最后发现没返回一个对象

时间: 2024-08-03 02:45:50

第八周(运算符重载)Time类的相关文章

第八周-运算符重载-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 * *问题描述:实现Time类中的运算符重载.定义对时间对象的自增和自减一目运算符 //一目运算符的重载 CTime operator++(int);//后置++,下一秒 CTime operator++();//前置++,下一秒,前置与后置返回值不一样 CTime operator-

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

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

第八周项目3-分数类中的运算符重载

实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简).比较(6种关系)的运算.可以在第4周分数类代码的基础上开始工作. /* * Copyright (c) 2015,烟台大学计算机学院 * All right reserved. * 作者:邵帅 * 文件:Demo.cpp * 完成时间:2015年04月29日 * 版本号:v1.0 */ #include <iostream> using namespace std; class CFraction { private:

第八周项目三-分数类中的运算符重载

实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简).比较(6种关系)的运算.可以在第4周分数类代码的基础上开始工作. /* * Copyright (c) 2015,烟台大学计算机学院 * All right reserved. * 作者:赵嵩 * 文件:Demo.cpp * 完成时间:2015年05月16日 * 版本号:v1.0 */ #include <iostream> using namespace std; class CFraction { private:

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

实现Time类中的运算符重载 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 >

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

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

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

这是一篇晦涩难懂的片面的研究 一,简单的继承层次 class CA { } class CB : CA{ } class CC : CB{ } } void Test(CA oa){//CATest Debug.Log ("CA==============="); } void Test(CB oa){//CBTest Debug.Log ("CB==============="); } void Test(CC oa){//CCTest Debug.Log (&q

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

问题: (1)请用类的成员函数,定义复数类重载运算符+.-.*./,使之能用于复数的加减乘除 class Complex { public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r; imag=i;} Complex operator+(const Complex &c2); Complex operator-(const Complex &c2); Complex operator*(const Comple

第八周 项目一-复数类中的运算符重载(3)完整产品

定义一个定义完整的类(是可以当作独立的产品发布,成为众多项目中的"基础工程").这样的类在(2)的基础上,扩展+.-.*./运算符的功能,使之能与double型数据进行运算.设Complex c; double d; c+d和d+c的结果为"将d视为实部为d的复数同c相加",其他-.*./运算符类似 /* * Copyright (c) 2015, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:冷基栋