您的位置:首页 > 其它

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

2015-04-26 12:38 441 查看
/*
*Copyright (c) 2014, 烟台大学计算机学院
*All rights reserved.
*文件名称:week8-2.cpp
*作者:高赞
*完成日期:2015年 4 月 26 日
*版本号:v1.0
*
* 问题描述:实现Time类中的运算符重载。
*/
#include <iostream>
#include "CTime.h"
using namespace std;
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;
t=t1+t2;
cout<<"t1+t2=";
t.display();
t=t1-t2;
cout<<"t1-t2=";
t.display();
t=t1+2000;
cout<<"t1+2000s=";
t.display();
t=t1-5000;
cout<<"t1-5000s=";
t.display();
t1+=t2;
cout<<"t1+=t2,t1=";
t1.display();
t1-=t2;
cout<<"t1-=t2,t1=";
t1.display();
t1+=2000;
cout<<"t1+=2000s,t1=";
t1.display();
t1-=7000;
cout<<"t1-=7000s,t1=";
t1.display();
return 0;
}


CTime类

#ifndef CTIME_H_INCLUDED
#define CTIME_H_INCLUDED

class CTime
{
private:
int hour;    // 时
int minute;  // 分
int sec;  // 秒
public:
CTime(int h=0,int m=0,int s=0):hour(h),minute(m),sec(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);
//二目的加减运算符的重载
//返回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秒前的时间
};

#endif // CTIME_H_INCLUDED


定义成员函数

#include <iostream>
#include "CTime.h"
using namespace std;
void CTime::setTime(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
bool CTime::operator > (CTime &t)
{
bool b;
if(hour>t.hour)
b=true;
else if(hour<t.hour)
b=false;
else
{
if(minute>t.minute)
b=true;
else if(minute<t.minute)
b=false;
else
{
if(sec>t.sec)
b=true;
else if(sec<=t.sec)
b=false;
}
}
return b;
}
bool CTime::operator < (CTime &t)
{
bool b;
if(hour<t.hour)
b=true;
else if(hour>t.hour)
b=false;
else
{
if(minute<t.minute)
b=true;
else if(minute>t.minute)
b=false;
else
{
if(sec<t.sec)
b=true;
else if(sec>=t.sec)
b=false;
}
}
return b;
}
bool CTime::operator >= (CTime &t)
{
if(*this<t)
return false;
else return true;
}
bool CTime::operator <= (CTime &t)
{
if(*this>t)
return false;
else return true;
}
bool CTime::operator == (CTime &t)
{
if(*this>t||*this<t)
return false;
else return true;
}
bool CTime::operator != (CTime &t)
{
if(*this>t||*this<t)
return true;
else return false;
}
CTime CTime::operator+(CTime &t)
{
int h=hour+t.hour,m=minute+t.minute,s=sec+t.sec;
if(s>=60)
{
s-=60;
++m;
}
if(m>=60)
{
m-=60;
++h;
}
if(h>=24)
h-=24;
return CTime(h,m,s);
}
CTime CTime::operator-(CTime &t)
{
int h=hour-t.hour,m=minute-t.minute,s=sec-t.sec;
if(s<0)
{
s+=60;
--m;
}
if(m<0)
{
m+=60;
--h;
}
if(h<0)
h+=24;
return CTime(h,m,s);
}
CTime CTime::operator+(int S)
{
int h,m,s;
s=S%60;
S=S/60;
m=S%60;
h=S/60;
CTime t2(h,m,s);
return (*this+t2);
}
CTime CTime::operator-(int S)
{
int h,m,s;
s=S%60;
S=S/60;
m=S%60;
h=S/60;
CTime t2(h,m,s);
return (*this-t2);
}
CTime CTime::operator+=(CTime &t)
{
hour+=t.hour,minute+=t.minute,sec+=t.sec;
if(sec>=60)
{
sec-=60;
++minute;
}
if(minute>=60)
{
minute-=60;
++hour;
}
if(hour>=24)
hour-=24;
return *this;
}
CTime CTime::operator-=(CTime &t)
{
hour-=t.hour,minute-=t.minute,sec-=t.sec;
if(sec<0)
{
sec+=60;
--minute;
}
if(minute<0)
{
minute+=60;
--hour;
}
if(hour<0)
hour+=24;
return *this;
}
CTime CTime::operator+=(int S)
{
int h,m,s;
s=S%60;
S=S/60;
m=S%60;
h=S/60;
CTime t2(h,m,s);
return (*this+=t2);
}
CTime CTime::operator-=(int S)
{
int h,m,s;
s=S%60;
S=S/60;
m=S%60;
h=S/60;
CTime t2(h,m,s);
return (*this-=t2);
}




unsigned short int 是无符号短整型 范围0~65535,

鉴于定义时间差的成员函数时需要用到负数,所以还是把unsigned short int改成int了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: