日期和時(shí)間課程設(shè)計(jì)報(bào)告_第1頁(yè)
日期和時(shí)間課程設(shè)計(jì)報(bào)告_第2頁(yè)
日期和時(shí)間課程設(shè)計(jì)報(bào)告_第3頁(yè)
日期和時(shí)間課程設(shè)計(jì)報(bào)告_第4頁(yè)
日期和時(shí)間課程設(shè)計(jì)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩49頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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思想和方法:通過(guò)重載運(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ò)誤的處理;通過(guò)拷貝函數(shù)的運(yùn)用,實(shí)現(xiàn)“+”、“-”的重載。1.3增加的新功能:增加了對(duì)運(yùn)算起始日期時(shí)間的修改和各操作菜單的退出功能。2. 詳細(xì)設(shè)計(jì)2.1 c_Tim

2、e類:重載了+、-、+、-、=、!=、=運(yùn)算符;將時(shí)間是否超過(guò)或少于當(dāng)天設(shè)置為靜態(tài)靜態(tài)成員函數(shù),設(shè)置了兩個(gè)輸出函數(shù);“-”中,優(yōu)化了結(jié)構(gòu),以四行將原程序代替,將少于一天的情況在后面列出;“+”中,將數(shù)值超過(guò)范圍的情況的處理在后面列出。2.2 c_Date類:重載了+、-、+、-、=、!=、=運(yùn)算符;設(shè)置了兩個(gè)輸出函數(shù);簡(jiǎn)化了“+”運(yùn)算符,同時(shí)修正了錯(cuò)誤;“+”“-”中,將數(shù)值超過(guò)范圍的情況的處理在后面列出;在第一個(gè)函數(shù)中,對(duì)當(dāng)前時(shí)間日期進(jìn)行了賦值,是起始日期時(shí)間的設(shè)定函數(shù)。2.3 TDmanage類:重載了+、-、+、-、=、!=、=運(yùn)算符;設(shè)置為c_Time和c_Date的派生類,可以實(shí)現(xiàn)對(duì)

3、時(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)試過(guò)程3.1出現(xiàn)了編譯時(shí)無(wú)問題,而調(diào)試時(shí)提示沒有相關(guān)函數(shù)的問題,編譯結(jié)束后載入相關(guān)文件解決了問題。3.2無(wú)法有效退出,將函數(shù)改為swith型解決問題。3.3出現(xiàn)了輸出結(jié)果與預(yù)期不符的狀況,更改了輸出變量名解決問題。3.4最開始時(shí)

4、沒有設(shè)置對(duì)“+”的錯(cuò)誤類進(jìn)行設(shè)置,由于不太可能遇到相關(guān)情況,沒有注意,后來(lái)看課本才發(fā)現(xiàn)。4. 輸入輸出開始界面時(shí)間格式的更改與否主操作菜單時(shí)間類操作,以“+”為例其后繼續(xù)為主操作菜單日期類操作,以“-”為例綜合類操作,以比較為例5. 總結(jié)本程序大量運(yùn)用了運(yùn)算符的重載,并且用到了多文件的處理,對(duì)以后處理或創(chuàng)建大程序很有幫助。而且運(yùn)算符重載實(shí)際上是有很多技巧的,最一般的“+”“-”重載,需要考慮輸入的數(shù)據(jù)的類型及對(duì)輸出結(jié)果的要求。另外錯(cuò)誤處理也是個(gè)難關(guān),即“error”類,不僅要考慮原日期時(shí)間的處理,還要考慮對(duì)運(yùn)算符錯(cuò)誤的處理,很有可能會(huì)落下某些情況,需要不斷調(diào)試以保證每種情況都考慮到了。這個(gè)程序

5、對(duì)個(gè)人對(duì)類處理必須相當(dāng)熟悉,幾乎將所有可能的情況都涉及了,而且難度不算太大,對(duì)于剛接觸c+沒有太長(zhǎng)時(shí)間的我們是個(gè)很好的練習(xí)課程。6. 附件源程序:/cTime_t.h Begin#include #include #ifndef cTime_t_h#define cTime_t_hclass cTime_tstatic int FlagMoreDay;static int FlagLessDay;static int format;int Seconds;int Minutes;int Hours;struct tm *CurrentTime;time_t time_date;public:c

6、Time_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);inline void SetSec (int sec) Seconds=sec;inline void SetMin (int min) Minutes=min;inline void SetHour (int hour) Hours=hour;void prin

7、t() const;void print1() const;inline int GetSec () const return Seconds;inline int GetMin () const return Minutes;inline int GetHour () const return Hours;int getFlagMoreDay()return FlagMoreDay;int getFlagLessDay()return FlagLessDay;void resetFlagMoreDay()FlagMoreDay=0;void resetFlagLessDay()FlagLes

