제8 주 미 션 2
#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 :41:15
CTime operator-(CTime &c);// + 6
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::display()
{
cout << hour <<":" << minute << ":" << second << endl;
}
void CTime::setTime(int h,int m,int s)
{
hour = h;
minute = m;
second = s;
}
CTime CTime::operator+(CTime &c)
{
CTime time;
time.hour = hour + c.hour;
time.minute = minute + c.minute;
time.second = second + c.second;
while(time.second >= 60)
{
time.second = time.second - 60;
time.minute ++;
}
while(time.minute >= 60)
{
time.minute = time.minute - 60;
time.hour ++;
}
return time;
}
CTime CTime::operator-(CTime &c)
{
CTime time;
int s1,s2;
s1 = second + minute * 60 + hour * 3600;
s2 = c.second + c.minute * 60 + c.hour * 3600;
if(s1 > s2) //////////////////////////////////////////////////////////////////
time.second = s1 - s2;
else
time.second = s2 - s1;
while(time.second >= 60)
{
time.second = time.second - 60;
time.minute ++;
}
while(time.minute >= 60)
{
time.minute = time.minute - 60;
time.hour ++;
}
return time;
}
CTime CTime::operator+(int s)
{
second = second + s;
while(second >= 60)
{
second = second - 60;
minute ++;
}
while(minute >= 60)
{
minute = minute - 60;
hour ++;
}
return *this;
}
CTime CTime::operator-(int s)
{
while(second < s)
{
minute --;
while(minute < 0)
{
hour--;
minute = minute + 60;
}
second = second + 60;
}
second = second - s;
return *this;
}
CTime CTime::operator++(int)
{
CTime temp(*this);
second++;
if(second >= 60)
{
second = second - 60;
++ minute;
}
return temp;
}
CTime CTime::operator++()
{
if(++ second >= 60)
{
second = second - 60;
++ minute;
}
return *this;
}
CTime CTime::operator--(int)
{
CTime temp(*this);
if(second-- < 0)
{
minute--;
if(--minute < 0)
{
hour--;
minute = minute + 60;
}
second = second + 60;
}
second --;
return temp;
}
CTime CTime::operator--()
{
if(-- second < 0)
{
-- minute;
if(--minute < 0)
{
hour--;
minute = minute + 60;
}
second = second + 60;
}
return *this;
}
CTime CTime::operator+=(CTime &c)
{
hour = hour + c.hour;
minute = minute + c.minute;
second = second + c.second;
while(second >= 60)
{
second = second - 60;
minute ++;
}
while(minute >= 60)
{
minute = minute - 60;
hour ++;
}
return *this;
}
CTime CTime::operator-=(CTime &c)
{
CTime time;
second = second + minute * 60 + hour * 3600;
c.second = c.second + c.minute * 60 + c.hour * 3600;
if(second > c.second )
{
second = second - c.second ;
while(second >= 60)
{
second = second - 60;
minute ++;
}
while(minute >= 60)
{
minute = minute - 60;
hour ++;
}
time = *this;
}
else
{
c.second = c.second - second;
while(c.second >= 60)
{
c.second = c.second - 60;
c.minute ++;
}
while(c.minute >= 60)
{
c.minute = c.minute - 60;
c.hour ++;
}
time = c;
}
return time;
}
CTime CTime::operator+=(int s)
{
second = second + s;
while(second >= 60)
{
second = second - 60;
minute ++;
}
while(minute >= 60)
{
minute = minute - 60;
hour ++;
}
return *this;
}
CTime CTime::operator-=(int s)
{
int min;
min = minute;
while(second < s)
{
minute = minute --;
while(min < 0)
{
hour--;
minute = minute + 60;
}
second = second + 60;
}
second = second - s;
return *this;
}
bool CTime::operator > (CTime &t)
{
if(hour > t.hour) return true;
else if (minute > t.minute) return true;
else if (minute > t.second) return true;
else
return false;
}
bool CTime::operator < (CTime &t)
{
if(hour < t.hour) return true;
else if (minute < t.minute) return true;
else if (minute < t.second) return true;
else
return false;
}
bool CTime::operator == (CTime &t)
{
if(hour == t.hour && minute == t.minute && second == t.second)
return true;
return false;
}
bool CTime::operator >= (CTime &t)
{
if(operator < (t) != 1)
return true;
return false;
}
bool CTime::operator <= (CTime &t)
{
if(operator > (t) != 1)
return true;
return false;
}
bool CTime::operator != (CTime &t)
{
if(hour != t.hour || minute != t.minute || second != t.second)
return true;
return false;
}
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 + t2;
cout << "t1 + t2 = ";
t.display();
t = t1 - t2;
cout << "t1 - t2 = ";
t.display();
t = t1;
t = t + 35;
cout<< "t1 + 35 = ";
t .display();
t = t2;
t = t - 8;
cout<< "t2 - 8 = " ;
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();
t2 = --t;
cout << "--t2 = ";
t.display();
t1 +=t2;
cout << endl;
cout << "t1 += t2,t1 = " ;
t1.display();
t2.display ();////////////
t2 -= t1;
cout << "t2 -= t1,t2 = " ;
t2.display();
t1 += 5;
cout << "t1 += 5 = " ;
t1.display();
t1 -= 6;
cout << "t1 -= 6 = " ;
t1.display();
system("pause");
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Linux Shell 프로 그래 밍 - 텍스트 처리 grep, sed사용자 가 지정 한 '모드' 에 따라 대상 텍스트 를 일치 하 게 검사 하고 일치 하 는 줄 을 인쇄 합 니 다. ##포함 되 지 않 음, 역방향 일치 \ ##키워드 앞 뒤 가 맞지 않 고 키워드 만 일치 합 니 다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.