日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

项目二--Time类

發布時間:2024/1/8 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 项目二--Time类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


?

/* * Copyright (c) 2011, 煙臺大學計算機學院 * All rights reserved. * 作 者:王靜 * 完成日期:2013 年 5 月 5 日 * 版 本 號:v1.0 * 輸入描述: * 問題描述: * 程序輸出: * 問題分析: * 算法設計:略 */ #include <iostream> using namespace std; class CTime {private: //unsigned short int hour; // 時 //unsigned short int minute; // 分 //unsigned short int second; // 秒 int hour;int minute;int second; public:CTime(){hour=0;minute=0;second=0;}friend istream& operator>>(istream&cin,CTime&t);friend ostream & operator<<(ostream&cout,CTime&t);//比較運算符(二目)的重載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:15CTime operator-(CTime &c);//對照+理解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);CTime operator-=(int s); }; //下面實現所有的運算符重載代碼。 //為簡化編程,請注意通過調用已有函數,利用好各函數之間的關系//比較運算符(二目)的重載 bool CTime::operator > (CTime &t) {if(hour>t.hour){return true;}else{if(hour==t.hour&&minute>t.minute){return true;}else{if(minute==t.minute&&second>t.second){return true;}else{return false;}}} } bool CTime::operator < (CTime &t) {if(hour<t.hour){return true;}else{if(hour==t.hour&&minute<t.minute){return true;}else{if(minute==t.minute&&second<t.second){return true;}else{return false;}}} } bool CTime::operator >= (CTime &t) {if(hour>=t.hour){if(hour==t.hour&&minute>=t.minute){if(minute==t.minute&&second>=t.second){return true;}else{return false;}}else{return false;}}else{return false;} } bool CTime::operator <= (CTime &t) {if(hour<=t.hour){if(hour==t.hour&&minute<=t.minute){if(minute==t.minute&&second<=t.second){return true;}else{return false;}}else{return false;}}else{return false;} } 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(hour!=t.hour||minute!=t.minute||second!=t.second){return true;}else {return false;} } //二目運算符的重載 CTime CTime::operator+(CTime &c)//返回c所規定的時、分、秒后的時間,例t1(8,20,25),t2(11,20,50),t1+t2為:41:15 {CTime t;t.hour=hour+c.hour;t.minute=minute+c.minute;t.second=second+c.second;if(t.hour>24||t.minute>60||t.second>60){if(t.second>60){second=t.second%60;t.minute=t.minute+t.second/60;if(t.minute>60){minute=t.minute%60;t.hour=t.hour+t.minute/60;if(t.hour>24){t.hour=t.hour/24;}t.minute=minute;}t.second=second;}}return t; } CTime CTime::operator-(CTime &c)//對照+理解 {CTime t;t.hour=hour-c.hour;t.minute=minute-c.minute;t.second=second-c.second;int i=0,j=0;if(t.second<0||t.minute<0||t.hour<0){for(;t.second<0;i++){t.second+=60;}t.minute-=i;for(;t.minute<0;j++){t.minute+=60;}t.hour-=j;for(;t.hour<0;){t.hour+=24;}}return t; } CTime CTime::operator+(int s)//返回s秒后的時間 {CTime t;t.second=s+second;t.minute=minute;t.hour=hour;if(t.hour>24||t.minute>60||t.second>60){if(t.second>60){second=t.second%60;t.minute=minute+t.second/60;t.second=second;if(t.minute>60){minute=t.minute%60;t.hour=hour+t.minute/60;t.minute=minute;if(t.hour>24){t.hour=t.hour%24;}}}}return t; } CTime CTime::operator-(int s)//返回s秒前的時間 {CTime t;int i=0,j=0;t.second=second-s;t.minute=minute;t.hour=hour;if(t.second<0||t.minute<0||t.hour<0){if(t.second<0){for(;t.second<0;i++){t.second+=60;}t.minute-=i;for(;t.minute<0;j++){t.minute+=60;}t.hour-=j;for(;t.hour<0;){t.hour+=24;}}}return t; } //一目運算符的重載 CTime CTime::operator++(int)//后置++,下一秒 {CTime t=*this;*this=*this+1;return t; } CTime CTime::operator++()//前置++,下一秒 {*this=*this+1;return *this; }CTime CTime::operator--(int)//后置--,前一秒 {CTime t=*this;*this=*this-1;return t; }CTime CTime::operator--()//前置--,前一秒 {*this=*this-1;return *this; }//賦值運算符的重載 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; } istream& operator>>(istream&cin,CTime&t) {cout<<"請輸入時,分,秒"<<endl;cin>>t.hour>>t.minute>>t.second;return cin; } ostream & operator<<(ostream &cout,CTime&t) {cout<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;return cout; } int main() {CTime t1,t2,t;cin>>t1>>t2;cout<<"t1為:"<<t1;cout<<"t2為:"<<t2;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;cout<<endl;//下面自行設計對其他運算符的重載的測試t=t1+t2;cout<<t;t=t1-t2;cout<<t;t=t1-300;cout<<t;t=t1+300;cout<<t;t=t1++;t=++t1;t1--;--t1;t1+=t2;t1-=t2;t1+=300;t1-=300;return 0; }


運行結果:

?

總結

以上是生活随笔為你收集整理的项目二--Time类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。