當前位置:編程學習大全網 - 源碼下載 - 用c++。 將時 ,分,秒表示的時間用壹個CTime類表示。具體要求如下:

用c++。 將時 ,分,秒表示的時間用壹個CTime類表示。具體要求如下:

/*

將時 ,分,秒表示的時間用壹個CTime類表示。

1)重載“>”和“<”實現當前時間對象表示的時間大於和小於某個對象的時間。

2)重載“==”實現當前時間對象表示的時間等於某個對象的時間。

3)重載“>>”實現按時間格式輸入。

4)重載“<<”實現按時間格式輸出。

5)析構函數:輸出當前時間。

*/

#include <iostream.h>

#include <ctime>

class CTime

{

public:

CTime();

~CTime();

bool operator >(CTime&);

bool operator <(CTime&);

bool operator ==(CTime&);

friend istream& operator >>(istream&,CTime&);

friend ostream& operator <<(ostream&,CTime&);

private:

int hour;

int minute;

int second;

};

CTime:: CTime()

{

this->hour = this->minute = this->second = 0;

}

CTime::~CTime()

{

char buffer[50];

time_t it=time(NULL);

strftime(buffer,50,"當前時間:%H:%M:%S",localtime(&it));

cout<<buffer<<endl;

}

bool CTime:: operator >(CTime& ct)

{

return (this->hour*3600 + this->minute*60 + this->second)

>

(ct.hour*3600 + ct.minute*60 + ct.second);

}

bool CTime:: operator <(CTime& ct)

{

return (this->hour*3600 + this->minute*60 + this->second)

<

(ct.hour*3600 + ct.minute*60 + ct.second);

}

bool CTime:: operator ==(CTime& ct)

{

return (this->hour*3600 + this->minute*60 + this->second)

==

(ct.hour*3600 + ct.minute*60 + ct.second);

}

istream& operator >>(istream& in,CTime& ct)

{

cout<<"hour:"; in>>ct.hour;

cout<<"minute:"; in>>ct.minute;

cout<<"second:"; in>>ct.second;

return in;

}

ostream& operator <<(ostream& out,CTime& ct)

{

out<<"time:"<<ct.hour<<":"<<ct.minute<<":"<<ct.second<<endl;

return out;

}

int main(int argc, char *argv[])

{

CTime ct1 = CTime();

CTime ct2 = CTime();

cout<<"ct1:"<<endl;

cout<<"-------------"<<endl;

//3)重載“>>”實現按時間格式輸入。

cin>>ct1;

//4)重載“<<”實現按時間格式輸出。

cout<<ct1;

cout<<"-------------"<<endl;

cout<<"ct2:"<<endl;

cout<<"-------------"<<endl;

//3)重載“>>”實現按時間格式輸入。

cin>>ct2;

//4)重載“<<”實現按時間格式輸出。

cout<<ct2;

cout<<"-------------"<<endl;

//1)重載“>”和“<”實現當前時間對象表示的時間大於和小於某個對象的時間。

cout<<"ct1 > ct2:"<<(ct1 > ct2?"true":"false")<<endl;

cout<<"ct1 < ct2:"<<(ct1 < ct2?"true":"false")<<endl;

cout<<"-------------"<<endl;

//2)重載“==”實現當前時間對象表示的時間等於某個對象的時間。

cout<<"ct1 == ct1:"<<(ct1 == ct1?"true":"false")<<endl;

cout<<"ct2 == ct2:"<<(ct2 == ct2?"true":"false")<<endl;

cout<<"ct1 == ct2:"<<(ct1 == ct2?"true":"false")<<endl;

cout<<"-------------"<<endl;

//5)析構函數:輸出當前時間。

//ct1 和 ct2 被析構時實現

return 0;

}

/*

test input:

12

44

30

16

33

22

test output:

ct1:

-------------

hour:12

minute:44

second:30

time:12:44:30

-------------

ct2:

-------------

hour:

16

minute:33

second:22

time:16:33:22

-------------

ct1 > ct2:false

ct1 < ct2:true

-------------

ct1 == ct1:true

ct2 == ct2:true

ct1 == ct2:false

-------------

當前時間:12:57:51

當前時間:12:57:51

*/

  • 上一篇:天堂2的專業特色!所有人!
  • 下一篇:JavaEE評論回復源代碼
  • copyright 2024編程學習大全網