版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、日期和時(shí)間課程設(shè)計(jì)報(bào)告1. 功能1.1課程設(shè)計(jì)題目功能:定義了日期類、時(shí)間類和日期時(shí)間綜合類,重載了+、-、+、-、=、>=、<=、=、!=等運(yùn)算符,可以設(shè)置時(shí)間、日期,比較時(shí)間和日期的大小,可以進(jìn)行時(shí)間、日期對(duì)象的運(yùn)算(加減),并按多種格式輸出結(jié)果。1.2思想和方法:通過重載運(yùn)算符使運(yùn)算符實(shí)現(xiàn)對(duì)對(duì)象的加減運(yùn)算,并在結(jié)果輸出時(shí)自動(dòng)載入閏年判斷程序,實(shí)現(xiàn)結(jié)果的智能化輸出;將菜單輸出和結(jié)果輸出隔開,防止混淆;日期類中將星期五單獨(dú)定義,實(shí)現(xiàn)對(duì)錯(cuò)誤的處理;通過拷貝函數(shù)的運(yùn)用,實(shí)現(xiàn)“+”、“-”的重載。1.3增加的新功能:增加了對(duì)運(yùn)算起始日期時(shí)間的修改和各操作菜單的退出功能。2. 詳細(xì)設(shè)計(jì)2
2、.1 c_time類:重載了+、-、+、-、=、!=、<、>、<=、>=運(yùn)算符;將時(shí)間是否超過或少于當(dāng)天設(shè)置為靜態(tài)靜態(tài)成員函數(shù),設(shè)置了兩個(gè)輸出函數(shù);“-”中,優(yōu)化了結(jié)構(gòu),以四行將原程序代替,將少于一天的情況在后面列出;“+”中,將數(shù)值超過范圍的情況的處理在后面列出。2.2 c_date類:重載了+、-、+、-、=、!=、<、>、<=、>=運(yùn)算符;設(shè)置了兩個(gè)輸出函數(shù);簡(jiǎn)化了“+”運(yùn)算符,同時(shí)修正了錯(cuò)誤;“+”“-”中,將數(shù)值超過范圍的情況的處理在后面列出;在第一個(gè)函數(shù)中,對(duì)當(dāng)前時(shí)間日期進(jìn)行了賦值,是起始日期時(shí)間的設(shè)定函數(shù)。2.3 tdmanage類
3、:重載了+、-、+、-、=、!=、<、>、<=、>=運(yùn)算符;設(shè)置為c_time和c_date的派生類,可以實(shí)現(xiàn)對(duì)時(shí)間和日期的混合加減比較。2.4 tdglobal類:為輸入輸出的專門的類,重載了<<,>>運(yùn)算符。2.5 error類:為專門處理錯(cuò)誤的類,包括對(duì)原日期時(shí)間少于1900的處理,以及在“+”、“-”中結(jié)果少于1900的處理,以及輸出時(shí)日期時(shí)間少于1900的處理。2.6 main類:以swith型進(jìn)行菜單的輸出,主要是對(duì)所有程序的綜合處理,對(duì)菜單項(xiàng)的編輯,是最后的運(yùn)行程序。3. 調(diào)試過程3.1出現(xiàn)了編譯時(shí)無問題,而調(diào)試時(shí)提示沒有相關(guān)函數(shù)的
4、問題,編譯結(jié)束后載入相關(guān)文件解決了問題。3.2無法有效退出,將函數(shù)改為swith型解決問題。3.3出現(xiàn)了輸出結(jié)果與預(yù)期不符的狀況,更改了輸出變量名解決問題。3.4最開始時(shí)沒有設(shè)置對(duì)“+”的錯(cuò)誤類進(jìn)行設(shè)置,由于不太可能遇到相關(guān)情況,沒有注意,后來看課本才發(fā)現(xiàn)。4. 輸入輸出開始界面時(shí)間格式的更改與否主操作菜單時(shí)間類操作,以“+”為例其后繼續(xù)為主操作菜單日期類操作,以“-”為例綜合類操作,以比較為例5. 總結(jié)本程序大量運(yùn)用了運(yùn)算符的重載,并且用到了多文件的處理,對(duì)以后處理或創(chuàng)建大程序很有幫助。而且運(yùn)算符重載實(shí)際上是有很多技巧的,最一般的“+”“-”重載,需要考慮輸入的數(shù)據(jù)的類型及對(duì)輸出結(jié)果的要求。
5、另外錯(cuò)誤處理也是個(gè)難關(guān),即“error”類,不僅要考慮原日期時(shí)間的處理,還要考慮對(duì)運(yùn)算符錯(cuò)誤的處理,很有可能會(huì)落下某些情況,需要不斷調(diào)試以保證每種情況都考慮到了。這個(gè)程序?qū)€(gè)人對(duì)類處理必須相當(dāng)熟悉,幾乎將所有可能的情況都涉及了,而且難度不算太大,對(duì)于剛接觸c+沒有太長(zhǎng)時(shí)間的我們是個(gè)很好的練習(xí)課程。6. 附件源程序:/ctime_t.h begin#include <iostream.h>#include <time.h>#ifndef ctime_t_h#define ctime_t_hclass ctime_tstatic int flagmoreday;static
6、 int flaglessday;static int format;int seconds;int minutes;int hours;struct tm *currenttime;time_t time_date;public:ctime_t();ctime_t(const ctime_t &t);ctime_t(int hour,int min=0,int sec=0):seconds(sec),minutes(min),hours(hour) ;ctime_t();const ctime_t& operator = (const ctime_t& t);inli
7、ne void setsec (int sec) seconds=sec;inline void setmin (int min) minutes=min;inline void sethour (int hour) hours=hour;void print() const;void print1() const;inline int getsec () const return seconds;inline int getmin () const return minutes;inline int gethour () const return hours;int getflagmored
8、ay()return flagmoreday;int getflaglessday()return flaglessday;void resetflagmoreday()flagmoreday=0;void resetflaglessday()flaglessday=0;bool operator < (const ctime_t& t)const;bool operator <= (const ctime_t& t)const;bool operator > (const ctime_t& t)const;bool operator >= (c
9、onst ctime_t& t)const;bool operator = (const ctime_t& t)const;bool operator != (const ctime_t& t)const;const ctime_t operator + (const ctime_t& t) const;int operator - (ctime_t& t);const ctime_t operator + (int newminutes);const ctime_t operator - (int newminutes);int getformat1(
10、)return format;void operator + ();void operator - (); static void changeformat() format = (format=1)? 2 : 1 ; /format can be only 1 or 2friend ostream& operator << (ostream &out,const ctime_t &t) ; friend istream& operator >> (istream &in, ctime_t &t) ;friend clas
11、s ctdmanage;#endif/ctime_t.h end/ctime_t.cpp/implementation of ctime_t#include "ctime_t.h"int ctime_t:format=1;int ctime_t:flagmoreday=0; / default values for static data membersint ctime_t:flaglessday=0;/-ctime_t:ctime_t() /empty c'tortime_date=time(0);currenttime=localtime(&time_
12、date);seconds=currenttime->tm_sec;minutes=currenttime->tm_min;hours=currenttime->tm_hour;/-ctime_t:ctime_t(const ctime_t &t) /copy c'torseconds=t.seconds;minutes=t.minutes;hours=t.hours;/-const ctime_t& ctime_t:operator = (const ctime_t& t) /operator = functionseconds=t.seco
13、nds;minutes=t.minutes; / copy relevent fieldshours=t.hours;return *this;/-void ctime_t:print() const /print functionswitch(format)case 1:if(seconds<10 && minutes<10)cout<<"the time is: "<< hours<<":0"<<minutes<<":0"<<sec
14、onds<<"."<<endl;else if(seconds<10)cout<<"the time is: "<< hours<<":"<<minutes<<":0"<<seconds<<"."<<endl;else if(minutes<10)cout<<"the time is: "<< hours<<&
15、quot;:0"<<minutes<<":"<<seconds<<"."<<endl;elsecout<<"the time is: "<< hours<<":"<<minutes<<":"<<seconds<<"."<<endl;break;case 2:if(hours<=12)if(seconds&l
16、t;10 && minutes<10)cout<<"the time is: "<<hours<<":0"<<minutes<<":0"<<seconds<< " am."<<endl;else if(minutes<10)cout<<"the time is: "<<hours<<":0"<<minute
17、s<<":"<<seconds<< " am."<<endl;else if(seconds<10)cout<<"the time is: "<<hours<<":"<<minutes<<":0"<<seconds<< " am."<<endl;else cout<<"the time is: "
18、<<hours<<":"<<minutes<<":"<<seconds<< " am."<<endl;else if (seconds<10 && minutes<10)cout<<"the time is: "<<hours-12<<":0"<<minutes<<":0"<<seconds&l
19、t;< " pm."<<endl; else if (minutes<10)cout<<"the time is: "<<hours-12<<":0"<<minutes<<":"<<seconds<< " pm."<<endl;else if (seconds<10)cout<<"the time is: "<<hours-12
20、<<":"<<minutes<<":0"<<seconds<< " pm."<<endl;elsecout<<"the time is: "<<hours-12<<":"<<minutes<<":"<<seconds<< " pm."<<endl;break;default:cout<
21、<"the format is not ok"<<endl; /error (if format value wasn't one of the above/-void ctime_t:print1() const /print functionswitch(format)case 1:if(seconds<10 && minutes<10)cout<< hours<<":0"<<minutes<<":0"<<second
22、s<<"."<<endl;else if(seconds<10)cout<< hours<<":"<<minutes<<":0"<<seconds<<"."<<endl;else if(minutes<10)cout<< hours<<":0"<<minutes<<":"<<seconds<&
23、lt;"."<<endl;elsecout<< hours<<":"<<minutes<<":"<<seconds<<"."<<endl;break;case 2:if(hours<=12)if(seconds<10 && minutes<10)cout<<hours<<":0"<<minutes<<":0&q
24、uot;<<seconds<< " am."<<endl;else if(minutes<10)cout<<hours<<":0"<<minutes<<":"<<seconds<< " am."<<endl;else if(seconds<10)cout<<hours<<":"<<minutes<<":0&q
25、uot;<<seconds<< " am."<<endl;else cout<<hours<<":"<<minutes<<":"<<seconds<< " am."<<endl;else if (seconds<10 && minutes<10)cout<<hours-12<<":0"<<minutes<&l
26、t;":0"<<seconds<< " pm."<<endl; else if (minutes<10)cout<<hours-12<<":0"<<minutes<<":"<<seconds<< " pm."<<endl;else if (seconds<10)cout<<hours-12<<":"<<minu
27、tes<<":0"<<seconds<< " pm."<<endl;elsecout<<hours-12<<":"<<minutes<<":"<<seconds<< " pm."<<endl;break;default:cout<<"the format is not ok"<<endl; /error (if forma
28、t value wasn't one of the above/-bool ctime_t:operator < (const ctime_t& t)const /operator < functionif (hours<t.hours)return true;if(hours=t.hours)if(minutes<t.minutes)return true;if(minutes=t.minutes)return (seconds<t.seconds);return false;/-bool ctime_t:operator <= (cons
29、t ctime_t &t)const /operator <= functionif (hours<t.hours)return true;if(hours=t.hours)if(minutes<t.minutes)return true;if(minutes=t.minutes)return (seconds<=t.seconds);return false;/-bool ctime_t:operator > (const ctime_t& t)const /operator > functionif (hours>t.hours)r
30、eturn true;if(hours=t.hours)if(minutes>t.minutes)return true;if(minutes=t.minutes)return (seconds>t.seconds);return false;/-bool ctime_t:operator >= (const ctime_t& t)const /operator >= functionif (hours>t.hours)return true;if(hours=t.hours)if(minutes>t.minutes)return true;if(m
31、inutes=t.minutes)return (seconds>=t.seconds);return false;/-bool ctime_t:operator = (const ctime_t &t)const /operator = functionreturn ( (hours=t.hours) && (minutes=t.minutes) && (seconds=t.seconds) );/-bool ctime_t:operator != (const ctime_t &t)const/operator != functionr
32、eturn !( (hours=t.hours) && (minutes=t.minutes) && (seconds=t.seconds) );/-const ctime_t ctime_t:operator + (const ctime_t &t) const /operator + functionint hourtemp,minutetemp,secondtemp;/define 3 temp variables to get time datasecondtemp=seconds+t.seconds;if(secondtemp>=60)/
33、more thrn 1 minutesecondtemp-=60;minutetemp=minutes+t.minutes+1;/so add to minuteelseminutetemp=minutes+t.minutes;if(minutetemp>=60)/more then 1 hourminutetemp-=60;hourtemp=hours+t.hours+1;/add to hourelsehourtemp=hours+t.hours;if(hourtemp>=24)flagmoreday=1; /to add day to date classhourtemp-=
34、24;return (ctime_t (hourtemp,minutetemp,secondtemp) ); /return local time class /-int ctime_t:operator - (ctime_t &t)int newhour,newminute;newhour=hours-t.hours;newminute=minutes-t.minutes;return newhour*60+newminute;/*const ctime_t ctime_t:operator - (const ctime_t &t) const /operaor - func
35、tionint hourtemp,minutetemp,secondtemp;/define 3 temp variables to get time datahourtemp = hours-t.hours;if(hourtemp<0)/t class hour was bigger then this classflaglessday=1; /to cut 1 day form date classhourtemp+=24;/ add 24 hours to previous dayminutetemp=minutes-t.minutes;if(minutetemp<0)/sa
36、me for minutesminutetemp+=60;-hourtemp;secondtemp=seconds-t.seconds;if(secondtemp<0)/same for secondssecondtemp+=60;-minutetemp;return ( ctime_t ( hourtemp,minutetemp,secondtemp) );/return local class */-void ctime_t:operator + () /operator + functionif ( (seconds=59) && (minutes=59) &
37、;& (hours=23) ) / end of a dayflagmoreday=1;/to add day to day classseconds=0;minutes=0;hours=0;else if( (seconds=59) && (minutes=59) )/end of hourseconds=0;minutes=0;+hours;else if( seconds=59 )/end of minuteseconds=0;+minutes;else+seconds;/-void ctime_t:operator - () /operator - functi
38、onif ( (seconds=0) && (minutes=0) && (hours=0) )/start of dayflaglessday=1;/to substruct 1 day from day classseconds=59;minutes=59;hours=23;else if( (seconds=0) && (minutes=0) )/start of hourseconds=59;minutes=59;-hours;else if( seconds=0 )/start of minuteseconds=59;-minutes;
39、else-seconds;const ctime_t ctime_t:operator + (int newminutes)int hourtemp,minutetemp,secondtemp;secondtemp=seconds;hourtemp=hours;minutetemp=minutes+newminutes;while(minutetemp>=60)minutetemp-=60;hourtemp+=1;while(hourtemp>=24)hourtemp-=24;flagmoreday+=1;return (ctime_t (hourtemp,minutetemp,s
40、econdtemp) );const ctime_t ctime_t:operator - (int newminutes)int hourtemp,minutetemp,secondtemp;secondtemp=seconds;hourtemp=hours;minutetemp=minutes-newminutes;while(minutetemp<0)minutetemp+=60;hourtemp-=1;while(hourtemp<0)hourtemp+=24;flaglessday=1;return (ctime_t (hourtemp,minutetemp,second
41、temp) );/ctime_t.cpp end/cdate_t.h begin#include <iostream.h>#include <time.h>#ifndef cdate_t_h#define cdate_t_h#define n 3class cdate_tenum days saturday,sunday,monday,tuesday,wensday,thursday,friday ;enum months none,january,february,mars,april,may,june,july,august,september,october,no
42、vember,december ;static int format;static char mon10;static char ddays10;int day;int month;int year;struct tm *currenttime;time_t time_date;public: bool error;cdate_t();cdate_t(const cdate_t &d);cdate_t(int year,int month,int day);cdate_t()const cdate_t& operator = (const cdate_t& d);inl
43、ine void setday (int day) day=day;inline void setmon (int mon) month=mon;inline void setyear (int year) year=year;void print();void print1();inline int getdayofmonth () const return day;inline int getmonth () const return month;inline int getyear () const return year;int getdayofyear(int year,int mo
44、nth,int day);int getdaysinmonth(int month,int year);int getdayofweek();bool isleapyear(int year);char* getdayofweekname();char* getnameofmonth();int retriveday(int days,int year);int retrivemonth(int days,int year);bool operator < (const cdate_t& d)const;bool operator <= (const cdate_t&
45、; d)const;bool operator > (const cdate_t& d)const;bool operator >= (const cdate_t& d)const;bool operator = (const cdate_t& d)const;bool operator != (const cdate_t& d)const;const cdate_t operator + (const cdate_t& d) ;int operator - (cdate_t& d) ;const cdate_t operator +
46、 (int days) ;const cdate_t operator - (int days) ;int getformat2()return format;void operator + ();void operator - (); static void cdate_t:changeformat() /implementation of static function must be in headerswitch(format) case 1: format=2; break;case 2: format=3; break;case 3: format=1; friend ostrea
47、m& operator << (ostream &out,const cdate_t &d) ; friend istream& operator >> (istream &in, cdate_t &d) ;void seterror() error=false;friend class ctdmanage;#endif/cdate_t.h end/cdate_t.cpp begin/implementation of cdate_t.h#include "cdate_t.h"#include &quo
48、t;error.h"#include <math.h>#define max(a, b) (a) > (b) ? (a) : (b)#define min(a, b) (a) < (b) ? (a) : (b)int cdate_t:format=1;/靜態(tài)成員變量char cdate_t:mon10 = "janaury","february","mars","april","may","juny","july",&qu
49、ot;august","september","october","november","december" ;char cdate_t:ddays10 = "saturday","sunday","monday","tuesday","wensday","thursday","friday" ;/-cdate_t:cdate_t() /構(gòu)造函數(shù),賦值當(dāng)前時(shí)間sete
50、rror();time_date=time(0);currenttime=localtime(&time_date); day=currenttime->tm_mday;month=currenttime->tm_mon+1;year=currenttime->tm_year+1900;/-cdate_t:cdate_t(int year,int month,int day):year(year),month(month),day(day) /c'torseterror();if (year<1900) /錯(cuò)誤的數(shù)字 error1();error=tru
51、e;/-cdate_t:cdate_t(const cdate_t &d) /拷貝的構(gòu)造函數(shù)seterror();day=d.day;month=d.month;year=d.year;if (year<1900)/錯(cuò)誤的數(shù)字error1();error=true;/-const cdate_t& cdate_t:operator = (const cdate_t& d) /重載=運(yùn)算符day=d.day;month=d.month;year=d.year;if (year<1900)/bad data from the other objecterror1
52、();error=true;/置錯(cuò)誤標(biāo)志elseseterror();/正確return *this;/-void cdate_t:print() /輸出char strn; for(int i=0;i<n;+i)stri=monmonth-1i;/將月份的英文賦值進(jìn)strstri='0'if(error)/如果日期出錯(cuò),輸出提示信息error2();return;switch(format) /判斷以什么形式輸出case 1: /歐洲格式cout<<"the date is: "<< day<<"/&qu
53、ot;<<month<<"/"<<year<<"."<<" "<<getdayofweekname()<<""<<endl;break;case 2: /美國(guó)格式cout<<"the date is: "<<month<<"/"<<day<<"/"<<year<< ".
54、"<<" "<<getdayofweekname()<<""<<endl;break;case 3: /用英文月份形式代替數(shù)字cout<<"the date is: "<<day<<"/"<<str<<"/"<<year<< "."<<" "<<getdayofweekname()<&l
55、t;""<<endl;break;default:cout<<"the format is not ok"<<endl;/-void cdate_t:print1() /輸出char strn; for(int i=0;i<n;+i)stri=monmonth-1i;/將月份的英文賦值進(jìn)strstri='0'if(error)/如果日期出錯(cuò),輸出提示信息error2();return;switch(format) /判斷以什么形式輸出case 1: /歐洲格式cout<<day<
56、<"/"<<month<<"/"<<year<<"."<<" "<<getdayofweekname()<<""<<endl;break;case 2: /美國(guó)格式cout<<month<<"/"<<day<<"/"<<year<< "."<<"
57、; "<<getdayofweekname()<<""<<endl;break;case 3: /用英文月份形式代替數(shù)字cout<<day<<"/"<<str<<"/"<<year<< "."<<" "<<getdayofweekname()<<""<<endl;break;default:cout<<
58、;"the format is not ok"<<endl;/-bool cdate_t:operator < (const cdate_t& d)const / 重載<運(yùn)算符/ return true if this object is < then the secondif (year<d.year) return true;if(year=d.year)if(month<d.month)return true;if(month=d.month)return (day<d.day);return false;/-b
59、ool cdate_t:operator <= (const cdate_t &d)const / 重載<=運(yùn)算符/ return true if this object is <= then the secondif (year<d.year)return true;if(year=d.year)if(month<d.month) return true;if(month=d.month) return (day<=d.day);return false;/-bool cdate_t:operator > (const cdate_t& d)const /重載>運(yùn)算符/ return true if this object is > then the secondif (year>d.year)return true;if(year=d.year) if(month>d.month)return true;if(month=d.month)return (day>d.day);return false;/-bool cdate_t:operator >= (const cdate_t &d)const /重載>=運(yùn)算符/ re
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業(yè)年會(huì)租車服務(wù)合同
- 臨海市網(wǎng)絡(luò)推廣勞動(dòng)合同
- 二手音響設(shè)備買賣協(xié)議
- 企業(yè)合同風(fēng)險(xiǎn)防范手冊(cè)
- 企業(yè)年會(huì)策劃化妝師服務(wù)合同
- 乳制品運(yùn)輸協(xié)議模板
- 企業(yè)培訓(xùn)外包合同
- 伐木作業(yè)安全合同
- 企業(yè)合作標(biāo)準(zhǔn)印刷合同范本
- 倉儲(chǔ)物流勞務(wù)工協(xié)議書
- 月北京地區(qū)成人本科學(xué)士學(xué)位英語真題與答案
- 檢具校準(zhǔn)規(guī)程
- 供應(yīng)商審核檢查表(共6頁)
- 小學(xué) 漢語拼音字母描紅(A4打印)
- 小組合作學(xué)習(xí)方法指導(dǎo)(課堂PPT)
- 09輸煤系統(tǒng)拆除方案
- VC6.0編的“文件讀寫”的詳細(xì)操作步驟(兩種方式MFC和基于對(duì)話框)
- PHC預(yù)應(yīng)力管樁報(bào)價(jià)單(錘擊、靜壓)綜合報(bào)價(jià)表
- 沁園春·疊嶂西馳.中職課件電子教案
- 中級(jí)財(cái)務(wù)會(huì)計(jì)英文課程教學(xué)大綱
- 核醫(yī)學(xué)第1章核醫(yī)學(xué)物理基礎(chǔ)
評(píng)論
0/150
提交評(píng)論