VisualC++课程设计报告时间和日期电子工程与光电技术学院通信工程(1)班Xxx0904220xxx2010年4月一、程序功能简介:定义了日期类、时间类和日期时间综合类,重载了+,-,++,--,=,=,=,==,!=等运算符,可以设置时间、日期,比较时间和日期的大小,进行时间、日期对象的运算,并输出多种格式的结果。二、课程设计说明:1、原程序日期类中输出星期几的函数有误,不能输出星期五,现修改如下:intcDate_t::GetDayOfWeek()//startingpoint:1/1/1900wasMonday(Saturday==0,Sunday==1,...)//1/1/1900是星期一,根据这个日期推断当前日期是星期几{intweekday=2;//in1/1/1900//从1/1/1900开始计算for(intTheYear=1900;TheYearYear;++TheYear)//looptoknowthedayin1/1/thisyear{if(IsLeapYear(TheYear))weekday=(weekday+366)%7;elseweekday=(weekday+365)%7;}//untilherewefoundthecorrectdayin1/1/thisyearfor(intTheMonth=1;TheMonthMonth;++TheMonth)//年份计算完后,计算月份天数weekday=(weekday+GetDaysInMonth(TheMonth,Year))%7;//untilherewefoundthecorrectdayin1/thismonthif((weekday+Day)%7==0)return6;elsereturn((weekday+Day)%7-1);//正确显示星期几//return(weekday+Day)%7-1;(修改前的,无法正确显示星期五)//thecorrectdayinTHISday...}2、根据要求在日期类中修改日期对象减去日期对象的重载运算符-,使得结果不是另一个日期,而是天数:intcDate_t::operator-(cDate_t&D)//重载-运算符//修改为日期对象减去日期对象,结果不是另一个日期对象,而是天数{intTemp1,Temp2,NewDays=0;Temp1=GetDayOfYear(Year,Month,Day);Temp2=D.GetDayOfYear(D.Year,D.Month,D.Day);if(Year==D.Year){NewDays=max(Temp1,Temp2)-min(Temp1,Temp2);}else{for(intTemp3=min(Year,D.Year);Temp3max(Year,D.Year);Temp3++){NewDays+=((IsLeapYear(Temp3))?366:365);}if(YearD.Year)NewDays=NewDays+Temp1-Temp2;elseNewDays=-(NewDays+Temp2-Temp1);}returnNewDays;}//------------------------------------/*constcDate_tcDate_t::operator-(constcDate_t&D)//operator-function(修改前的函数,结果为另一个日期对象){intYearTemp,MonthTemp,DayTemp,NewDays,Temp1,Temp2;//variablestostoretonewdateobjectif(Error||D.Error)//ifnolessthen1objecthaveorrordata{Error4();//printorrormessegereturn(cDate_t(-1,-1,-1));//returnerrorclass}YearTemp=Year-D.Year;//substractionofTHISandsecondyearif(YearTemp1900)//error{Error1();//printerrormessegereturn(cDate_t(-1,-1,-1));//returnerrorclass}Temp1=GetDayOfYear(Year,Month,Day);//getdaysofyearofTHISobjectif(Temp10)//error{Error5();//printerrormessegereturn(cDate_t(-1,-1,-1));//returnerrorclass}Temp2=GetDayOfYear(D.Year,D.Month,D.Day);//getdaysofyearofsecondobjectif(Temp20)//error{Error5();//printerrormessegereturn(cDate_t(-1,-1,-1));//returnerrorclass}NewDays=Temp1-Temp2;//ifthedataokgettheirsubstractionif(NewDays0)//thesubstractionislessthenzero{NewDays=NewDays+((IsLeapYear(YearTemp))?366:365);//setdaysforpreviousyearYearTemp-=1;//sub1year}DayTemp=RetriveDay(NewDays,YearTemp);//retrivethecurrentdayinthespecificmonthMonthTemp=RetriveMonth(NewDays,YearTemp);//samewithmonthreturn(cDate_t(YearTemp,MonthTemp,DayTemp));//returnlocalclass}*/3、根据要求在时间类中修改时间对象减去时间对象的重载运算符-,使得结果不是另一个时间,而是分钟数:intcTime_t::operator-(cTime_t&T)//重载-运算符(结果不是另一个时间对象,而是分钟数){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;//define3tempvariablestogettimedata//定义三个时间变量HourTemp=Hours-T.Hours;if(HourTemp0)//TclasshourwasbiggerthanTHISclass{FlagLessDay=1;//tocut1dayformdateclass//小时相减为负数,将少于1天的标志置1HourTemp+=24;//add24hourstopreviousday//加24小时到前一天}MinuteTemp=Minutes-T.Minutes;if(MinuteTemp0)//sameforminutes//分钟相减为负{MinuteTemp+=60;--HourTemp;}SecondTemp=Seconds-T.Seconds;if(SecondTemp0)//sameforseconds//秒数相减为负{SecondTemp+=60;--MinuteTemp;}return(cTime_t(HourTemp,MinuteTemp,SecondTemp));//returnlocalclass}*/4、修改综合类cTDmanage的结构,重新定义为日期类和时间类的派生类,并定义构造函数、各种运算符重载函数,重载输入输出函数:#ifndefTDmanage_h#defineTDmanage_hclasscTDmanage:publiccTime_t,publiccDate_t{public:cTDmanage():cTime_t(),cDate_t()//构造函数{}cTDmanage(intyear,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);//OK//optiontoputallgetandsetfunctionofdateandtimeclassvoidprint();//operator:booloperator(constcTDmanage&M)const;booloperator=(constcTDmanage&M)const;booloperator(constcTDmanage&M)const;booloperator=(constcTDmanage&M)const;booloperator==(constcTDmanage&M)const;booloperator!=(constcTDmanage&M)const;constcTDmanageoperator+(constcTDmanage&M);intoperator-(constcTDmanage&M);constcTDmanageoperator+(intMINUTE);constcTDmanageoperator-(intMINUTE);//usingDateandTime++operator:voidoperator++(){AddDay();AddSecond();}voidAddDay();voidAddSecond();voidChangeDateFormat(){cDate_t::ChangeFormat();}voidChangeTimeFormat(){cTime_t::ChangeFormat();}friendostream&operator(ostream&out,cTDmanage&M);friendistream&operator(istream&in,cTDmanage&M);};#endif//TDmanage.hEnd5、修改main函数结构,采用菜单选项的方式,逐个测试三个类中定义的高中函数和运算符:voidmanuselect(cTime_tT,cDate_tD,cTDmanageL){cout\n***********************************建议菜单***********************************endl;coutendl;cout1.时间类操作\n2.日期类操作\n3.综合类操作\n4.退出\n;intselect1,select2=0,select3=0,select4=0;cout请选择:\n;cinselect1;switch(select1){case1:cout\n---------------------------------------------------\n时间类操作\n;cout1.时间加分钟\n2.时间减分钟\n3.时间减时间\n