时间和日期报告xym_第1页
时间和日期报告xym_第2页
时间和日期报告xym_第3页
时间和日期报告xym_第4页
时间和日期报告xym_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

1、Visual C+裸程设计报告时间和日期电子工程与光电技术学院通信工程(1)班Xxx0904220xxx2010年4月一、程序功能简介:定义了日期类、时间类和日期时间综合类,重载了+,-,+,-, = ,=,=,=,!=等运算符,可以设置时间、日期,比较时间和日期的大小,进行 时间、日期对象的运算,并输出多种格式的结果。二、课程设计说明:1、原程序日期类中输出星期几的函数有误,不能输出星期五,现修改如下:int cDate_t:GetDayOfWeek()/startingpoint : 1/1/1900 was Monday(Saturday=O,S un day=1,.)/1/1/1900

2、 是星期一,根据这个日期推断当前日期是星期几int weekday=2 ; /in 1/1/1900/ 从 1/1/1900 开始计算for(int TheYear=1900;TheYearYear;+TheYear) /loop to know the day in 1/1/this yearif(lsLeapYear(TheYear)weekday=(weekday+366)%7;elseweekday=(weekday+365)%7;un til here we found the correct day in 1/1/this yearfor(i nt TheMo nth=1;TheM

3、 on thMo nth;+TheMo nth)/年份计算完后,计算月份天数weekday=(weekday+GetDaysI nMon th(TheMo nth,Year)%7;un til here we found the correct day in 1/this monthif(weekday+Day)%7=0)return 6;elsereturn(weekday+Day)%7-1);正确显示星期几/return (weekday+Day)%7-1;(修改前的,无法正确显示星期五)/thecorrect day in THIS day.2、根据要求在日期类中修改日期对象减去日期对象

4、的重载运算符-,使得结果不是另一个日期,而是天数:in t cDate_t:operator - (cDate_t & D)/重载-运算符/修改为日期对象减去日期对象,结果不是另一个日期对象,而是天数int Temp1,Temp2,NewDays=0;Temp仁GetDayOfYear(Year,Mo nth,Day);Temp2=D.GetDayOfYear(D.Year,D.Mo nth,D.Day); if(Year=D.Year)NewDays=max(Temp1,Temp2)-min( Temp1,Temp2);elsefor(i ntTemp3=min( Year,D.Year);

5、Temp3D.Year)NewDays=NewDays+Temp1-Temp2;elseNewDays=-(NewDays+Temp2-Temp1);return NewDays;/*const cDate_t cDate_t:operator- (const cDate_t &D) /operatorfun ction(修改前的函数,结果为另一个日期对象)int YearTemp,M on thTemp,DayTemp,NewDays,Temp1,Temp2;/variables to store to new date objectif(Error | D.Error)/ if no le

6、ss the n 1 object have orror data Error4(); print orror messegeretur n (cDate_t (-1,-1,-1) ); /retur n error class _YearTemp=Year-D.Year;/substract ion of THIS and sec ond year if(YearTemp1900) / errorError1();/pri nt error messegeretur n (cDate_t (-1,-1,-1) ); /retur n error class _Temp仁GetDayOfYea

7、r(Year,Month,Day); / get days of year of THIS objectif(Temp10) /errorError5();/pri nt error messegeretur n (cDate_t (-1,-1,-1) );/retur n error class _Temp2=GetDayOfYear(D.Year,D.Month,D.Day);get days of year ofsec ond objectif(Temp20)/errorError5();/pri nt error messegeretur n (cDate_t (-1,-1,-1) )

8、;/retur n error class _NewDays= Temp1 - Temp2; /if the data ok get their substracti onif(NewDays0) / the substract ion is less the n zeroNewDays = NewDays + (IsLeapYear(YearTemp)? 366 : 365);/ set days for previous yearYearTemp-=1; /sub 1 yearDayTemp=RetriveDay(NewDays,YearTemp); /retrive the curre

9、nt day inthe specific monthMon thTemp=RetriveM on th(NewDays,YearTemp);/same with mon threturn (cDate_t (YearTemp,MonthTemp,DayTemp);/returnlocal class*/3、根据要求在时间类中修改时间对象减去时间对象的重载运算符-,使得结果不是另一个时间,而是分钟数:in t cTime_t:operator - (cTime_t & T)/重载-运算符(结果不是另一个时间对象,而是分钟数)一int n ewhour, newm inu te;n ewhour

