1、时间和日期 报告xymVisual C+课程设计报告 时间和日期电子工程与光电技术学院通信工程(1)班Xxx0904220xxx2010年4月一、 程序功能简介:定义了日期类、时间类和日期时间综合类,重载了+,-,+,- -,= ,= ,= ,= =,!=等运算符,可以设置时间、日期,比较时间和日期的大小,进行时间、日期对象的运算,并输出多种格式的结果。二、 课程设计说明:1、 原程序日期类中输出星期几的函数有误,不能输出星期五,现修改如下:int cDate_t:GetDayOfWeek() /starting point : 1/1/1900 was Monday (Saturday=0,
2、Sunday=1,.)/1/1/1900是星期一,根据这个日期推断当前日期是星期几 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 year if(IsLeapYear(TheYear) weekday=(weekday+366)%7; else weekday=(weekday+365)%7; /until here we found the correct day in 1/1/this year f
3、or(int TheMonth=1;TheMonthMonth;+TheMonth)/年份计算完后,计算月份天数 weekday=(weekday+GetDaysInMonth(TheMonth,Year)%7; /until here we found the correct day in 1/this month if(weekday+Day)%7=0) return 6; else return(weekday+Day)%7-1);/正确显示星期几 /return (weekday+Day)%7-1;(修改前的,无法正确显示星期五) /the correct day in THIS da
4、y.2、根据要求在日期类中修改日期对象减去日期对象的重载运算符-,使得结果不是另一个日期,而是天数:int cDate_t:operator - (cDate_t &D)/重载-运算符/修改为日期对象减去日期对象,结果不是另一个日期对象,而是天数 int Temp1,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(i
5、nt Temp3=min(Year,D.Year);Temp3D.Year) NewDays=NewDays+Temp1-Temp2; else NewDays=-(NewDays+Temp2-Temp1); return NewDays;/-/*const cDate_t cDate_t:operator - (const cDate_t &D) /operator - function(修改前的函数,结果为另一个日期对象) int YearTemp,MonthTemp,DayTemp,NewDays,Temp1,Temp2; /variables to store to new date
6、object if(Error | D.Error)/ if no less then 1 object have orror data Error4();/ print orror messege return (cDate_t (-1,-1,-1) ); /return error class YearTemp=Year-D.Year;/substraction of THIS and second year if(YearTemp1900) / error Error1();/print error messege return (cDate_t (-1,-1,-1) ); /retur
7、n error class Temp1=GetDayOfYear(Year,Month,Day); / get days of year of THIS object if(Temp10) /error Error5();/print error messege return (cDate_t (-1,-1,-1) );/return error class Temp2=GetDayOfYear(D.Year,D.Month,D.Day);/ get days of year of second object if(Temp20)/error Error5();/print error mes
8、sege return (cDate_t (-1,-1,-1) );/return error class NewDays= Temp1 - Temp2; /if the data ok get their substraction if(NewDays0) / the substraction is less then zero NewDays = NewDays + (IsLeapYear(YearTemp)? 366 : 365); / set days for previous year YearTemp-=1; /sub 1 year DayTemp=RetriveDay(NewDa
9、ys,YearTemp); /retrive the current day in the specific month MonthTemp=RetriveMonth(NewDays,YearTemp);/same with month return (cDate_t (YearTemp,MonthTemp,DayTemp) );/return local class */3、根据要求在时间类中修改时间对象减去时间对象的重载运算符-,使得结果不是另一个时间,而是分钟数:int cTime_t:operator - (cTime_t &T)/重载-运算符(结果不是另一个时间对象,而是分钟数) i
10、nt 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 - function/重载-运算符/修改前的,结果为时间对象 int HourTemp,MinuteTemp,SecondTemp;/define 3 temp variables to get time data/定义三个时间变量 HourTemp =
11、 Hours-T.Hours; if(HourTemp0)/T class hour was bigger than THIS class FlagLessDay=1; /to cut 1 day form date class/小时相减为负数,将少于1天的标志置1 HourTemp+=24;/ add 24 hours to previous day/加24小时到前一天 MinuteTemp=Minutes-T.Minutes; if(MinuteTemp0)/same for minutes/分钟相减为负 MinuteTemp+=60; -HourTemp; SecondTemp=Seco
12、nds-T.Seconds; if(SecondTemp0)/same for seconds/秒数相减为负 SecondTemp+=60; -MinuteTemp; return ( cTime_t ( HourTemp,MinuteTemp,SecondTemp) );/return local class */4、修改综合类cTDmanage的结构,重新定义为日期类和时间类的派生类,并定义构造函数、各种运算符重载函数,重载输入输出函数:#ifndef TDmanage_h#define TDmanage_hclass cTDmanage:public cTime_t,public cDa
13、te_tpublic: cTDmanage ():cTime_t(),cDate_t()/构造函数 cTDmanage (int year,int month,int day,int hour,int minute,int second):cDate_t(year,month,day),cTime_t(hour,minute,second) cTDmanage (int hour,int minute,int second):cTime_t(hour,minute,second) void operator = (const cTDmanage& M);/OK /option to put a
14、ll get and set function of date and time class void print(); /operator: bool operator (const cTDmanage& M)const; bool operator (const cTDmanage& M)const; bool operator = (const cTDmanage& M)const; bool operator = (const cTDmanage& M)const; bool operator != (const cTDmanage& M)const; const cTDmanage
15、operator + (const cTDmanage& M) ; int operator - (const cTDmanage& M) ; const cTDmanage operator + (int MINUTE) ; const cTDmanage operator - (int MINUTE) ; /using Date and Time + operator: void operator + () AddDay() ; AddSecond() ; void AddDay(); void AddSecond(); void ChangeDateFormat() cDate_t:Ch
16、angeFormat(); void ChangeTimeFormat() cTime_t:ChangeFormat(); friend ostream& operator (istream &in, cTDmanage &M) ;#endif/TDmanage.h End5、修改main函数结构,采用菜单选项的方式,逐个测试三个类中定义的高中函数和运算符:void manuselect(cTime_t T,cDate_t D,cTDmanage L) coutn*建议菜单*endl; coutendl; cout 1.时间类操作n 2.日期类操作n 3.综合类操作n 4.退出n; int s
17、elect1,select2=0,select3=0,select4=0; coutselect1; switch(select1) case 1: coutn-n 时间类操作n; cout1.时间加分钟n2.时间减分钟n3.时间减时间n4.两时间比较n5.返回上一层n; coutselect2; timeselect(select2,T,D,L); break; case 2: cout-n日期类操作n; cout1.日期加天数n2.日期减天数n3.日期减日期n4.两日期比较n5.返回上一层n; coutselect3; dateselect(select3,T,D,L); break; c
18、ase 3: cout-n综合类操作n; cout1.时间日期加分钟n2.时间日期减分钟n3.时间日期减时间日期(结果为分钟)n4.两时间日期比较n5.返回上一层n; coutselect4; manageselect(select4,T,D,L); break; case 4: break; /采用的菜单选项如下:1、 时间类操作2、 日期类操作3、 综合类操作时间类操作的子菜单1、 时间加分钟2、 时间减分钟3、 时间减时间4、 两时间比较日期类操作的子菜单1、 日期加天数2、 日期减天数3、 日期减日期4、 两日期比较综合类操作的子菜单1、 日期时间加分钟2、 日期时间减分钟3、 日期时
19、间减去日期时间(结果为分钟)4、 日期时间比较6、程序在各个类中均重载了+ ,- ,+, - - ,= ,= ,=60)/more than 1 minute SecondTemp-=60; MinuteTemp=GetMin ()+M.GetMin ()+1;/so add to minute else MinuteTemp=GetMin ()+M.GetMin (); if(MinuteTemp=60)/more than 1 hour MinuteTemp-=60; HourTemp=GetHour ()+M.GetHour ()+1;/add to hour else HourTemp
20、=GetHour ()+M.GetHour (); if(HourTemp=24) FlagMoreDay=1; /to add day to date class HourTemp-=24; int YearTemp,MonthTemp,DayTemp,NewDays; YearTemp=GetYear ()+M.GetYear (); NewDays=GetDayOfYear(GetYear (),GetMonth (),GetDayOfMonth () + GetDayOfYear(M.GetYear (),M.GetMonth (),M.GetDayOfMonth (); if(New
21、Days (IsLeapYear(YearTemp)? 366 : 365) /if the sum is more than in 1 year NewDays=NewDays - (IsLeapYear(YearTemp)? 366 : 365);/cut 1 year days YearTemp+=1;/and add a year MonthTemp=RetriveMonth(NewDays,YearTemp);/same with month DayTemp=RetriveDay(NewDays,YearTemp); /retrive the current day in the s
22、pecific month DayTemp+=(cTime_t:FlagMoreDay); DayTemp=RetriveDay(NewDays,YearTemp); /retrive the current day in the specific month return cTDmanage(YearTemp,MonthTemp,DayTemp,HourTemp,MinuteTemp,SecondTemp);int cTDmanage:operator - (const cTDmanage& M)/重载-运算符(比较两个综合类时间对象,结果得到相差的分钟数) int NewDays,Temp
23、1,Temp2; Temp1=GetDayOfYear(GetYear (),GetMonth (),GetDayOfMonth (); Temp2=GetDayOfYear(M.GetYear (),M.GetMonth (),M.GetDayOfMonth (); if(GetYear ()=M.GetYear () NewDays=max(Temp1,Temp2)-min(Temp1,Temp2); else for(int Temp3=min(GetYear (),M.GetYear ();Temp3M.GetYear () NewDays=NewDays+Temp1-Temp2; e
24、lse NewDays=-(NewDays+Temp2-Temp1); int newhour,newminute; newhour=GetHour ()-M.GetHour (); newminute=GetMin ()-M.GetMin (); newminute=newminute+60*newhour+24*60*NewDays; return newminute;void cTDmanage:operator = (const cTDmanage& M)/重载运算符= SetSec(M.GetSec(); SetMin(M.GetMin (); SetHour (M.GetHour
25、(); SetDay(M.GetDayOfMonth (); SetMon(M.GetMonth (); SetYear(M.GetYear ();bool cTDmanage:operator (const cTDmanage& M)const/重载运算符 if (YearM.GetYear () return true; if(Year=M.GetYear () if(GetMonth ()M.GetMonth () return true; if(GetMonth ()=M.GetMonth () if(GetDayOfMonth ()M.GetDayOfMonth () return
26、true; if(GetDayOfMonth ()=M.GetDayOfMonth () 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 cTDmanage:operator = (co
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1