c++实验日期类Date类.docx
- 文档编号:12064102
- 上传时间:2023-04-16
- 格式:DOCX
- 页数:17
- 大小:83.22KB
c++实验日期类Date类.docx
《c++实验日期类Date类.docx》由会员分享,可在线阅读,更多相关《c++实验日期类Date类.docx(17页珍藏版)》请在冰豆网上搜索。
c++实验日期类Date类
华南农业大学信息(软件)学院
综合性、设计性实验成绩单
开设时间:
2013~2014年第二学期
专业
工业工程
班级
12工工2班
学号
姓名
实验题目
日期类Date类
自我评价
能较好完成实验,理解和掌握运算符重载,了解静态变量和const成员函数。
实现系统时间的获取,通过对运算符的重载,实现对用户输入的时间进行加减处理、判断等;对静态变量const的理解,并利用静态变量const编写新的Time程序,实现多格式输出系统当前时间,实现时间相加减的功能。
教师评语
评价指标:
●题目内容独立完成情况优□良□中□差□
●对算法原理的理解程度优□良□中□差□
●程序设计水平优□良□中□差□
●程序运行情况优□良□中□差□
●实验报告结构清晰优□良□中□差□
●实验总结和分析详尽优□良□中□差□
成绩
教师签名
1实验内容及目的
目的:
理解和掌握运算符重载,了解静态变量和const成员函数。
实验内容:
(1)设计一个名为Date的日期类,完成基本的日期处理功能。
(2)扩充实例编程中的日期类,为Date类增加一个成员函数,可以判断一个日期是否是系统的当前日期。
从键盘输入你的生日,如果今天是你的生日则显示“HappyBirthday!
”,否则显示“还有XX天是你的生日”或“你的生日已经过去了XX天,明年的生日要再等YY天”。
(3)仿照Date类设计一个Time类,设计多个重载的构造函数,可以设置时间、进行时间的加减运算、按各种可能的格式输出时间。
2实验步骤
(1)
(2)小题:
1、设计一个Date的日期类,其私有成员有year,month,day三个日期以及voidIncDay();日期增加一天intDayCalc()const;距基准日期的天数staticconstintdays[];每月天数;共有成员包括日期设置,系统日期,判断闰年、判断月末,日期增加等函数,这是实现日期处理的基本函数。
2、重载日期增加符号+、+=、-;例:
constDate&operator+(intdays)至此Date类设置完成;
3、设计关于Date的CPP函数,首先包含time.h头文件(刚才设置的Date类)以及ctime文件(系统时间)。
4、设计构造函数,主要用于设置日期year,month,day;
5、编写系统时间函数、重载函数、判断闰年、判断月末,日期输出函数等
6、编写测试函数,设定下一个生日日期Birthday(2015,2,11);
输出当前系统时间;然后让用户输入一个日期,利用重载函数符号“-”判断输入日期时候为当前系统日期,若是则输出“输入日期是系统当前日期!
!
”否则输出“输入日期不是系统当前日期!
!
”;
再让用户输入自己的生日,当输入的生日日期与系统设定的一致则输出"今天是您的生日,祝您生日快乐!
!
",否则输出"还有XX天就是您的生日啦!
!
"
(3)小题:
1、设计一个Time类,私有成员包括hour、min两个时间成员;公有成员包
Time构造函数,settime时间设置函数,时间格式输出函数,重载函数等。
2、编写Time.cpp,编写函数调用settime函数设定时间,设定systemtime函数获取当前系统时间;设置时间输出格式(12小时制、24小时制);设置重载符号“+”、“-”实现时间的相加减;
3、编写测试函数,首先测试设置时间Timetesttime(11,00);然后对系统当前时间进行12小时制和24小时制输出;最后取两个测试时间time0,time1,利用重载函数,实现时间的相加减,并把结果输出来;
3源代码
#ifndefDATE_H
#defineDATE_H
#include
usingnamespacestd;
classDate{
intday,month,year;
voidIncDay();//日期增加一天
intDayCalc()const;//距基准日期的天数
staticconstintdays[];//每月天数
public:
Date(inty,intm,intd);
Date(intm,intd);
Date();
voidSystemDate();
voidSetDate(intyy,intmm,intdd);//日期设置
voidSetDate(intmm,intdd);//日期设置,年默认为系统年份
boolIsLeapYear(intyy)const;//是否闰年
boolIsEndofMonth()const;//是否月末
voidprint_ymd()const;
voidcin_ymd();
voidprint_mdy()const;
constDate&operator+(intdays);//日期增加任意天
constDate&operator+=(intdays);//日期增加任意天
intoperator-(constDate&ymd)const;
};
#endif//DATE_H
#include
#include
#include
usingnamespacestd;
#include"Date.h"
//静态成员初始化
constintDate:
:
days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//构造函数
Date:
:
Date(inty,intm,intd){SetDate(y,m,d);}
Date:
:
Date(intm,intd){SetDate(m,d);}
Date:
:
Date(){SystemDate();}
voidDate:
:
SystemDate()
{//取得系统日期
tm*gm;
time_tt=time(NULL);
gm=gmtime(&t);
year=1900+gm->tm_year;
month=gm->tm_mon+1;
day=gm->tm_mday;
}
voidDate:
:
SetDate(intyy,intmm,intdd)
{
month=(mm>=1&&mm<=12)?
mm:
1;
year=(yy>=1900&&yy<=2100)?
yy:
1900;
if(month==2&&IsLeapYear(year))
day=(dd>=1&&dd<=29)?
dd:
1;
elseday=(dd>=1&&dd<=days[month])?
dd:
1;
}
voidDate:
:
SetDate(intmm,intdd)
{
tm*gm;
time_tt=time(NULL);
gm=gmtime(&t);
month=(mm>=1&&mm<=12)?
mm:
1;
year=1900+gm->tm_year;
if(month==2&&IsLeapYear(year))
day=(dd>=1&&dd<=29)?
dd:
1;
elseday=(dd>=1&&dd<=days[month])?
dd:
1;
}
constDate&Date:
:
operator+(intdays)
{//重载+
for(inti=0;i IncDay(); return*this; } constDate&Date: : operator+=(intdays) {//重载+= for(inti=0;i IncDay(); return*this; } intDate: : operator-(constDate&ymd)const { intdays; days=DayCalc()-ymd.DayCalc(); returndays; } boolDate: : IsLeapYear(inty)const{ if(y%400==0||(y%100! =0&&y%4==0))returntrue; returnfalse; } boolDate: : IsEndofMonth()const { if(month==2&&IsLeapYear(year)) returnday==29; elsereturnday==days[month]; } voidDate: : IncDay() {//日期递增一天 if(IsEndofMonth()) if(month==12){ day=1;month=1;year++;} else{day=1;month++;} elseday++; } intDate: : DayCalc()const { intdd; intyy=year-1900; dd=yy*365; if(yy)dd+=(yy-1)/4; for(inti=1;i if(IsLeapYear(year)&&(month>2))dd++; dd+=day; returndd; } voidDate: : cin_ymd() { intyy,mm,dd; cin>>yy>>mm>>dd; this->year=yy;this->month=mm;this->day=dd; } voidDate: : print_ymd()const {cout< voidDate: : print_mdy()const { char*monthName[12]={"Jan.","Feb.","Mar.","Apr.","May","June","July","Aug.","Sep.","Oct.","Nov.","Dec."}; cout< } #include usingnamespacestd; #include"Date.h" intmain() { Datetoday,inputday,inputday1,Birthday(2015,2,11); cout<<"今天是: "; today.print_ymd(); cout<<"请输入一个日期: (例)2014319"< inputday.cin_ymd(); if(today-inputday==0){cout<<"输入日期是系统当前日期! ! "< else{cout<<"输入日期不是系统当前日期! ! "< cout<<"请输入您的生日: (例)2014319"< inputday1.cin_ymd(); if(Birthday-inputday1==0){cout<<"今天是您的生日,祝您生日快乐! ! ";} elseif(Birthday-inputday1<0){cout<<"您的生日已过"< ! "< else{cout<<"还有"< ! "< return0; } (3) #ifndefTime_H_INCLUDED #defineTime_H_INCLUDED classTime { inthour,min; public: Time(inth,intm); Time(intm); Time(); //设置时间 voidsettime(inth,intm); voidsettime(intm); voidsystemtime(); //判断日,时,分是否到最后 boolIsEndofday()const; boolIsEndofhour()const; boolIsEndofmin()const; //设置时间输出格式 voidprint_24()const; voidprint_12()const; //重载“+”“-”运算符 Timeoperator+(Time&)const; Timeoperator-(Time&)const; //取得时间 intgethour()const; intgetmin()const; intgetsec()const; }; #endif//Time_H_INCLUDED #include usingnamespacestd; #include #include"Time.h" Time: : Time(inth,intm) { settime(h,m); } Time: : Time(intm) { settime(m); } Time: : Time() { systemtime(); } voidTime: : settime(inth,intm) { if(h==24) hour=0; elsehour=h; min=m; } voidTime: : settime(intm) { tm*gm; time_tt=time(NULL); gm=gmtime(&t); hour=gm->tm_hour+8; min=m; } voidTime: : systemtime() { tm*gm; time_tt=time(NULL); gm=gmtime(&t); hour=gm->tm_hour+8; min=gm->tm_min; } voidTime: : print_24()const { cout< "< try { if(hour>24||hour<0) throwhour; } catch(inti) { cout<<"Error! thehouris"< hourmustberandin0to24"< } try { if(min>60||min<0) throwmin; } catch(inti) { cout<<"Error! theminuteis"< minutemustberandin0to60"< } } voidTime: : print_12()const { if(hour<=12) cout< "< elseif(hour>12&&hour<=24)cout< "< elsecout< "< .m"< try { if(hour>24||hour<0) throwhour; } catch(inti) { cout<<"Error! thehouris"< hourmustberandin0to24"< } try { if(min>60||min<0) throwmin; } catch(inti) { cout<<"Error! theminuteis"< minutemustberandin0to60"< } } TimeTime: : operator+(Time&t)const { Timesumtime; intsum=0; sum=(hour+t.hour)*60+(min+t.min); while(sum>1440) sum=sum-1440; sumtime.min=sum%60; sumtime.hour=(sum-sumtime.min)/60; returnsumtime; } TimeTime: : operator-(Time&t)const { Timediffstime; intdiff; diff=(hour-t.hour)*60+(min-t.min); if(diff>=0) { while(diff>1440) diff=diff-1440; diffstime.min=diff%60; diffstime.hour=(diff-diffstime.min)/60; } else { diff=1440+diff; diffstime.min=diff%60; diffstime.hour=(diff-diffstime.min)/60; } returndiffstime; } #include usingnamespacestd; #include #include"Time.h" intmain() { Timetesttime(11,00);//测试时间 cout<<"系统测试时间是: "; testtime.print_24(); TimeSystemtime;//重载构造函数,并输出系统时间 cout< "; Systemtime.print_24(); cout<<"系统时间是(12小时制): "; Systemtime.print_12(); Timetime0(23,59),time1(05,40),sumtime=time0+time1,difft=time1-time0; cout< "; time0.print_24(); cout<<"time1是: "; time1.print_24(); cout<<"time0+time1="; sumtime.print_24(); cout<<"time0-time1="; difft.print_24(); return0;} 4遇到的问题与分析 1、理解如何提取系统时间化为当前的年月日时间。 参照源程序,可以大致了解到时利用了ctime文件里面定义的函数提取系统时间的,然后由time_tt=time(NULL);函数取得系统走过的时间值,然后利用gm=gmtime(&t);保存到结构体中,这样我们就可以很方便地提取出结构体中的年月日的等时间信息。 其中: 年份上加1900,月份上加1,小时数加上8。 2、重载运算符“+”“-”,实现时间的相加减。 算数重载并不难,而时间的加减法重载却要十分注意格式转换,时间是采用24、12小时制和60分钟制的,所以在两个时间相加减的时候就要充分考虑到这一点才能写程序约束时间,不让它超出正常范围内。 5实验体会 通过这次日期类的程序编写,最大的感受是更加熟练地运用类的各种运算,以及感受到重载函数的方便之处了,经过重载“+”“-”运算符号,我们就可以直接利用它们进行时间的加减,这无疑给时间转换带来极大的方便。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 实验 日期 Date