时间和日期报告xym.docx

上传人:b****9 文档编号:23319548 上传时间:2023-05-16 格式:DOCX 页数:30 大小:22.72KB
下载 相关 举报
时间和日期报告xym.docx_第1页
第1页 / 共30页
时间和日期报告xym.docx_第2页
第2页 / 共30页
时间和日期报告xym.docx_第3页
第3页 / 共30页
时间和日期报告xym.docx_第4页
第4页 / 共30页
时间和日期报告xym.docx_第5页
第5页 / 共30页
点击查看更多>>
下载资源
资源描述

时间和日期报告xym.docx

《时间和日期报告xym.docx》由会员分享,可在线阅读,更多相关《时间和日期报告xym.docx(30页珍藏版)》请在冰豆网上搜索。

时间和日期报告xym.docx

时间和日期报告xym

 

VisualC++课程设计报告

时间和日期

电子工程与光电技术学院

通信工程

(1)班

Xxx

0904220xxx

 

2010年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;TheYear

{

if(IsLeapYear(TheYear))

weekday=(weekday+366)%7;

else

weekday=(weekday+365)%7;

}

//untilherewefoundthecorrectdayin1/1/thisyear

for(intTheMonth=1;TheMonth

weekday=(weekday+GetDaysInMonth(TheMonth,Year))%7;

//untilherewefoundthecorrectdayin1/thismonth

if((weekday+Day)%7==0)

return6;

else

return((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);Temp3

{

NewDays+=((IsLeapYear(Temp3))?

366:

365);

}

if(Year>D.Year)

NewDays=NewDays+Temp1-Temp2;

else

NewDays=-(NewDays+Temp2-Temp1);

}

returnNewDays;

}

//------------------------------------

/*constcDate_tcDate_t:

:

operator-(constcDate_t&D)//operator-function(修改前的函数,结果为另一个日期对象)

{

intYearTemp,MonthTemp,DayTemp,NewDays,Temp1,Temp2;

//variablestostoretonewdateobject

if(Error||D.Error)//ifnolessthen1objecthaveorrordata

{

Error4();//printorrormessege

return(cDate_t(-1,-1,-1));//returnerrorclass

}

YearTemp=Year-D.Year;//substractionofTHISandsecondyear

if(YearTemp<1900)//error

{

Error1();//printerrormessege

return(cDate_t(-1,-1,-1));//returnerrorclass

}

Temp1=GetDayOfYear(Year,Month,Day);//getdaysofyearofTHISobject

if(Temp1<0)//error

{

Error5();//printerrormessege

return(cDate_t(-1,-1,-1));//returnerrorclass

}

Temp2=GetDayOfYear(D.Year,D.Month,D.Day);//getdaysofyearofsecondobject

if(Temp2<0)//error

{

Error5();//printerrormessege

return(cDate_t(-1,-1,-1));//returnerrorclass

}

NewDays=Temp1-Temp2;//ifthedataokgettheirsubstraction

if(NewDays<0)//thesubstractionislessthenzero

{

NewDays=NewDays+((IsLeapYear(YearTemp))?

366:

365);

//setdaysforpreviousyear

YearTemp-=1;//sub1year

}

DayTemp=RetriveDay(NewDays,YearTemp);//retrivethecurrentdayinthespecificmonth

MonthTemp=RetriveMonth(NewDays,YearTemp);//samewithmonth

return(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(HourTemp<0)//TclasshourwasbiggerthanTHISclass

{

FlagLessDay=1;//tocut1dayformdateclass//小时相减为负数,将少于1天的标志置1

HourTemp+=24;//add24hourstopreviousday//加24小时到前一天

}

MinuteTemp=Minutes-T.Minutes;

if(MinuteTemp<0)//sameforminutes//分钟相减为负

{

MinuteTemp+=60;

--HourTemp;

}

SecondTemp=Seconds-T.Seconds;

if(SecondTemp<0)//sameforseconds//秒数相减为负

{

SecondTemp+=60;

--MinuteTemp;

}

return(cTime_t(HourTemp,MinuteTemp,SecondTemp));//returnlocalclass

}*/

4、修改综合类cTDmanage的结构,重新定义为日期类和时间类的派生类,并定义构造函数、各种运算符重载函数,重载输入输出函数:

#ifndefTDmanage_h

#defineTDmanage_h

classcTDmanage:

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

//optiontoputallgetandsetfunctionofdateandtimeclass

voidprint();

//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.hEnd

 

5、修改main函数结构,采用菜单选项的方式,逐个测试三个类中定义的高中函数和运算符:

voidmanuselect(cTime_tT,cDate_tD,cTDmanageL)

