第8周实验报告2

实验目的:实现Time类的运算符重载

* 程序头部注释开始
* 程序的版权和版本声明部分
* 烟台大学计算机学院学生
* 文件名称:         运算符重载
* 作    者:          胡斌                
* 完成日期:  2012 年 4月10 日
* 版本号:     v1.0    

* 程序头部的注释结束(此处也删除了斜杠)

#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);  
    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);  
  
    //二目运算符的重载   
    CTime operator+(CTime &c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15   
    CTime operator-(CTime &c);//对照+理解   
    CTime operator+(int s);//返回s秒后的时间   
    CTime operator-(int s);//返回s秒前的时间   
  
    //一目运算符的重载   
    CTime operator++(int);//后置++,下一秒   
    CTime operator++();//前置++,下一秒   
    CTime operator--(int);//后置--,前一秒   
    CTime operator--();//前置--,前一秒   
  
    //赋值运算符的重载        
    CTime operator+=(CTime &c);  
    CTime operator-=(CTime &c);  
    CTime operator+=(int s);//返回s秒后的时间   
    CTime operator-=(int s);//返回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 * 3600 + minute * 60 + second > t.hour * 3600 + t.minute * 60 + second)  
    {  
        return true;  
    }  
  
    else  
    {  
        return false;  
    }  
}  
  
//对 “<”的定义   
bool CTime::operator < (CTime &t)//将时分化成秒,再比较大小   
{  
    if(hour * 3600 + minute * 60 + second < t.hour * 3600 + t.minute * 60 + second)  
    {  
        return true;  
    }  
  
    else  
    {  
        return false;  
    }  
}  
  
//对 “<=”的定义   
bool CTime::operator <= (CTime &t)//将时分化成秒,再比较大小   
{  
    if(hour * 3600 + minute * 60 + second <= t.hour * 3600 + t.minute * 60 + second)  
    {  
        return true;  
    }  
  
    else  
    {  
        return false;  
    }  
}  
  
//对 “>=”的定义   
bool CTime::operator >= (CTime &t)//将时分化成秒,再比较大小   
{  
    if(hour * 3600 + minute * 60 + second >= t.hour * 3600 + t.minute * 60 + second)  
    {  
        return true;  
    }  
  
    else  
    {  
        return false;  
    }  
}  
  
//对 “==”的定义   
bool CTime::operator == (CTime &t)//时分秒都要相等   
{  
    if(hour == t.hour && minute == t.minute && second == t.second)  
    {  
        return true;  
    }  
  
    else  
    {  
        return false;  
    }  
}  
  
//对 “!=”的定义   
bool CTime::operator != (CTime &t)  
{  
    if(hour != t.hour || minute != t.minute || second != t.second)  
    {  
        return true;  
    }  
  
    else  
    {  
        return false;  
    }  
}  
  
//二目运算符的重载   
//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15   
CTime CTime::operator+(CTime &c)//先转换成秒,再相加,然后输出   
{  
    int T;  
  
    T = hour * 3600 + minute *60 + second + c.hour * 3600 + c.minute * 60 + c.second;  
  
    hour = T / 3600;  
    T = T % 3600;  
    minute = T / 60;  
    second = T % 60;  
  
    return (*this);  
}  
  