8、sDay=0;bool operator (const cTime_t& T)const;bool operator (const cTime_t& T)const;bool operator = (const 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 oper

9、ator + (int NewMinutes);const cTime_t operator - (int NewMinutes);int getformat1()return format;void operator + ();void operator - (); static void ChangeFormat() format = (format=1)? 2 : 1 ; /format can be only 1 or 2friend ostream& operator (istream &in, cTime_t &T) ;friend class cTDmanage;#endif/c

10、Time_t.h End/cTime_t.cpp/implementation of cTime_t#include cTime_t.hint cTime_t:format=1;int cTime_t:FlagMoreDay=0; / default values for static data membersint cTime_t:FlagLessDay=0;/-cTime_t:cTime_t() /empty ctortime_date=time(0);CurrentTime=localtime(&time_date);Seconds=CurrentTime-tm_sec;Minutes=

11、CurrentTime-tm_min;Hours=CurrentTime-tm_hour;/-cTime_t:cTime_t(const cTime_t &T) /copy ctorSeconds=T.Seconds;Minutes=T.Minutes;Hours=T.Hours;/-const cTime_t& cTime_t:operator = (const cTime_t& T) /operator = functionSeconds=T.Seconds;Minutes=T.Minutes; / copy relevent fieldsHours=T.Hours;return *thi

12、s;/-void cTime_t:print() const /print functionswitch(format)case 1:if(Seconds10 & Minutes10)coutThe time is: Hours:0Minutes:0Seconds.endl;else if(Seconds10)coutThe time is: Hours:Minutes:0Seconds.endl;else if(Minutes10)coutThe time is: Hours:0Minutes:Seconds.endl;elsecoutThe time is: Hours:Minutes:S

13、econds.endl;break;case 2:if(Hours=12)if(Seconds10 & Minutes10)coutThe time is: Hours:0Minutes:0Seconds AM.endl;else if(Minutes10)coutThe time is: Hours:0Minutes:Seconds AM.endl;else if(Seconds10)coutThe time is: Hours:Minutes:0Seconds AM.endl;else coutThe time is: Hours:Minutes:Seconds AM.endl;else

14、if (Seconds10 & Minutes10)coutThe time is: Hours-12:0Minutes:0Seconds PM.endl; else if (Minutes10)coutThe time is: Hours-12:0Minutes:Seconds PM.endl;else if (Seconds10)coutThe time is: Hours-12:Minutes:0Seconds PM.endl;elsecoutThe time is: Hours-12:Minutes:Seconds PM.endl;break;default:coutThe forma

15、t is not okendl; /error (if format value wasnt one of the above/-void cTime_t:print1() const /print functionswitch(format)case 1:if(Seconds10 & Minutes10)cout Hours:0Minutes:0Seconds.endl;else if(Seconds10)cout Hours:Minutes:0Seconds.endl;else if(Minutes10)cout Hours:0Minutes:Seconds.endl;elsecout H

16、ours:Minutes:Seconds.endl;break;case 2:if(Hours=12)if(Seconds10 & Minutes10)coutHours:0Minutes:0Seconds AM.endl;else if(Minutes10)coutHours:0Minutes:Seconds AM.endl;else if(Seconds10)coutHours:Minutes:0Seconds AM.endl;else coutHours:Minutes:Seconds AM.endl;else if (Seconds10 & Minutes10)coutHours-12

17、:0Minutes:0Seconds PM.endl; else if (Minutes10)coutHours-12:0Minutes:Seconds PM.endl;else if (Seconds10)coutHours-12:Minutes:0Seconds PM.endl;elsecoutHours-12:Minutes:Seconds PM.endl;break;default:coutThe format is not okendl; /error (if format value wasnt one of the above/-bool cTime_t:operator (co

18、nst cTime_t& T)const /operator functionif (HoursT.Hours)return true;if(Hours=T.Hours)if(MinutesT.Minutes)return true;if(Minutes=T.Minutes)return (SecondsT.Seconds);return false;/-bool cTime_t:operator = (const cTime_t &T)const /operator = functionif (HoursT.Hours)return true;if(Hours=T.Hours)if(Minu

19、tesT.Minutes)return true;if(Minutes=T.Minutes)return (Seconds (const cTime_t& T)const /operator functionif (HoursT.Hours)return true;if(Hours=T.Hours)if(MinutesT.Minutes)return true;if(Minutes=T.Minutes)return (SecondsT.Seconds);return false;/-bool cTime_t:operator = (const cTime_t& T)const /operato

20、r = functionif (HoursT.Hours)return true;if(Hours=T.Hours)if(MinutesT.Minutes)return true;if(Minutes=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 c

21、Time_t:operator != (const cTime_t &T)const/operator != functionreturn !( (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 dataSecondTem

22、p=Seconds+T.Seconds;if(SecondTemp=60)/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 a

23、dd day to date classHourTemp-=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)

24、 const /operaor - functionint HourTemp,MinuteTemp,SecondTemp;/define 3 temp variables to get time dataHourTemp = Hours-T.Hours;if(HourTemp0)/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(M

25、inuteTemp0)/same for minutesMinuteTemp+=60;-HourTemp;SecondTemp=Seconds-T.Seconds;if(SecondTemp=60)MinuteTemp-=60;HourTemp+=1;while(HourTemp=24)HourTemp-=24;FlagMoreDay+=1;return (cTime_t (HourTemp,MinuteTemp,SecondTemp) );const cTime_t cTime_t:operator - (int NewMinutes)int HourTemp,MinuteTemp,Seco

26、ndTemp;SecondTemp=Seconds;HourTemp=Hours;MinuteTemp=Minutes-NewMinutes;while(MinuteTemp0)MinuteTemp+=60;HourTemp-=1;while(HourTemp0)HourTemp+=24;FlagLessDay=1;return (cTime_t (HourTemp,MinuteTemp,SecondTemp) );/cTime_t.cpp end/cDate_t.h Begin#include #include #ifndef cDate_t_h#define cDate_t_h#defin

27、e 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,November,December ;static int format;static char Mon10;static char DDays10;int Day;int Month;int Year;struct tm *CurrentTime;time_t

28、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);inline void SetDay (int day) Day=day;inline void SetMon (int mon) Month=mon;inline void SetYear (int year) Year=year;void print();void print1();i

29、nline int GetDayOfMonth () const return Day;inline int GetMonth () const return Month;inline int GetYear () const return Year;int GetDayOfYear(int year,int month,int day);int GetDaysInMonth(int month,int year);int GetDayOfWeek();bool IsLeapYear(int year);char* GetDayOfWeekName();char* GetNameOfMonth

30、();int RetriveDay(int Days,int year);int RetriveMonth(int Days,int year);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;bool operator != (const cDate_t& D)const;const cDate_t operator + (cons

31、t cDate_t& D) ;int operator - (cDate_t& D) ;const cDate_t operator + (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: for

32、mat=2; break;case 2: format=3; break;case 3: format=1; friend ostream& 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 error.h#include #define max(a, b) (a) (b) ? (a) :

33、 (b)#define min(a, b) (a) 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) /ctorSetError();if (Year1900) /錯(cuò)誤的數(shù)字 Error1();Error=true;/-cDate_t:cDate_t(const cDate_t &D) /拷貝的構(gòu)造函數(shù)SetError();Day=D.Day;Month=D.

34、Month;Year=D.Year;if (Year1900)/錯(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 (Year1900)/bad data from the other objectError1();Error=true;/置錯(cuò)誤標(biāo)志elseSetError();/正確return *this;/-void cDate_t:print() /輸出char strN; for(int

35、 i=0;iN;+i)stri=MonMonth-1i;/將月份的英文賦值進(jìn)strstri=0;if(Error)/如果日期出錯(cuò),輸出提示信息Error2();return;switch(format) /判斷以什么形式輸出case 1: /歐洲格式coutThe date is: Day/Month/Year. GetDayOfWeekName()endl;break;case 2: /美國(guó)格式coutThe date is: Month/Day/Year . GetDayOfWeekName()endl;break;case 3: /用英文月份形式代替數(shù)字coutThe date is:

36、Day/str/Year . GetDayOfWeekName()endl;break;default:coutThe format is not okendl;/-void cDate_t:print1() /輸出char strN; for(int i=0;iN;+i)stri=MonMonth-1i;/將月份的英文賦值進(jìn)strstri=0;if(Error)/如果日期出錯(cuò),輸出提示信息Error2();return;switch(format) /判斷以什么形式輸出case 1: /歐洲格式coutDay/Month/Year. GetDayOfWeekName()endl;break;

37、case 2: /美國(guó)格式coutMonth/Day/Year . GetDayOfWeekName()endl;break;case 3: /用英文月份形式代替數(shù)字coutDay/str/Year . GetDayOfWeekName()endl;break;default:coutThe format is not okendl;/-bool cDate_t:operator (const cDate_t& D)const / 重載運(yùn)算符/ return true if THIS object is then the secondif (YearD.Year) return true;if

38、(Year=D.Year)if(MonthD.Month)return true;if(Month=D.Month)return (DayD.Day);return false;/-bool cDate_t:operator = (const cDate_t &D)const / 重載=運(yùn)算符/ return true if THIS object is = then the secondif (YearD.Year)return true;if(Year=D.Year)if(MonthD.Month) return true;if(Month=D.Month) return (Day (co

39、nst cDate_t& D)const /重載運(yùn)算符/ return true if THIS object is then the secondif (YearD.Year)return true;if(Year=D.Year) if(MonthD.Month)return true;if(Month=D.Month)return (DayD.Day);return false;/-bool cDate_t:operator = (const cDate_t &D)const /重載=運(yùn)算符/ return true if THIS object is = then the secondi

40、f (YearD.Year)return true;if(Year=D.Year)if(MonthD.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 and the second one are equal in all their date data member return ( (Year=D.Year) & (Month=D.Month) & (Day=D.Day) );/-bool cDate_t:operator != (const cDate_t &D)const/重載!=運(yùn)算符/return true if THIS object and the second one are not equal in all th

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論