{

cout<<"\n***********************************建议菜单***********************************"<

cout<

cout<<""<<"1.时间类操作\n"<<""<<"2.日期类操作\n"<<""

<<"3.综合类操作\n"<<""<<"4.退出\n";

intselect1,select2=0,select3=0,select4=0;

cout<<"请选择:

\n";

cin>>select1;

switch(select1)

{

case1:

cout<<"\n---------------------------------------------------\n"

<<"时间类操作\n";

cout<<"1.时间加分钟\n"<<"2.时间减分钟\n"<<"3.时间减时间\n"<<"4.两时间比较\n"<<"5.返回上一层\n";

cout<<"请选择:

";

cin>>select2;

timeselect(select2,T,D,L);

break;

case2:

cout<<"---------------------------------------------------\n"<<"日期类操作\n";

cout<<"1.日期加天数\n"<<"2.日期减天数\n"<<"3.日期减日期\n"<<"4.两日期比较\n"<<"5.返回上一层\n";

cout<<"请选择:

";

cin>>select3;

dateselect(select3,T,D,L);

break;

case3:

cout<<"---------------------------------------------------\n"<<"综合类操作\n";

cout<<"1.时间日期加分钟\n"<<"2.时间日期减分钟\n"<<"3.时间日期减时间日期(结果为分钟)\n"<<"4.两时间日期比较\n"<<"5.返回上一层\n";

cout<<"请选择:

";

cin>>select4;

manageselect(select4,T,D,L);

break;

case4:

break;

}

}

//采用的菜单选项如下:

1、时间类操作

2、日期类操作

3、综合类操作

时间类操作的子菜单

1、时间加分钟

2、时间减分钟

3、时间减时间

4、两时间比较

日期类操作的子菜单

1、日期加天数

2、日期减天数

3、日期减日期

4、两日期比较

综合类操作的子菜单

1、日期时间加分钟

2、日期时间减分钟

3、日期时间减去日期时间(结果为分钟)

4、日期时间比较

6、程序在各个类中均重载了+,-,++,--,=,>=,<=,==,!

=等运算符,使时间,日期对象可以进行运算和比较,下以综合类中各重载函数为例:

constcTDmanagecTDmanage:

:

operator+(constcTDmanage&M)//重载+运算符

{

intHourTemp,MinuteTemp,SecondTemp;//define3tempvariablestogettimedata

SecondTemp=GetSec()+M.GetSec();

if(SecondTemp>=60)//morethan1minute

{

SecondTemp-=60;

MinuteTemp=GetMin()+M.GetMin()+1;//soaddtominute

}

else

MinuteTemp=GetMin()+M.GetMin();

if(MinuteTemp>=60)//morethan1hour

{

MinuteTemp-=60;

HourTemp=GetHour()+M.GetHour()+1;//addtohour

}

else

HourTemp=GetHour()+M.GetHour();

if(HourTemp>=24)

{

FlagMoreDay=1;//toadddaytodateclass

HourTemp-=24;

}

intYearTemp,MonthTemp,DayTemp,NewDays;

YearTemp=GetYear()+M.GetYear();

NewDays=GetDayOfYear(GetYear(),GetMonth(),GetDayOfMonth())+GetDayOfYear(M.GetYear(),M.GetMonth(),M.GetDayOfMonth());

if(NewDays>((IsLeapYear(YearTemp))?

366:

365))//ifthesumismorethanin1year

{

NewDays=NewDays-((IsLeapYear(YearTemp))?

366:

365);//cut1yeardays

YearTemp+=1;//andaddayear

}

MonthTemp=RetriveMonth(NewDays,YearTemp);//samewithmonth

DayTemp=RetriveDay(NewDays,YearTemp);//retrivethecurrentdayinthespecificmonth

DayTemp+=(cTime_t:

:

FlagMoreDay);

DayTemp=RetriveDay(NewDays,YearTemp);//retrivethecurrentdayinthespecificmonth

returncTDmanage(YearTemp,MonthTemp,DayTemp,HourTemp,MinuteTemp,SecondTemp);

}

intcTDmanage:

:

operator-(constcTDmanage&M)//重载-运算符(比较两个综合类时间对象,结果得到相差的分钟数)

{

intNewDays,Temp1,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(intTemp3=min(GetYear(),M.GetYear());Temp3

{

NewDays+=((IsLeapYear(Temp3))?

366:

365);

}

if(GetYear()>M.GetYear())

NewDays=NewDays+Temp1-Temp2;

else

NewDays=-(NewDays+Temp2-Temp1);

}

intnewhour,newminute;

newhour=GetHour()-M.GetHour();

newminute=GetMin()-M.GetMin();

 

newminute=newminute+60*newhour+24*60*NewDays;

returnnewminute;

}

voidcTDmanage:

:

operator=(constcTDmanage&M)//重载运算符=

{

SetSec(M.GetSec());

SetMin(M.GetMin());

SetHour(M.GetHour());

SetDay(M.GetDayOfMonth());

SetMon(M.GetMonth());

SetYear(M.GetYear());

}

boolcTDmanage:

:

operator<(constcTDmanage&M)const//重载<运算符

{

if(Year

if(Year==M.GetYear())

{

if(GetMonth()

returntrue;

if(GetMonth()==M.GetMonth())

{

if(GetDayOfMonth()

if(GetDayOfMonth()==M.GetDayOfMonth())

{

if(GetHour()

if(GetHour()==M.GetHour())

{

if(GetMin()

returntrue;

if(GetMin()==M.GetMin())

{

return(GetSec()

}

returnfalse;

}

returnfalse;

}

returnfalse;

}

returnfalse;

}

returnfalse;

}

boolcTDmanage:

:

operator<=(co

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

当前位置:首页 > PPT模板 > 节日庆典

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1