10、=Hours-T.Hours;n ewmi nu te=Mi nu tes-T.Mi nu tes;return n ewhour*60+newmi nute;/*const cTime_t cTime_t:operator - (const cTime_t &T) const /operaor-function重载-运算符/修改前的,结果为时间对象int HourTemp,MinuteTemp,SecondTemp;/define 3 temp variables to get time data/ 定义三个时间变量HourTemp = Hours-T.Hours;if(HourTemp0)

11、/T class hour was bigger tha n THIS classFlagLessDay=1; /to cut 1 day form date class/小时相减为负数,将少于1天的标志置1HourTemp+=24;/ add 24 hours to previous day/ 力卩 24 小时到前一 天Min uteTemp=Mi nutes-T.Mi nutes;if(Mi nuteTempO)/same for minu tes/分钟相减为负Mi nuteTemp+=60;-HourTemp;Secon dTemp=Sec on ds-T.Sec on ds;if(Se

12、co ndTemp0)/same for sec on ds/秒数相减为负Secon dTemp+=60;-Mi nu teTemp;return ( cTime_t ( HourTemp,MinuteTemp,SecondTemp);/returnlocalclass*/4、修改综合类cTDmanage的结构,重新定义为日期类和时间类的派生类,并定 义构造函数、各种运算符重载函数,重载输入输出函数:#if ndef TDma nage_h#defi ne TDma nage_hclass cTDma nage:public cTime_t,public cDate_t 一 一public:c

13、TDma nage ():cTime_t(),cDate_t()构造函数 cTDma nage (int year,i nt mon th,i ntday,i nthour,i ntminu te,i ntsec on d):cDate_t(year,m on th,day),cTime_t(hour,mi nu te,sec on d)cTDma nage(inthour,i ntminu te,i ntsec on d):cTime_t(hour,mi nu te,sec ond) void operator = (const cTDma nage& M);/OK/opti on to p

14、ut all get and set fun cti on of date and time classvoid prin t();/operator:bool operator (const cTDma nage& M)con st;bool operator (const cTDma nage& M)con st;bool operator = (const cTDma nage& M)con st;bool operator = (const cTDma nage& M)con st;bool operator != (const cTDma nage& M)con st;const c

15、TDma nage operator + (const cTDma nage& M);int operator - (const cTDma nage& M);const cTDma nage operator + (int MINUTE);const cTDma nage operator - (int MINUTE);/us ing Date and Time + operator:void operator + () AddDay() ; AddSeco nd() ; void AddDay();void AddSeco nd();void Chan geDateFormat() cDa

16、te_t:Cha ngeFormat(); void Chan geTimeFormat() cTime_t:Cha ngeFormat(); frie nd ostream& operator (istream &in, cTDma nage &M);#en dif /TDma nage.h End5、修改main函数结构,采用菜单选项的方式,逐个测试三个类中定义的高中 函数和运算符:void manu select(cTime_t T,cDate_t D,cTDma nage L) 一 一r *、e ndl;coute ndl;cout1.2.日期类操作nvv3.综合类操作nvv4.建 议

17、 菜 单时间类操作n select1;switch(select1)case 1:coutnn时间类操作n;cout1.时 间加 分钟nvv2.时间减分钟nvv3.时间减 时间nvv4.两时间比较n select2;timeselect(select2,T,D, L);break;case 2:coutn日期类操作n;cout select3;dateselect(select3,T,D, L);break;case 3:coutn综合类操作n;cout1.时间日期加分钟n2.时间日期减分钟n3.时间日期 减时间日期(结果为分钟)n4.两时间日期比较n select4;man ageselec

18、t(select4,T,D,L);break;case 4:break;/采用的菜单选项如下:1、时间类操作2、日期类操作3、综合类操作时间类操作的子菜单1、时间加分钟2、时间减分钟3、时间减时间4、两时间比较日期类操作的子菜单1、日期加天数2、日期减天数3、日期减日期4、两日期比较综合类操作的子菜单1、日期时间加分钟2、日期时间减分钟3、日期时间减去日期时间(结果为分钟)4、日期时间比较6程序在各个类中均重载了 + , - , +, - -, = , = , =60)/more tha n 1 minuteSecon dTemp-=60;Min uteTemp=GetMin ()+M.Get

