/************************************************
* *
* 自定义时间(不含日期)类型 *
* *
************************************************/
#pragma once
class CMyTime
{
public:
CMyTime() ; //初始化时间
CMyTime(int hour) ; //初始化时间
CMyTime(int hour,int minute) ; //初始化时间
CMyTime(int hour,int minute,int second) ; //初始化时间
~CMyTime() ;
public:
void Clear() ; //清零
void SetTime(int hour,int minute,int second) ;//设置时间
void SetCarry(int carry) ; //设置进位
CMyTime AddSecond(int second) ; //添加秒数
CMyTime operator =(CMyTime &t) ; //时间赋值
CMyTime operator +=(CMyTime &t) ; //时间相加
CMyTime operator -=(CMyTime &t) ; //时间相减
CMyTime operator +(CMyTime &t) ; //两个时间值相加
CMyTime operator -(CMyTime &t) ; //两个时间值相加
CMyTime operator *(int n) ; //时间乘于倍数
CMyTime operator ++() ; //自增量秒
CMyTime operator --() ; //自减量秒
bool operator ==(CMyTime &t) ; //==判断
bool operator >(CMyTime &t) ; //>判断
bool operator >=(CMyTime &t) ; //>=判断
bool operator <(CMyTime &t) ; //<判断
bool operator <=(CMyTime &t) ; //<=判断
bool operator !=(CMyTime &t) ; //!=判断
private:
CMyTime ConvertToTime(int second) ; //秒转换成Time
protected :
int hour() ; //时0-23
int minute() ; //分0-59
int second() ; //秒0-59
int carry() ; //进位/借位
private:
int _hour ; //时
int _minute ; //分
int _second ; //秒
int _carry ; //进位,如果时间相加大于24个小时,则进位为天数;小于则借位
};
/////////////////////////
#include "StdAfx.h"
#include "MyTime.h"
#include
CMyTime::CMyTime()
{
SetTime(0,0,0) ;
}
CMyTime::CMyTime(int hour)
{
SetTime(hour,0,0) ;
}
CMyTime::CMyTime(int hour,int minute)
{
SetTime(hour,minute,0) ;
}
CMyTime::CMyTime(int hour,int minute,int second)
{
SetTime(hour,minute,second) ;
}
CMyTime::~CMyTime()
{
}
void CMyTime::SetTime(int hour,int minute,int second)
{
_carry = 0 ;//进/借位清零
_hour = hour<0?0:(hour>23?23:hour) ;
_minute = minute<0?0:(minute>59?59:minute) ;
_second = second<0?0:(second>59?59:second) ;
}
int CMyTime::hour()
{
return _hour ;
}
int CMyTime::minute()
{
return _minute ;
}
int CMyTime::second()
{
return _second ;
}
int CMyTime::carry()
{
return _carry ;
}
CMyTime CMyTime::operator =(CMyTime &t)
{
return *this ;
}
CMyTime CMyTime::operator +=(CMyTime &t)
{
int h = t.hour() ;
int m = t.minute() ;
int s = t.second() ;
int c = t._carry ;
int cs = (_second + s) / 60 ;
_second = (_second + s) % 60 ;
int cm = (_minute + m + cs) / 60 ;
_minute = (_minute + m + cs) % 60 ;
int ch = (_hour + h + cm) / 24 ;
_hour = (_hour + h + cm) % 24 ;
_carry += c + ch ;
return *this ;
}
CMyTime CMyTime::operator -=(CMyTime &t)
{
int h = t.hour() ;
int m = t.minute() ;
int s = t.second() ;
int c = t._carry ;
//秒
int cs = _second-s>0?0:1 ;
if(cs==1)_minute-- ; //借位i
_second = (_second + 60*cs) - s ;
//分
int cm = _minute-m>0?0:1 ;
if(cm==1)_hour-- ; //借位
_minute = (_minute + 60*cm) - m ;
//时
int ch = _hour-h>0?0:1 ;
_hour = (_hour + 24*ch) - h ;
_carry -= ch ;
_carry -= c ;
return *this ;
}
CMyTime CMyTime::operator +(CMyTime &t)
{
CMyTime r = *this ;
return r += t ;
}
CMyTime CMyTime::operator -(CMyTime &t)
{
CMyTime r = *this ;
return r -= t ;
}
CMyTime CMyTime::operator *(int n)
{
n = n<0?0:n;
_second = _second * n ;
int cs = _second / 60 ;
_second = _second % 60 ;
_minute = _minute * n + cs ;
int cm = _minute / 60 ;
_minute = _minute % 60 ;
_hour = _hour * n + cm ;
int ch = _hour / 24 ;
_hour = _hour % 24 ;
_carry = _carry<0?(_carry-ch):(_carry+ch);
return *this ;
}
CMyTime CMyTime::AddSecond(int second) //添加秒数
{
CMyTime tmp = ConvertToTime(abs(second)) ;
if(second<0)
{
(*this)-=tmp ;
}else{
(*this)+=tmp ;
}
return *this ;
}
CMyTime CMyTime::operator ++() //自增量秒
{
return AddSecond(1) ;
}
CMyTime CMyTime::operator --() //自减量秒
{
return AddSecond(-1) ;
}
CMyTime CMyTime::ConvertToTime(int second) //秒转换成Time
{
CMyTime t(0,0,0) ;
int h = 0 ;
int m = 0 ;
int s = 0 ;
int c = 0 ;
s = second % 60 ;
int cs = second / 60 ;
m = cs % 60 ;
int cm = m / 24 ;
h = cm % 24 ;
c = cm / 24 ;
t.SetCarry(c) ;
return t ;
}
void CMyTime::Clear()
{
_hour = 0 ;
_minute = 0 ;
_second = 0 ;
_carry = 0 ;
}
void CMyTime::SetCarry(int carry)
{
_carry = carry ;
}
bool CMyTime::operator ==(CMyTime &t) //==判断
{
return _carry == t.carry() && _hour==t.hour() && _minute==t.minute() && _second==t.second() ;
}
bool CMyTime::operator >(CMyTime &t) //>判断
{
bool bGrater = false ;
if(_carry>t.carry()){
bGrater = true ;
}else if(_carry==t.carry()){
if(_hour>t.hour()){
bGrater = true ;
}else if(_hour==t.hour()){
if(_minute>t.minute()){
bGrater = true ;
}else if(_minute==t.minute()){
if(_second>t.second()){
bGrater = true ;
}
}
}
}
return bGrater ;
}
bool CMyTime::operator >=(CMyTime &t) //>=判断
{
bool bGraterOrEqu = false ;
if(_carry>t.carry()){
bGraterOrEqu = true ;
}else if(_carry==t.carry()){
if(_hour>t.hour()){
bGraterOrEqu = true ;
}else if(_hour==t.hour()){
if(_minute>t.minute()){
bGraterOrEqu = true ;
}else if(_minute==t.minute()){
if(_second>=t.second()){
bGraterOrEqu = true ;
}
}
}
}
return bGraterOrEqu ;
}
bool CMyTime::operator <(CMyTime &t) //<判断
{
bool bLess = false ;
if(_carry bLess = true ;
}else if(_carry==t.carry()){
if(_hour bLess = true ;
}else if(_hour==t.hour()){
if(_minute bLess = true ;
}else if(_minute==t.minute()){
if(_second bLess = true ;
}
}
}
}
return bLess ;
}
bool CMyTime::operator <=(CMyTime &t) //<=判断
{
bool bLessOrEqu = false ;
if(_carry bLessOrEqu = true ;
}else if(_carry==t.carry()){
if(_hour bLessOrEqu = true ;
}else if(_hour==t.hour()){
if(_minute bLessOrEqu = true ;
}else if(_minute==t.minute()){
if(_second<=t.second()){
bLessOrEqu = true ;
}
}
}
}
return bLessOrEqu ;
}
bool CMyTime::operator !=(CMyTime &t) //!=判断
{
return _carry!=t.carry() || _hour!=t.hour() || _minute!=t.minute() || _second!=t.second() ;
}
阅读(1163) | 评论(0) | 转发(0) |