《时间和日期-报告xym.docx》由会员分享,可在线阅读,更多相关《时间和日期-报告xym.docx(47页珍藏版)》请在第一文库网上搜索。
1、兴2成+锦程极计想告时间和日期电子工程与光电找术学院通信工程(1)班Xxx0904220xxx2010年4月一、程序功能简介:定义了日期类、时间类和日期时间综合类,重载了+,+,=,=,=,=,!=等运算符,可以设置时间、日期,比较时间和日期的大小,进行时间、日期对象的运算,并输出多种格式的结果。二、课程设计说明:1、原程序日期类中输出星期几的函数有误,不能输出星期五,现修改如下:intcDate_t:GetDayOfWeekO/startingpoint:1/1/1900wasMonday(Saturday=O,Sunday=I,.)1/1/1900是星期一,根据这个日期推断当前日期是星期几
2、intweekday=2;/in1/1/1900从1/1/1900开始计算for(intTheYear=1900;TheYearYear;+TheYear)/1ooptoknowthedayin1/1/thisyear(if(Is1eapYear(TheYear)weekday=(weekday+366)%7;e1seweekday=(weekday+365)%7;/unti1herewefoundthecorrectdayin1/1/thisyearfor(intTheMonth=I;TheMonthMonth;+TheMonth)/年份计算完后,计算月份天数weekday=(weekday
3、+GetDaysInMonth(TheMonth,Year)%7;/unti1herewefoundthecorrectdayin1/thismonthif(weekday+Day)%7=0)return6;e1sereturn(weekday+Day)%7T);正确显示星期几/return(Weekday+Day)%7T;(修改前的,无法正确显示星期五)thecorrectdayinTHISday.2、根据要求在日期类中修改日期对象减去日期对象的重载运算符-,使得结果不是另一个日期,而是天数:intcDate_t:operator-(cDate_t&D)重载-运算符修改为日期对象减去日期对象
4、,结果不是另一个日期对象,而是天数(intTempi,Temp2,NewDays=O;Temp1=GetDayOfYear(Year,Month,Day);Temp2=D.GetDayOfYear(D.Year,D.Month,D.Day);if(Year=D.Year)(NewDays=max(Tempi,Temp2)-min(Tempi,Temp2);e1sefor(intTemp3=min(Year,D.Year);Temp3D.Year)NewDays=NewDays+!emp1-Temp2;e1seNewDays=-(NewDays+Temp2-Temp1);returnNewDay
5、s;/constcDate_tcDate_t:operator-(constcDate_t&D)/operator-function(修改前的函数,结果为另一个日期对象)intYearTemp,MonthTemp,DayTemp,NewDays,Tempi,Temp2;/variab1estostoretonewdateobjectif(ErrorD.Error)/ifno1essthen1objecthaveorrordataError4();/printorrormessegereturn(cDate_t(-1,-1,-1);/returnerrorc1assYearTemp=Year-D
6、.Year;/substractionofTHISandsecondyearif(YearTemp1900)/error(Error1O;/printerrormessegereturn(cDate_t(-1,-1,-1);/returnerrorc1ass)Temp1=GetDayOfYear(Year,Month,Day);/getdaysofyearofTHISobjectif(Temp1O)/error(Error5();/printerrormessegereturn(cDate_t(-1,-1,-1);/returnerrorc1assTemp2=GetDay0fYear(D.Ye
7、ar,D.Month,D.Day);/getdaysofyearofsecondobjectif(Temp20)/error(Error5();/printerrormessegereturn(cDate_t(-1,-1,-1);/returnerrorc1ass)NewDays=Tempi-Temp2;/ifthedataokgettheirsubstractionif(NewDays0)/thesubstractionis1essthenzero(NewDays=NewDays+(Is1eapYear(YearTemp)?366:365);/setdaysforpreviousyearYe
8、arTemp-=I;/sub1year)DayTemp=RetriveDay(NewDays,YearTemp);/retrivethecurrentdayinthespecificmonthMonthTemp=RetriveMonth(NewDays,YearTemp);/samewithmonthreturn(cDate_t(YearTemp,MonthTemp,DayTemp);/return1oca1c1ass*/3、根据要求在时间类中修改时间对象减去时间对象的重载运算符-,使得结果不是另一个时间,而是分钟数:intcTime_t:operator-(cTime_t&T)重载-运算符(
9、结果不是另一个时间对象,而是分钟数)(intnewhour,newminute;newhour=Hours-T.Hours;newminute=Minutes-T.Minutes;returnnewhour*60+newminute;/constcTime_tcTime_t:operator-(constcTime_t&T)const/operaor-function重载-运算符修改前的,结果为时间对象(intHourTemp,MinuteTemp,SecondTemp;/define3tempvariab1estogettimedata定义三个时间变量HourTemp=Hours-T.Hou
10、rs;if(HourTemp0)/Tc1asshourwasbiggerthanTHISc1ass(F1ag1essDay=I;/tocut1dayformdateC1aSS小时相减为负数,将少于1天的标志置1HourTemp+=24;/add24hourstopreviousday加24小时到前一天MinuteTemp=Minutes-T.Minutes;if(MinuteTempO)/sameforIninUteS分钟相减为负(MinuteTemp+=60;HourTemp;SecondTemp=Seconds-T.Seconds;if(SecondTemp0)/sameforSeCond
11、S秒数相减为负SecondTemp+=60;一MinuteTemp;)return(cTime_t(HourTemp,MinuteTemp,SecondTemp);/return1oca1c1ass*/4、修改综合类CTDmanage的结构,重新定义为日期类和时间类的派生类,并定义构造函数、各种运算符重载函数,重载输入输出函数:#ifndefTDmanage_h#defineTDmanage_hc1asscTDmanage:pub1iecTime-t,pub1iccDate_t(pub1ic:cTDmanage():cTime_t(),cDate_t()构造函数(cTDmanage(intye
12、ar,intmonth,intday,inthour,intminute,intsecond):cDate_t(year,month,day),cTime_t(hour,minute,second)cTDmanage(inthour,intminute,intsecond):cTime_t(hour,minute,second)voidoperator=(constcTDmanage&M);/0K/optiontoputa11getandsetfunctionofdateandtimec1assvoidprint();/operator:boo1operator(constCTDmanagef
13、eM)const;boo1operator(constCTDmanageAM)const;boo1operator=(constCTDmanagefeM)const;boo1operator=(constCTDmanagefeM)const;boo1operator!=(constCTDmanagefeM)const;constcTDmanageoperator+(constcTDmanage&M);intoperator-(constcTDmanage&M);constcTDmanageoperator+(intMINUTE);constcTDmanageoperator-(intMINUT
14、E);/usingDateandTime+operator:voidoperator+()AddDay();AddSecond();voidAddDay();voidAddSecond();voidChangeDateFormatOcDate_t:ZChangeFormat();voidChangeTimeFormatOcTime_t:ZChangeFormat();friendostream&operator(ostream&out,cTDmanage&M);friendistreamfeoperator(istream&in,cTDmanage&M);;#endif/TDmanage.hEnd5、修改main函数结构,采用菜单选项的方式,逐个测试三个类中定义的高中函数和运算符:voidmanuse1ect(cTime_tT,cDate_tD,cTDmanage1)建议菜单*Mc*M*”end1;coutend1;cout1时间类操作n“2,日期类操作r*4.