객체 선행++, 선행 --후면++, 후면 -- (및 연산자를 다시 로드하지 않음)
/*
*All right reserved.
* *
*All right reserved.
* : ++, --; ++, --
* :
* :2014 4 11
* :v1.0
* : : ++, --; ++, --
* :*/
#include <iostream>
using namespace std;
class CTime
{
private:
int hour; //
int minute; //
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 > (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 :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--();// --
};
CTime::CTime(int h,int m,int s)
{
hour=h;
minute=m;
second=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>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(hour==t.hour&&minute==t.minute&&second==t.second)
return true;
else
return false;
}
bool CTime::operator != (CTime &t)
{
if(*this==t)
return false;
return true;
}
CTime CTime::operator+(CTime &t)
{
int h,m,s;
s=second+t.second;
m=minute+t.minute;
h=hour+t.hour;
if (s>59)
{
s-=60;
m++;
}
if (m>59)
{
m-=60;
h++;
}
if (h>23) h-=24;
CTime t3(h,m,s);
return t3;
}
CTime CTime::operator-(CTime &t)
{
int h,m,s;
s=second-t.second;
m=minute-t.minute;
h=hour-t.hour;
if (s<0)
{
s+=60;
m--;
}
if (m<0)
{
m+=60;
h--;
}
if (h<0) h+=24;
CTime t3(h,m,s);
return t3;
}
CTime CTime::operator+(int s)
{
int s1=s%60;
int m1=s/60;
int h1=s/3600;
CTime t3(h1,m1,s1);
return *this+t3;
}
CTime CTime::operator-(int s)
{
int s1,m1,h1;
s1=s%60;
m1=s/60;
h1=s/3600;
CTime t3(h1,m1,s1);
return *this-t3;
}
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 temp(*this);
second++;
if(second>59)
{
second-=60;
minute++;
}
if(minute>59)
{
minute-=60;
hour++;
}
if(hour>23)
{
hour-=24;
}
return temp;
}
CTime CTime::operator++()
{
++second;
if(second>59)
{
second-=60;
minute++;
}
if(minute>59)
{
minute-=60;
hour++;
}
if(hour>23)
{
hour-=24;
}
return *this;
}
CTime CTime::operator--(int)
{
CTime temp(*this);
second--;
if (second<0)
{
second+=60;
minute--;
}
if (minute<0)
{
minute+=60;
hour--;
}
if (hour<0) hour+=24;
return temp;
}
CTime CTime::operator--()
{
--second;
if (second<0)
{
second+=60;
minute--;
}
if (minute<0)
{
minute+=60;
hour--;
}
if (hour<0) hour+=24;
return *this;
}
int main()
{
CTime t1(10,20,25),t2(6,20,45),t;
cout<<"t1 :";
t1.display();
cout<<"t2 :";
t2.display();
cout<<" :
";
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;
cout<<" ;"<<endl;
t.display();
t=t1-t2;
cout<<" ;"<<endl;
t.display();
cout<<"+600 ;"<<endl;
t=t1+600;
t.display();
cout<<"-400 ;"<<endl;
t=t1-400;
t.display();
cout<<" "<<endl;
t1+=t2;
t1.display();
cout<<" "<<endl;
t1-=t2;
t1.display();
cout<<" 600 "<<endl;
t1+=600;
t1.display();
cout<<" 600 "<<endl;
t1-=600;
t1.display();
cout<<" ++ :"<<endl;
++t1;
t1.display();
cout<<" -- :"<<endl;
--t1;
t1.display();
cout<<" ++ :"<<endl;
t1++;
t1.display();
cout<<" -- :"<<endl;
t1--;
t1.display();
return 0;
}
실행 결과:
체험
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.