日期和时间课程设计报告.docx

上传人:scccc 文档编号:12679275 上传时间:2021-12-05 格式:DOCX 页数:54 大小:186.50KB
返回 下载 相关 举报
日期和时间课程设计报告.docx_第1页
第1页 / 共54页
日期和时间课程设计报告.docx_第2页
第2页 / 共54页
日期和时间课程设计报告.docx_第3页
第3页 / 共54页
日期和时间课程设计报告.docx_第4页
第4页 / 共54页
日期和时间课程设计报告.docx_第5页
第5页 / 共54页
点击查看更多>>
资源描述

《日期和时间课程设计报告.docx》由会员分享,可在线阅读,更多相关《日期和时间课程设计报告.docx(54页珍藏版)》请在三一文库上搜索。

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

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

3、类:重载了+、-、+、-、=、!=、<、>、<=、>=运算符;设置为c_Time和c_Date的派生类,可以实现对时间和日期的混合加减比较。2.4 TDglobal类:为输入输出的专门的类,重载了<<,>>运算符。2.5 error类:为专门处理错误的类,包括对原日期时间少于1900的处理,以及在“+”、“-”中结果少于1900的处理,以及输出时日期时间少于1900的处理。2.6 main类:以swith型进行菜单的输出,主要是对所有程序的综合处理,对菜单项的编辑,是最后的运行程序。3. 调试过程3.1出现了编译时无问题,而调试时提示没有相关函数

4、的问题,编译结束后载入相关文件解决了问题。3.2无法有效退出,将函数改为swith型解决问题。3.3出现了输出结果与预期不符的状况,更改了输出变量名解决问题。3.4最开始时没有设置对“+”的错误类进行设置,由于不太可能遇到相关情况,没有注意,后来看课本才发现。4. 输入输出开始界面时间格式的更改与否主操作菜单时间类操作,以“+”为例其后继续为主操作菜单日期类操作,以“-”为例综合类操作,以比较为例5. 总结本程序大量运用了运算符的重载,并且用到了多文件的处理,对以后处理或创建大程序很有帮助。而且运算符重载实际上是有很多技巧的,最一般的“+”“-”重载,需要考虑输入的数据的类型及对输出结果的要求

5、。另外错误处理也是个难关,即“error”类,不仅要考虑原日期时间的处理,还要考虑对运算符错误的处理,很有可能会落下某些情况,需要不断调试以保证每种情况都考虑到了。这个程序对个人对类处理必须相当熟悉,几乎将所有可能的情况都涉及了,而且难度不算太大,对于刚接触c+没有太长时间的我们是个很好的练习课程。6. 附件源程序:/cTime_t.h Begin#include <iostream.h>#include <time.h>#ifndef cTime_t_h#define cTime_t_hclass cTime_tstatic int FlagMoreDay;stati

6、c 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);inl

7、ine 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 getFlagMore

8、Day()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 >= (

9、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 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 cla

11、ss 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.Sec

13、onds;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"<<Se

14、conds<<"."<<endl;else if(Seconds<10)cout<<"The time is: "<< Hours<<":"<<Minutes<<":0"<<Seconds<<"."<<endl;else if(Minutes<10)cout<<"The time is: "<< Hours<<

15、":0"<<Minutes<<":"<<Seconds<<"."<<endl;elsecout<<"The time is: "<< Hours<<":"<<Minutes<<":"<<Seconds<<"."<<endl;break;case 2:if(Hours<=12)if(Seconds&

16、lt;10 && Minutes<10)cout<<"The time is: "<<Hours<<":0"<<Minutes<<":0"<<Seconds<< " AM."<<endl;else if(Minutes<10)cout<<"The time is: "<<Hours<<":0"<<Minut

17、es<<":"<<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&

19、lt;< " 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-1

20、2<<":"<<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"<<Secon

22、ds<<"."<<endl;else if(Seconds<10)cout<< Hours<<":"<<Minutes<<":0"<<Seconds<<"."<<endl;else if(Minutes<10)cout<< Hours<<":0"<<Minutes<<":"<<Seconds<

23、<"."<<endl;elsecout<< Hours<<":"<<Minutes<<":"<<Seconds<<"."<<endl;break;case 2:if(Hours<=12)if(Seconds<10 && Minutes<10)cout<<Hours<<":0"<<Minutes<<":0&

24、quot;<<Seconds<< " AM."<<endl;else if(Minutes<10)cout<<Hours<<":0"<<Minutes<<":"<<Seconds<< " AM."<<endl;else if(Seconds<10)cout<<Hours<<":"<<Minutes<<":0&

25、quot;<<Seconds<< " AM."<<endl;else cout<<Hours<<":"<<Minutes<<":"<<Seconds<< " AM."<<endl;else if (Seconds<10 && Minutes<10)cout<<Hours-12<<":0"<<Minutes<&

26、lt;":0"<<Seconds<< " PM."<<endl; else if (Minutes<10)cout<<Hours-12<<":0"<<Minutes<<":"<<Seconds<< " PM."<<endl;else if (Seconds<10)cout<<Hours-12<<":"<<Min

27、utes<<":0"<<Seconds<< " PM."<<endl;elsecout<<Hours-12<<":"<<Minutes<<":"<<Seconds<< " PM."<<endl;break;default:cout<<"The format is not ok"<<endl; /error (if form

28、at 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 <= (con

29、st 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)

30、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)return true;if(Hours=T.Hours)if(Minutes>T.Minutes)return true;if(

31、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 cTime_t:operator != (const cTime_t &T)const/operator != function

32、return !( (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 - fun

35、ctionint 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)/s

36、ame 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) &am

37、p;& (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 - funct

38、ionif ( (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,

40、SecondTemp) );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,Secon

41、dTemp) );/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,N

42、ovember,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);in

43、line 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 m

44、onth,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&am

45、p; 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 ostre

47、am& 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 &qu

48、ot;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;/静态成员变量char cDate_t:Mon10 = "Janaury","February","Mars","April","May","Juny","July",&q

49、uot;August","September","October","November","December" ;char cDate_t:DDays10 = "Saturday","Sunday","Monday","Tuesday","Wensday","Thursday","Friday" ;/-cDate_t:cDate_t() /构造函数,赋值当前时间Set

50、Error();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) /错误的数字 Error1();Error=true;/-cDate_t:cDate_t(const cDate_t &D) /拷贝的构造函数SetError();Day=D.Day;Month=D.Month;Year=D.Year;if (Year<1900)/错误的数字Error1();Error=true;/-const cDate_t& cDate_t:operator = (cons

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1