19、Min ()+1;/so add to mi nuteelseMin uteTemp=GetMin ()+M.GetMin ();if(Mi nuteTemp=60)/more than 1 hourMi nuteTemp-=60;HourTemp=GetHour ()+M.GetHour ()+1;/add to hourelseHourTemp=GetHour ()+M.GetHour ();if(HourTemp=24)FlagMoreDay=1; /to add day to date classHourTemp-=24;int YearTemp,M on thTemp,DayTemp

20、,NewDays;YearTemp=GetYear ()+M.GetYear ();NewDays=GetDayOfYear(GetYear (),GetM on th(),GetDayOfMo nth () +GetDayOfYear(M.GetYear (),M.GetMo nth (),M.GetDayOfMo nth ();if(NewDays (IsLeapYear(YearTemp)? 366 : 365) /if the sumis more than in 1 yearNewDays=NewDays - (IsLe apYear(YearTemp)? 366 : 365);/c

21、ut 1year daysYearTemp+=1;/a nd add a yearMon thTemp=RetriveM on th(NewDays,YearTemp);/same with mon thDayTemp=RetriveDay(NewDays,YearTemp); /retrive the curre nt day inthe specific monthDayTemp+=(cTime_t:FlagMoreDay);DayTemp=RetriveDay(NewDays,YearTemp); /retrive the curre nt day inthe specific mont

22、hreturncTDma nage(YearTemp,M on thTemp,DayTemp,HourTemp, Minu teTemp,Sec on dTemp)Jint cTDma nage:operator - (const cTDma nage& M)/重载-运算符(比较两个综合类时间对象,结果得到相差的分钟数)int NewDays,Temp1,Temp2;Temp仁GetDayOfYear(GetYear (),GetMo nth (),GetDayOfMo nth ();Temp2=GetDayOfYear(M.GetYear (),M.GetMonth (),M.GetDayO

23、fMonth (); if(GetYear ()=M.GetYear ()NewDays=max(Temp1,Temp2)-min( Temp1,Temp2);elsefor(intTemp3=min(GetYear (),M.GetYear ();Temp3M.GetYear () NewDays=NewDays+Temp1-Temp2;elseNewDays=-(NewDays+Temp2-Temp1);int n ewhour, newm inu te;n ewhour=GetHour ()-M.GetHour ();newmi nute=GetMin ()-M.GetMin ();n

24、ewm inute=n ewm inu te+60* newhour+24*60*NewDays;return n ewmi nute;void cTDma nage:operator = (const cTDma nage & M)重载运算符=SetSec(M.GetSec();SetMi n(M.GetMin ();SetHour (M.GetHour ();SetDay(M.GetDayOfMo nth ();SetMo n(M.GetMo nth ();SetYear(M.GetYear ();bool cTDma nage:operator (const cTDma nage & M

25、)c onst/重载 运算符if (YearvM.GetYear () return true;if(Year=M.GetYear ()if(GetMo nth ()M.GetMo nth ()return true;if(GetMo nth ()=M.GetMo nth ()if(GetDayOfMo nth ()M.GetDayOfMo nth () return true; if(GetDayOfMo nth ()=M.GetDayOfMo nth () if (GetHour ()M.GetHour ()return true;if(GetHour ()=M.GetHour ()if(

26、GetMin ()M.GetMin ()return true;if(GetMin ()=M.GetMin () return (GetSec()M.GetSec();return false;return false;return false;return false;return false;bool cTDma nage:operator = (const cTDma nage & M)const /重载 =运算符if (YearvM.GetYear () return true;if(Year=M.GetYear ()if(GetMo nth ()M.GetMo nth ()retur

27、n true;if(GetMo nth ()=M.GetMo nth ()if(GetDayOfMo nth ()M.GetDayOfMo nth () return true; if(GetDayOfMo nth ()=M.GetDayOfMo nth () return true;if (GetHour ()M.GetHour () if(GetHour ()=M.GetHour () if(GetMin () (const cTDma nage & M)const /重载运算符if (YearM.GetYear () return true;if(Year=M.GetYear ()if(

28、GetMo nth ()M.GetMo nth ()return true;if(GetMo nth ()=M.GetMo nth ()if(GetDayOfMo nth ()M.GetDayOfMo nth () return true; if(GetDayOfMo nth ()=M.GetDayOfMo nth () if (GetHour ()M.GetHour ()return true;if(GetHour ()=M.GetHour ()if(GetMin ()M.GetMin ()return true;if(GetMin ()=M.GetMin ()return (GetSec(

29、)M.GetSec();return false;return false;return false;return false;return false;bool cTDma nage:operator = (const cTDma nage & M)c onst/重载 =运算符if (YearM.GetYear () return true;if(Year=M.GetYear ()if(GetMo nth ()M.GetMo nth ()return true;if(GetMo nth ()=M.GetMo nth ()if(GetDayOfMo nth ()M.GetDayOfMo nth

30、 () return true; if(GetDayOfMo nth ()=M.GetDayOfMo nth ()if (GetHour ()M.GetHour ()return true;if(GetHour ()=M.GetHour ()if(GetMin ()M.GetMin ()return true;if(GetMin ()=M.GetMin () return (GetSec()=M.GetSec();return false;return false;return false;return false;return false;bool cTDma nage:operator =

31、 (const cTDma nage & M)c onst/重载=运算符return (GetSec()=M.GetSec()&(GetMin()=M.GetMin ()&(GetHour()=M.GetHour ()&(GetDayOfMonth ()=M.GetDayOfMonth ()&(GetMonth ()=M.GetMonth ()&(GetYear ()=M.GetYear ();bool cTDma nage:operator != (const cTDma nage & M)constreturn (GetSec()=M.GetSec()|(GetMin()=M.GetMin

32、 ()|(GetHour()=M.GetHour ()|(GetDayOfMonth ()=M.GetDayOfMonth ()|(GetMonth()=M.GetMonth()|(GetYear ()=M.GetYear (); 宀const cTDma nage cTDma nage:operator + (int MINUTE) /综合类和分钟加运算符的重载int HourTemp,Mi nu teTemp,Sec on dTemp;Sec on dTemp=GetSec();HourTemp=GetHour();Minu teTemp=GetMin ()+MINUTE;while(Mi

33、 nuteTemp=60)Mi nuteTemp-=60;HourTemp+=1;while(HourTemp=24)HourTemp-=24;FlagMoreDay+=1;int YearTemp,DayTemp,M on thTemp,NewDays;YearTemp=GetYear ();Mo nthTemp=GetMo nth ();NewDays=GetDayOfYear(GetYear(),GetM on th(),GetDayOfMo nth()+FlagMoreDay;while(NewDays(lsLeapYear(YearTemp)? 366 : 365)NewDays=N

34、ewDays-(lsLeapYear(YearTemp)? 366 : 365);YearTemp+=1;DayTemp=RetriveDay(NewDays,YearTemp);Mo nthTemp=RetriveMo nth(NewDays,YearTemp);FlagMoreDay=0;returncTDma nage(YearTemp,M on thTemp,DayTemp,HourTemp, Minu teTemp,Sec on dTemp)Jconst cTDma nage cTDma nage:operator - (int MINUTE)/综合类和分钟减运算符的重载int Ho

35、urTemp,Mi nu teTemp,Sec on dTemp; Sec on dTemp=GetSec();HourTemp=GetHour();Min uteTemp=GetMi n()-MINUTE;while(Mi nu teTempvO)Mi nuteTemp+=60;HourTemp-=1;while(HourTemp0)HourTemp+=24;FlagLessDay+=1;int YearTemp,DayTemp,M on thTemp,NewDays;int Temp=GetDayOfYear(GetYear (),GetMo nth (),GetDayOfMo nth (

36、); YearTemp=GetYear ();NewDays=Temp-FlagLessDay;while(NewDays0)NewDays=NewDays+(lsLe apYear(YearTemp)? 366 : 365);DayTemp=RetriveDay(NewDays,YearTemp)+1;Mo nthTemp=RetriveMo nth(NewDays,YearTemp);FlagLessDay=0;returncTDma nage(YearTemp,M on thTemp,DayTemp,HourTemp, Minu teTemp,Sec on dTemp)Jvoid cTD

37、ma nage:AddDay()if(GetDayOfMo nth()=GetDaysl nMon th(GetMo nth(),GetYear()&(GetMo nth()=12)/ if reached the end of the yearSetDay(1);SetMo n(1);SetYear(GetYear()+1);else if( (GetDayOfMonth()=GetDayslnMonth(GetMonth(),GetYear()/ if reached the end of the month (but not year)SetDay(1);SetMo n(GetMo nt

38、h()+1);elseSetDay(GetDayOfMo nth()+1); /add 1 dayvoid cTDma nage:AddSeco nd()if ( (GetSec()=59) & (GetMin()=59)& (GetHour()=23) ) / end ofa dayFlagMoreDay=1;/to add day to day classSetSec(0);SetMi n(0);SetHour(O);else if( (GetSec()=59) & (GetMi n()=59) )/e nd of hourSetSec(0);SetMi n(0);SetHour(GetH

39、our()+1);else if( GetSec()=59 )/end of min uteSetSec(O);SetMi n(GetMi n()+1); elseSetSec(GetSec()+1);三、课程设计中遇到的困难及心得:这个课题的程序比较长,需要很多的时间,这对于平时学业较繁重,社团 活动也不少的我来说,更合理的安排时间,更有效利用本就有限的课余时间, 成了个不小的挑战。于是,把程序分成很多块,零散的思考研究,就成为我常 用的方式。在这个过程中,我发现,程序中有不少相同结构的内容,而各运算 符重载函数也存有很多相似之处,加上原程序中类中的成员函数的功能很全面, 这些都让我省下不少

40、时间和精力。通过C+课程设计这个比较漫长的过程,我不错的掌握了运算符的重载,学 习了输入输出流函数的重载,同时对类间函数调用的关系以及如何实现类间复 杂的相关调用也有了更深的理解。通过阅读本课题的程序,我掌握了一些阅读 程序的技巧,养成了认真阅读程序的好习惯,并能从较长的程序中找到程序的 核心实现部分。在不断思考和调试的过程中,我的意志也得到一定的磨练,而 且在时间的合理安排和有效利用方面也有的很大的提高。四、关键原代码注释:cDate_t();缺省构造函数,初始化为当前时间cDate_t(const cDate_t & D);/拷 贝的构造函数 cDate_t(int year,int mo

41、nth,int day);/构造函数cDate_t()析构函数const cDate_t & operator = (const cDate_t & D);/重 载=运算符inline void SetDay (int day) Day=day;/设置日期inline void SetMon (int mo n) Month=mon;/设置月份inline void SetYear (int year) Year=year;/设置年份void print();void print1();inline int GetDayOfMonth () const return Day;/ 返回月份中的日

42、期inline int GetMonth () const return Month;/ 返回月份inline int GetYear () const return Year;/返 回年份int GetDayOfYear(int year,int month,int day);/ 返回从元旦到所给日期的总 天数int GetDayslnMonth(int month,int year);/ 返回所给日期那月的总天数int GetDayOfWeek();/ 返回星期bool IsLeapYear(int year);/判断是否闰年char* GetDayOfWeekName();/ 返回星期的

43、英文char* GetNameOfMonth();/ 返回月份的英文int RetriveDay(int Days,int year);/将距year年元旦的总天数转换为具体月 份中的天数int RetriveMonth(int Days,int year);/将距year年元旦的总天数转换为具体 月份bool operator (const cDate_t & D)const;重载 运算符bool operator = (const cDate_t & D)const;重 载 (const cDate_t & D)const;重载 运算符bool operator = (const cDat

44、e_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 + (int Days) ;/重载+运算符(一个日期对象加上天数 得到一个新的日期)const c

45、Date_t operator - (int Days) ;/重载-运算符(一个日期对象减去天数得 到一个新的日期)int getformat2()return format;/ 返回格式void operator + ();/重 载 + 运算符void operator - ();/重载-运算符static void cDate_t:ChangeFormat() /implementation of static function must be in header/设置日期格式switch(format)case 1: format=2; break;case 2: format=3; b

46、reak;case 3:format=1;friend ostream& operator (istream &in, cDate_t &D) ;/ 重载输入流void SetError() Error=false; 设置出错标志friend class cTDmanage;/说明综合类是日期类的友元类 static int FlagMoreDay;/时间是否超过一天标志static int FlagLessDay;时间是否不到一天标志static int format;/格式 cTime_t(const cTime_t &T);/ 拷贝构造函数cTime_t(inthour,intmin=0

47、,intsec=O):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 mi n) Minutes=min;/ 设置分数inline void SetHour (int hour) Hours=hour; 设置小时数 void print() const;/输出 void print

48、1() 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;/

49、 重设时间是否超过一天 void resetFlagLessDay()FlagLessDay=0;重设时间是否不到一天 bool operator (const cTime_t & T)const;重载 运算符 bool operator = (const cTime_t & T)const;重载 (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 operator + (int NewMinutes);/重 载 + 运算符(一个时间对象加 上分钟得到一个新的时间)const cTime_t operator - (int NewMinutes)

温馨提示

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

评论

0/150

提交评论