//返回c所规定的时、分、秒后的时间   
CTime CTime::operator-(CTime &c)//先转换成秒,再相减,然后输出   
{  
    int T;  
  
    T = hour * 3600 + minute *60 + second - c.hour * 3600 - c.minute * 60 - c.second;  
    hour = T / 3600;  
    T = T % 3600;  
    minute = T / 60;  
    second = T % 60;  
  
    return (*this);  
}  
//返回s秒后的时间   
CTime CTime::operator+(int s)  
{  
    second = second + s;  
  
    if(second >= 60)  
    {  
        minute = minute + second /60;  
        second = second % 60;  
  
        if(minute >= 60)  
        {  
            hour = hour + minute / 60;  
            minute = minute % 60;  
  
            if(hour >= 24)  
            {  
                hour = hour % 24;  
            }  
  
        }  
  
    }  
  
    return (*this);  
}  
//返回s秒前的时间   
CTime  CTime::operator-(int s)  
{  
    int T;  
  
    T = hour * 3600 + minute *60 + second - s;  
    hour = T / 3600;  
    T = T % 3600;  
    minute = T / 60;  
    second = T % 60;  
  
    return (*this);  
}  
  
  
//一目运算符的重载   
//后置++,下一秒   
CTime  CTime::operator++(int)  
{  
    CTime temp = (*this);  
  
    int T;  
  
    T = hour * 3600 + minute *60 + second + 1;  
    hour = T / 3600;  
    T = T % 3600;  
    minute = T / 60;  
    second = T % 60;  
  
    return temp;  
}  
  
  
//前置++,下一秒   
CTime CTime::operator++()  
{  
    int T;  
  
    T = hour * 3600 + minute *60 + second + 1;  
    hour = T / 3600;  
    T = T % 3600;  
    minute = T / 60;  
    second = T % 60;  
  
    return (*this);  
}  
//后置--,下一秒   
CTime  CTime::operator--(int)  
{  
    CTime temp = (*this);  
  
    int T;  
  
    T = hour * 3600 + minute *60 + second - 1;  
    hour = T / 3600;  
    T = T % 3600;  
    minute = T / 60;  
    second = T % 60;  
  
    return temp;  
}  
  
  
//前置--,下一秒   
CTime CTime::operator--()  
{  
    int T;  
  
    T = hour * 3600 + minute *60 + second - 1;  
    hour = T / 3600;  
    T = T % 3600;  
    minute = T / 60;  
    second = T % 60;  
  
    return (*this);  
}  
  
CTime CTime::operator+=(CTime &c)  
{  
    CTime c1;  
  
    c1 = (*this) + c;  
  
    return c1;  
}  
CTime CTime::operator-=(CTime &c)  
{  
    CTime c1;  
  
    c1 = (*this) - c;  
  
    return c1;  
}  
CTime CTime::operator+=(int s)  
{  
    CTime c1;  
  
    c1 = (*this) + s;  
  
    return c1;  
}  
CTime CTime::operator-=(int s)  
{  
    CTime c1;  
  
    c1 = (*this) - s;  
  
    return c1;  
}  
  
void main()    
{    
    CTime t1(8,20,25), t2(11,20,50), t;    
    
    cout << "t1为:";    
    
    t1.display();    
    
    cout << "t2为:";    
    
    t2.display();    
    
    cout << "比较两个时间大小:" << 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;    
    
    if (t1 <= t2) cout << "t1 ≤ t2" << endl;    
    
    cout<<endl;  
  
    t = t1;  
    
    t = t + t2;    
    
    cout << "t1 + t2 = ";    
    
    t.display();  
  
    t = t1;  
  
    t = t - t2;    
    
    cout << "t1 - t2 = ";    
    
    t.display();  
  
    t = t1;  
  
    t = t + 35;  
  
    cout<< "t1 + 35 = ";  
      
    t .display();  
  
    t = t2;  
  
    t = t - 55;  
  
    cout<< "t2 - 55 = " ;  
  
    t.display();  
  
    t = t1;  
  
    t = t ++;  
  
    cout << endl;  
  
    cout << "t1++ = ";  
  
    t.display();  
  
    t = t1;  
  
    t = ++t;  
  
    cout << "++t1 = ";  
  
    t.display();  
  
    t = t2;  
  
    t = t --;  
  
    cout << "t2-- = ";  
  
    t.display();  
  
    t = t2;  
  
    t = --t;  
  
    cout << "--t1 = ";  
  
    t.display();  
  
    t = t1;  
  
    t +=t2;  
  
    cout << "t1 += t2,t1 = " ;  
  
    t.display();  
  
    t = t2;  
  
    t -= t1;  
  
    cout << "t2 -= t1,t2 = " ;  
  
    t.display();  
  
    t = t1;  
  
    t += 5;  
  
    cout << "t1 += 5 = " ;  
  
    t.display();  
  
    t = t1;  
  
    t -= 6;  
   
    cout << "t1 -= 6 = " ;  
  
    t.display();  
  
    system("pause");    
}  

截图;


 

 

 

代码太长了,虽然不难,但打的很辛苦!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值