日期和时间课程设计报告_第1页
日期和时间课程设计报告_第2页
日期和时间课程设计报告_第3页
日期和时间课程设计报告_第4页
日期和时间课程设计报告_第5页
已阅读5页,还剩49页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、日期和时间课程设计报告1. 功能1.1课程设计题目功能:定义了日期类、时间类和日期时间综合类,重载了+、-、+、-、=、=、=、=、!=等运算符,可以设置时间、日期,比较时间和日期的大小,可以进行时间、日期对象的运算(加减),并按多种格式输出结果。1.2思想和方法:通过重载运算符使运算符实现对对象的加减运算,并在结果输出时自动载入闰年判断程序,实现结果的智能化输出;将菜单输出和结果输出隔开,防止混淆;日期类中将星期五单独定义,实现对错误的处理;通过拷贝函数的运用,实现“+”、“-”的重载。1.3增加的新功能:增加了对运算起始日期时间的修改和各操作菜单的退出功能。2. 详细设计2.1 c_Tim

2、e类:重载了+、-、+、-、=、!=、=运算符;将时间是否超过或少于当天设置为静态静态成员函数,设置了两个输出函数;“-”中,优化了结构,以四行将原程序代替,将少于一天的情况在后面列出;“+”中,将数值超过范围的情况的处理在后面列出。2.2 c_Date类:重载了+、-、+、-、=、!=、=运算符;设置了两个输出函数;简化了“+”运算符,同时修正了错误;“+”“-”中,将数值超过范围的情况的处理在后面列出;在第一个函数中,对当前时间日期进行了赋值,是起始日期时间的设定函数。2.3 TDmanage类:重载了+、-、+、-、=、!=、=运算符;设置为c_Time和c_Date的派生类,可以实现对

3、时间和日期的混合加减比较。2.4 TDglobal类:为输入输出的专门的类,重载了运算符。2.5 error类:为专门处理错误的类,包括对原日期时间少于1900的处理,以及在“+”、“-”中结果少于1900的处理,以及输出时日期时间少于1900的处理。2.6 main类:以swith型进行菜单的输出,主要是对所有程序的综合处理,对菜单项的编辑,是最后的运行程序。3. 调试过程3.1出现了编译时无问题,而调试时提示没有相关函数的问题,编译结束后载入相关文件解决了问题。3.2无法有效退出,将函数改为swith型解决问题。3.3出现了输出结果与预期不符的状况,更改了输出变量名解决问题。3.4最开始时

4、没有设置对“+”的错误类进行设置,由于不太可能遇到相关情况,没有注意,后来看课本才发现。4. 输入输出开始界面时间格式的更改与否主操作菜单时间类操作,以“+”为例其后继续为主操作菜单日期类操作,以“-”为例综合类操作,以比较为例5. 总结本程序大量运用了运算符的重载,并且用到了多文件的处理,对以后处理或创建大程序很有帮助。而且运算符重载实际上是有很多技巧的,最一般的“+”“-”重载,需要考虑输入的数据的类型及对输出结果的要求。另外错误处理也是个难关,即“error”类,不仅要考虑原日期时间的处理,还要考虑对运算符错误的处理,很有可能会落下某些情况,需要不断调试以保证每种情况都考虑到了。这个程序

5、对个人对类处理必须相当熟悉,几乎将所有可能的情况都涉及了,而且难度不算太大,对于刚接触c+没有太长时间的我们是个很好的练习课程。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) /错误的数字 Error1();Error=true;/-cDate_t:cDate_t(const cDate_t &D) /拷贝的构造函数SetError();Day=D.Day;Month=D.

34、Month;Year=D.Year;if (Year1900)/错误的数字Error1();Error=true;/-const cDate_t& cDate_t:operator = (const cDate_t& D) /重载=运算符Day=D.Day;Month=D.Month;Year=D.Year;if (Year1900)/bad data from the other objectError1();Error=true;/置错误标志elseSetError();/正确return *this;/-void cDate_t:print() /输出char strN; for(int

35、 i=0;iN;+i)stri=MonMonth-1i;/将月份的英文赋值进strstri=0;if(Error)/如果日期出错,输出提示信息Error2();return;switch(format) /判断以什么形式输出case 1: /欧洲格式coutThe date is: Day/Month/Year. GetDayOfWeekName()endl;break;case 2: /美国格式coutThe date is: Month/Day/Year . GetDayOfWeekName()endl;break;case 3: /用英文月份形式代替数字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;/将月份的英文赋值进strstri=0;if(Error)/如果日期出错,输出提示信息Error2();return;switch(format) /判断以什么形式输出case 1: /欧洲格式coutDay/Month/Year. GetDayOfWeekName()endl;break;

37、case 2: /美国格式coutMonth/Day/Year . GetDayOfWeekName()endl;break;case 3: /用英文月份形式代替数字coutDay/str/Year . GetDayOfWeekName()endl;break;default:coutThe format is not okendl;/-bool cDate_t:operator (const cDate_t& D)const / 重载运算符/ 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 / 重载=运算符/ 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 /重载运算符/ 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 /重载=运算符/ 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/重载=运算符/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/重载!=运算符/return true if THIS object and the second one are not equal in all th

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论